QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsprocessingcontext.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingcontext.cpp
3  ----------------------
4  begin : April 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsprocessingcontext.h"
19 #include "qgsprocessingutils.h"
20 #include "qgsproviderregistry.h"
21 #include "qgssettings.h"
22 
24  : mPreferredVectorFormat( QgsProcessingUtils::defaultVectorExtension() )
25  , mPreferredRasterFormat( QgsProcessingUtils::defaultRasterExtension() )
26 {
27  auto callback = [ = ]( const QgsFeature & feature )
28  {
29  if ( mFeedback )
30  mFeedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
31  };
32  mTransformErrorCallback = callback;
33 }
34 
36 {
37  for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
38  {
39  delete it.value().postProcessor();
40  }
41 }
42 
43 void QgsProcessingContext::setLayersToLoadOnCompletion( const QMap<QString, QgsProcessingContext::LayerDetails> &layers )
44 {
45  for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
46  {
47  if ( !layers.contains( it.key() ) || layers.value( it.key() ).postProcessor() != it.value().postProcessor() )
48  delete it.value().postProcessor();
49  }
50  mLayersToLoadOnCompletion = layers;
51 }
52 
54 {
55  if ( mLayersToLoadOnCompletion.contains( layer ) && mLayersToLoadOnCompletion.value( layer ).postProcessor() != details.postProcessor() )
56  delete mLayersToLoadOnCompletion.value( layer ).postProcessor();
57 
58  mLayersToLoadOnCompletion.insert( layer, details );
59 }
60 
62 {
63  mInvalidGeometryCheck = check;
64  mInvalidGeometryCallback = defaultInvalidGeometryCallbackForCheck( check );
65 }
66 
68 {
69  switch ( check )
70  {
72  {
73  auto callback = []( const QgsFeature & feature )
74  {
75  throw QgsProcessingException( QObject::tr( "Feature (%1) has invalid geometry. Please fix the geometry or change the Processing setting to the \"Ignore invalid input features\" option." ).arg( feature.id() ) );
76  };
77  return callback;
78  }
79 
81  {
82  auto callback = [ = ]( const QgsFeature & feature )
83  {
84  if ( mFeedback )
85  mFeedback->reportError( QObject::tr( "Feature (%1) has invalid geometry and has been skipped. Please fix the geometry or change the Processing setting to the \"Ignore invalid input features\" option." ).arg( feature.id() ) );
86  };
87  return callback;
88  }
89 
91  return nullptr;
92  }
93  return nullptr;
94 }
95 
97 {
98  setLayersToLoadOnCompletion( context.mLayersToLoadOnCompletion );
99  context.mLayersToLoadOnCompletion.clear();
100  tempLayerStore.transferLayersFromStore( context.temporaryLayerStore() );
101 }
102 
103 QgsMapLayer *QgsProcessingContext::getMapLayer( const QString &identifier )
104 {
105  return QgsProcessingUtils::mapLayerFromString( identifier, *this, false );
106 }
107 
109 {
110  return tempLayerStore.takeMapLayer( tempLayerStore.mapLayer( id ) );
111 }
112 
113 
114 
116 {
117  return mPostProcessor;
118 }
119 
121 {
122  if ( mPostProcessor && mPostProcessor != processor )
123  delete mPostProcessor;
124 
125  mPostProcessor = processor;
126 }
127 
129 {
130  if ( !layer )
131  return;
132 
133  const bool preferFilenameAsLayerName = QgsSettings().value( QStringLiteral( "Processing/Configuration/PREFER_FILENAME_AS_LAYER_NAME" ), true ).toBool();
134 
135  // note - for temporary layers, we don't use the filename, regardless of user setting (it will be meaningless!)
136  if ( ( preferFilenameAsLayerName && !layer->isTemporary() ) || name.isEmpty() )
137  {
138  const QVariantMap sourceParts = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->source() );
139  const QString layerName = sourceParts.value( QStringLiteral( "layerName" ) ).toString();
140  // if output layer name exists, use that!
141  if ( !layerName.isEmpty() )
142  layer->setName( layerName );
143  else
144  {
145  const QString path = sourceParts.value( QStringLiteral( "path" ) ).toString();
146  if ( !path.isEmpty() )
147  {
148  const QFileInfo fi( path );
149  layer->setName( fi.baseName() );
150  }
151  else if ( !name.isEmpty() )
152  {
153  // fallback to parameter's name -- shouldn't happen!
154  layer->setName( name );
155  }
156  }
157  }
158  else
159  {
160  layer->setName( name );
161  }
162 }
QgsMapLayerStore::transferLayersFromStore
void transferLayersFromStore(QgsMapLayerStore *other)
Transfers all the map layers contained within another map layer store and adds them to this store.
Definition: qgsmaplayerstore.cpp:207
QgsProcessingContext::LayerDetails::setPostProcessor
void setPostProcessor(QgsProcessingLayerPostProcessorInterface *processor)
Sets the layer post-processor.
Definition: qgsprocessingcontext.cpp:120
QgsFeatureRequest::GeometryAbortOnInvalid
@ GeometryAbortOnInvalid
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...
Definition: qgsfeaturerequest.h:116
QgsProcessingContext::~QgsProcessingContext
~QgsProcessingContext()
Definition: qgsprocessingcontext.cpp:35
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsProcessingContext::takeResultLayer
QgsMapLayer * takeResultLayer(const QString &id)
Takes the result map layer with matching id from the context and transfers ownership of it back to th...
Definition: qgsprocessingcontext.cpp:108
QgsProcessingContext::setLayersToLoadOnCompletion
void setLayersToLoadOnCompletion(const QMap< QString, QgsProcessingContext::LayerDetails > &layers)
Sets the map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completion...
Definition: qgsprocessingcontext.cpp:43
QgsSettings
Definition: qgssettings.h:61
QgsFeatureRequest::InvalidGeometryCheck
InvalidGeometryCheck
Handling of features with invalid geometries.
Definition: qgsfeaturerequest.h:112
QgsFeatureRequest::GeometrySkipInvalid
@ GeometrySkipInvalid
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
Definition: qgsfeaturerequest.h:115
QgsMapLayerStore::mapLayer
QgsMapLayer * mapLayer(const QString &id) const
Retrieve a pointer to a layer by layer id.
Definition: qgsmaplayerstore.cpp:49
QgsMapLayer::providerType
QString providerType() const
Returns the provider type (provider key) for this layer.
Definition: qgsmaplayer.cpp:1614
QgsProcessingContext::LayerDetails::setOutputLayerName
void setOutputLayerName(QgsMapLayer *layer) const
Sets a layer name to match this output, respecting any local user settings which affect this name.
Definition: qgsprocessingcontext.cpp:128
QgsProcessingContext::takeResultsFrom
void takeResultsFrom(QgsProcessingContext &context)
Takes the results from another context and merges them with the results currently stored in this cont...
Definition: qgsprocessingcontext.cpp:96
qgsproviderregistry.h
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsProcessingUtils
Definition: qgsprocessingutils.h:50
QgsProcessingContext::setInvalidGeometryCheck
void setInvalidGeometryCheck(QgsFeatureRequest::InvalidGeometryCheck check)
Sets the behavior used for checking invalid geometries in input layers.
Definition: qgsprocessingcontext.cpp:61
QgsProviderRegistry::decodeUri
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
Definition: qgsproviderregistry.cpp:431
QgsProcessingLayerPostProcessorInterface
An interface for layer post-processing handlers for execution following a processing algorithm operat...
Definition: qgsprocessingcontext.h:572
QgsProcessingContext::LayerDetails::postProcessor
QgsProcessingLayerPostProcessorInterface * postProcessor() const
Layer post-processor.
Definition: qgsprocessingcontext.cpp:115
QgsProcessingContext::LayerDetails
Details for layers to load into projects.
Definition: qgsprocessingcontext.h:158
QgsMapLayerStore::takeMapLayer
QgsMapLayer * takeMapLayer(QgsMapLayer *layer)
Takes a layer from the store.
Definition: qgsmaplayerstore.cpp:177
QgsProcessingContext::getMapLayer
QgsMapLayer * getMapLayer(const QString &identifier)
Returns a map layer from the context with a matching identifier.
Definition: qgsprocessingcontext.cpp:103
QgsProcessingContext::temporaryLayerStore
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
Definition: qgsprocessingcontext.h:151
QgsProcessingContext::QgsProcessingContext
QgsProcessingContext()
Constructor for QgsProcessingContext.
Definition: qgsprocessingcontext.cpp:23
QgsMapLayer::isTemporary
virtual bool isTemporary() const
Returns true if the layer is considered a temporary layer.
Definition: qgsmaplayer.cpp:1745
qgsprocessingutils.h
QgsProcessingContext::defaultInvalidGeometryCallbackForCheck
std::function< void(const QgsFeature &) > defaultInvalidGeometryCallbackForCheck(QgsFeatureRequest::InvalidGeometryCheck check) const
Returns the default callback function to use for a particular invalid geometry check.
Definition: qgsprocessingcontext.cpp:67
QgsMapLayer::source
QString source() const
Returns the source for the layer.
Definition: qgsmaplayer.cpp:192
QgsProcessingContext::addLayerToLoadOnCompletion
void addLayerToLoadOnCompletion(const QString &layer, const QgsProcessingContext::LayerDetails &details)
Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
Definition: qgsprocessingcontext.cpp:53
QgsMapLayer::setName
void setName(const QString &name)
Set the display name of the layer.
Definition: qgsmaplayer.cpp:153
QgsMapLayer
Definition: qgsmaplayer.h:81
qgssettings.h
QgsFeatureRequest::GeometryNoCheck
@ GeometryNoCheck
No invalid geometry checking.
Definition: qgsfeaturerequest.h:114
qgsprocessingcontext.h
QgsProcessingUtils::mapLayerFromString
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType)
Interprets a string as a map layer within the supplied context.
Definition: qgsprocessingutils.cpp:314
QgsFeature
Definition: qgsfeature.h:55
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition: qgsproviderregistry.cpp:48
QgsProcessingException
Definition: qgsexception.h:82