QGIS API Documentation 3.39.0-Master (47f7b3a4989)
Loading...
Searching...
No Matches
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
19#include "qgsprocessingutils.h"
20#include "qgsunittypes.h"
21#include "qgsproviderregistry.h"
22#include "qgsprocessing.h"
23
24//
25// QgsProcessingContext
26//
27
29 : mPreferredVectorFormat( QgsProcessingUtils::defaultVectorExtension() )
30 , mPreferredRasterFormat( QgsProcessingUtils::defaultRasterExtension() )
31{
32 auto callback = [this]( const QgsFeature & feature )
33 {
34 if ( mFeedback )
35 mFeedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
36 };
37 mTransformErrorCallback = callback;
38 mExpressionContext.setLoadedLayerStore( &tempLayerStore );
39}
40
42{
43 for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
44 {
45 delete it.value().postProcessor();
46 }
47}
48
50{
51 mExpressionContext = context;
52 // any layers temporarily loaded by expressions should use the same temporary layer store as this context
53 mExpressionContext.setLoadedLayerStore( &tempLayerStore );
54}
55
56void QgsProcessingContext::setLayersToLoadOnCompletion( const QMap<QString, QgsProcessingContext::LayerDetails> &layers )
57{
58 for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
59 {
60 if ( !layers.contains( it.key() ) || layers.value( it.key() ).postProcessor() != it.value().postProcessor() )
61 delete it.value().postProcessor();
62 }
63 mLayersToLoadOnCompletion = layers;
64}
65
67{
68 if ( mLayersToLoadOnCompletion.contains( layer ) && mLayersToLoadOnCompletion.value( layer ).postProcessor() != details.postProcessor() )
69 delete mLayersToLoadOnCompletion.value( layer ).postProcessor();
70
71 mLayersToLoadOnCompletion.insert( layer, details );
72}
73
75{
76 mInvalidGeometryCheck = check;
77 mUseDefaultInvalidGeometryCallback = true;
78 mInvalidGeometryCallback = defaultInvalidGeometryCallbackForCheck( check );
79}
80
81std::function<void ( const QgsFeature & )> QgsProcessingContext::invalidGeometryCallback( QgsFeatureSource *source ) const
82{
83 if ( mUseDefaultInvalidGeometryCallback )
84 return defaultInvalidGeometryCallbackForCheck( mInvalidGeometryCheck, source );
85 else
86 return mInvalidGeometryCallback;
87}
88
90{
91 const QString sourceName = source ? source->sourceName() : QString();
92 switch ( check )
93 {
95 {
96 auto callback = [sourceName]( const QgsFeature & feature )
97 {
98 if ( !sourceName.isEmpty() )
99 throw QgsProcessingException( QObject::tr( "Feature (%1) from “%2” has invalid geometry. Please fix the geometry or change the “Invalid features filtering” option for this input or globally in Processing settings." ).arg( feature.id() ).arg( sourceName ) );
100 else
101 throw QgsProcessingException( QObject::tr( "Feature (%1) has invalid geometry. Please fix the geometry or change the “Invalid features filtering” option for input layers or globally in Processing settings." ).arg( feature.id() ) );
102 };
103 return callback;
104 }
105
107 {
108 auto callback = [this, sourceName]( const QgsFeature & feature )
109 {
110 if ( mFeedback )
111 {
112 if ( !sourceName.isEmpty() )
113 mFeedback->reportError( QObject::tr( "Feature (%1) from “%2” has invalid geometry and has been skipped. Please fix the geometry or change the “Invalid features filtering” option for this input or globally in Processing settings." ).arg( feature.id() ).arg( sourceName ) );
114 else
115 mFeedback->reportError( QObject::tr( "Feature (%1) has invalid geometry and has been skipped. Please fix the geometry or change the “Invalid features filtering” option for input layers or globally in Processing settings." ).arg( feature.id() ) );
116 }
117 };
118 return callback;
119 }
120
122 return nullptr;
123 }
124 return nullptr;
125}
126
128{
129 setLayersToLoadOnCompletion( context.mLayersToLoadOnCompletion );
130 mModelResult = context.mModelResult;
131 context.mLayersToLoadOnCompletion.clear();
132 tempLayerStore.transferLayersFromStore( context.temporaryLayerStore() );
133}
134
136{
137 return QgsProcessingUtils::mapLayerFromString( identifier, *this, false );
138}
139
141{
142 return tempLayerStore.takeMapLayer( tempLayerStore.mapLayer( id ) );
143}
144
146{
147 return mLogLevel;
148}
149
151{
152 mLogLevel = level;
153}
154
156{
157 return mTemporaryFolderOverride;
158}
159
160void QgsProcessingContext::setTemporaryFolder( const QString &folder )
161{
162 mTemporaryFolderOverride = folder;
163}
164
166{
167 return mMaximumThreads;
168}
169
171{
172 mMaximumThreads = threads;
173}
174
176{
177 QVariantMap res;
178 if ( mDistanceUnit != Qgis::DistanceUnit::Unknown )
179 res.insert( QStringLiteral( "distance_units" ), QgsUnitTypes::encodeUnit( mDistanceUnit ) );
180 if ( mAreaUnit != Qgis::AreaUnit::Unknown )
181 res.insert( QStringLiteral( "area_units" ), QgsUnitTypes::encodeUnit( mAreaUnit ) );
182 if ( !mEllipsoid.isEmpty() )
183 res.insert( QStringLiteral( "ellipsoid" ), mEllipsoid );
184 if ( mProject )
185 res.insert( QStringLiteral( "project_path" ), mProject->fileName() );
186
187 return res;
188}
189
191{
192 auto escapeIfNeeded = []( const QString & input ) -> QString
193 {
194 // play it safe and escape everything UNLESS it's purely alphanumeric characters (and a very select scattering of other common characters!)
195 const thread_local QRegularExpression nonAlphaNumericRx( QStringLiteral( "[^a-zA-Z0-9.\\-/_]" ) );
196 if ( nonAlphaNumericRx.match( input ).hasMatch() )
197 {
198 QString escaped = input;
199 escaped.replace( '\'', QLatin1String( "'\\''" ) );
200 return QStringLiteral( "'%1'" ).arg( escaped );
201 }
202 else
203 {
204 return input;
205 }
206 };
207
208 QStringList res;
209 if ( mDistanceUnit != Qgis::DistanceUnit::Unknown )
210 res << QStringLiteral( "--distance_units=%1" ).arg( QgsUnitTypes::encodeUnit( mDistanceUnit ) );
211 if ( mAreaUnit != Qgis::AreaUnit::Unknown )
212 res << QStringLiteral( "--area_units=%1" ).arg( QgsUnitTypes::encodeUnit( mAreaUnit ) );
213 if ( !mEllipsoid.isEmpty() )
214 res << QStringLiteral( "--ellipsoid=%1" ).arg( mEllipsoid );
215
217 {
218 res << QStringLiteral( "--project_path=%1" ).arg( escapeIfNeeded( mProject->fileName() ) );
219 }
220
221 return res;
222}
223
225{
226 return mCurrentTimeRange;
227}
228
230{
231 mCurrentTimeRange = currentTimeRange;
232}
233
235{
236 return mEllipsoid;
237}
238
239void QgsProcessingContext::setEllipsoid( const QString &ellipsoid )
240{
241 mEllipsoid = ellipsoid;
242}
243
245{
246 return mDistanceUnit;
247}
248
250{
251 mDistanceUnit = unit;
252}
253
255{
256 return mAreaUnit;
257}
258
260{
261 mAreaUnit = areaUnit;
262}
263
268
270{
271 if ( mPostProcessor && mPostProcessor != processor )
272 delete mPostProcessor;
273
274 mPostProcessor = processor;
275}
276
278{
279 if ( !layer )
280 return;
281
282 const bool preferFilenameAsLayerName = QgsProcessing::settingsPreferFilenameAsLayerName->value();
283
284 // note - for temporary layers, we don't use the filename, regardless of user setting (it will be meaningless!)
285 if ( ( !forceName && preferFilenameAsLayerName && !layer->isTemporary() ) || name.isEmpty() )
286 {
287 const QVariantMap sourceParts = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->source() );
288 const QString layerName = sourceParts.value( QStringLiteral( "layerName" ) ).toString();
289 // if output layer name exists, use that!
290 if ( !layerName.isEmpty() )
291 layer->setName( layerName );
292 else
293 {
294 const QString path = sourceParts.value( QStringLiteral( "path" ) ).toString();
295 if ( !path.isEmpty() )
296 {
297 const QFileInfo fi( path );
298 layer->setName( fi.baseName() );
299 }
300 else if ( !name.isEmpty() )
301 {
302 // fallback to parameter's name -- shouldn't happen!
303 layer->setName( name );
304 }
305 }
306 }
307 else
308 {
309 layer->setName( name );
310 }
311}
312
313
318
319void QgsProcessingContext::setModelInitialRunConfig( std::unique_ptr< QgsProcessingModelInitialRunConfig > config )
320{
321 mModelConfig = std::move( config );
322}
323
324std::unique_ptr< QgsProcessingModelInitialRunConfig > QgsProcessingContext::takeModelInitialRunConfig()
325{
326 return std::move( mModelConfig );
327}
DistanceUnit
Units of distance.
Definition qgis.h:4363
@ Unknown
Unknown distance unit.
AreaUnit
Units of area.
Definition qgis.h:4401
@ Unknown
Unknown areal unit.
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
Definition qgis.h:1875
@ NoCheck
No invalid geometry checking.
@ AbortOnInvalid
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...
@ SkipInvalid
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
ProcessingLogLevel
Logging level for algorithms to use when pushing feedback messages.
Definition qgis.h:3102
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setLoadedLayerStore(QgsMapLayerStore *store)
Sets the destination layer store for any layers loaded during expression evaluation.
An interface for objects which provide features via a getFeatures method.
virtual QString sourceName() const =0
Returns a friendly display name for the source.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsMapLayer * takeMapLayer(QgsMapLayer *layer)
Takes a layer from the store.
QgsMapLayer * mapLayer(const QString &id) const
Retrieve a pointer to a layer by layer id.
void transferLayersFromStore(QgsMapLayerStore *other)
Transfers all the map layers contained within another map layer store and adds them to this store.
Base class for all map layer types.
Definition qgsmaplayer.h:75
virtual bool isTemporary() const
Returns true if the layer is considered a temporary layer.
QString source() const
Returns the source for the layer.
QString providerType() const
Returns the provider type (provider key) for this layer.
void setName(const QString &name)
Set the display name of the layer.
Details for layers to load into projects.
QgsProcessingLayerPostProcessorInterface * postProcessor() const
Layer post-processor.
void setPostProcessor(QgsProcessingLayerPostProcessorInterface *processor)
Sets the layer post-processor.
void setOutputLayerName(QgsMapLayer *layer) const
Sets a layer name to match this output, respecting any local user settings which affect this name.
Contains information about the context in which a processing algorithm is executed.
QgsProcessingContext::Flags flags() const
Returns any flags set in the context.
@ IncludeProjectPath
Include the associated project path argument.
QFlags< ProcessArgumentFlag > ProcessArgumentFlags
std::unique_ptr< QgsProcessingModelInitialRunConfig > takeModelInitialRunConfig()
Takes the model initial run configuration from the context.
QgsDateTimeRange currentTimeRange() const
Returns the current time range to use for temporal operations.
void takeResultsFrom(QgsProcessingContext &context)
Takes the results from another context and merges them with the results currently stored in this cont...
QVariantMap exportToMap() const
Exports the context's settings to a variant map.
Qgis::AreaUnit areaUnit() const
Returns the area unit to use for area calculations.
QStringList asQgisProcessArguments(QgsProcessingContext::ProcessArgumentFlags flags=QgsProcessingContext::ProcessArgumentFlags()) const
Returns list of the equivalent qgis_process arguments representing the settings from the context.
void setLogLevel(Qgis::ProcessingLogLevel level)
Sets the logging level for algorithms to use when pushing feedback messages to users.
QgsMapLayer * getMapLayer(const QString &identifier)
Returns a map layer from the context with a matching identifier.
std::function< void(const QgsFeature &) > invalidGeometryCallback(QgsFeatureSource *source=nullptr) const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
void setMaximumThreads(int threads)
Sets the (optional) number of threads to use when running algorithms.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
std::function< void(const QgsFeature &) > defaultInvalidGeometryCallbackForCheck(Qgis::InvalidGeometryCheck check, QgsFeatureSource *source=nullptr) const
Returns the default callback function to use for a particular invalid geometry check.
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...
QgsMapLayer * takeResultLayer(const QString &id)
Takes the result map layer with matching id from the context and transfers ownership of it back to th...
void setDistanceUnit(Qgis::DistanceUnit unit)
Sets the unit to use for distance calculations.
void setInvalidGeometryCheck(Qgis::InvalidGeometryCheck check)
Sets the behavior used for checking invalid geometries in input layers.
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.
void setAreaUnit(Qgis::AreaUnit areaUnit)
Sets the unit to use for area calculations.
void setEllipsoid(const QString &ellipsoid)
Sets a specified ellipsoid to use for distance and area calculations.
QgsProcessingModelInitialRunConfig * modelInitialRunConfig()
Returns a reference to the model initial run configuration, used to run a model algorithm.
Qgis::DistanceUnit distanceUnit() const
Returns the distance unit to use for distance calculations.
Qgis::ProcessingLogLevel logLevel() const
Returns the logging level for algorithms to use when pushing feedback messages to users.
QgsProcessingContext()
Constructor for QgsProcessingContext.
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
void setModelInitialRunConfig(std::unique_ptr< QgsProcessingModelInitialRunConfig > config)
Sets the model initial run configuration, used to run a model algorithm.
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
void setTemporaryFolder(const QString &folder)
Sets the (optional) temporary folder to use when running algorithms.
QString temporaryFolder() const
Returns the (optional) temporary folder to use when running algorithms.
void setCurrentTimeRange(const QgsDateTimeRange &currentTimeRange)
Sets the current time range to use for temporal operations.
int maximumThreads() const
Returns the (optional) number of threads to use when running algorithms.
Custom exception class for processing related exceptions.
An interface for layer post-processing handlers for execution following a processing algorithm operat...
Configuration settings which control how a Processing model is executed.
Utility functions for use with processing classes.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags=QgsProcessing::LayerOptionsFlags())
Interprets a string as a map layer within the supplied context.
static const QgsSettingsEntryBool * settingsPreferFilenameAsLayerName
Settings entry prefer filename as layer name.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.