QGIS API Documentation 3.31.0-Master (b8458777cd)
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
25 : mPreferredVectorFormat( QgsProcessingUtils::defaultVectorExtension() )
26 , mPreferredRasterFormat( QgsProcessingUtils::defaultRasterExtension() )
27{
28 auto callback = [ = ]( const QgsFeature & feature )
29 {
30 if ( mFeedback )
31 mFeedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
32 };
33 mTransformErrorCallback = callback;
34 mExpressionContext.setLoadedLayerStore( &tempLayerStore );
35}
36
38{
39 for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
40 {
41 delete it.value().postProcessor();
42 }
43}
44
46{
47 mExpressionContext = context;
48 // any layers temporarily loaded by expressions should use the same temporary layer store as this context
49 mExpressionContext.setLoadedLayerStore( &tempLayerStore );
50}
51
52void QgsProcessingContext::setLayersToLoadOnCompletion( const QMap<QString, QgsProcessingContext::LayerDetails> &layers )
53{
54 for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
55 {
56 if ( !layers.contains( it.key() ) || layers.value( it.key() ).postProcessor() != it.value().postProcessor() )
57 delete it.value().postProcessor();
58 }
59 mLayersToLoadOnCompletion = layers;
60}
61
63{
64 if ( mLayersToLoadOnCompletion.contains( layer ) && mLayersToLoadOnCompletion.value( layer ).postProcessor() != details.postProcessor() )
65 delete mLayersToLoadOnCompletion.value( layer ).postProcessor();
66
67 mLayersToLoadOnCompletion.insert( layer, details );
68}
69
71{
72 mInvalidGeometryCheck = check;
73 mUseDefaultInvalidGeometryCallback = true;
74 mInvalidGeometryCallback = defaultInvalidGeometryCallbackForCheck( check );
75}
76
77std::function<void ( const QgsFeature & )> QgsProcessingContext::invalidGeometryCallback( QgsFeatureSource *source ) const
78{
79 if ( mUseDefaultInvalidGeometryCallback )
80 return defaultInvalidGeometryCallbackForCheck( mInvalidGeometryCheck, source );
81 else
82 return mInvalidGeometryCallback;
83}
84
86{
87 const QString sourceName = source ? source->sourceName() : QString();
88 switch ( check )
89 {
91 {
92 auto callback = [sourceName]( const QgsFeature & feature )
93 {
94 if ( !sourceName.isEmpty() )
95 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 ) );
96 else
97 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() ) );
98 };
99 return callback;
100 }
101
103 {
104 auto callback = [ = ]( const QgsFeature & feature )
105 {
106 if ( mFeedback )
107 {
108 if ( !sourceName.isEmpty() )
109 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 ) );
110 else
111 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() ) );
112 }
113 };
114 return callback;
115 }
116
118 return nullptr;
119 }
120 return nullptr;
121}
122
124{
125 setLayersToLoadOnCompletion( context.mLayersToLoadOnCompletion );
126 context.mLayersToLoadOnCompletion.clear();
127 tempLayerStore.transferLayersFromStore( context.temporaryLayerStore() );
128}
129
131{
132 return QgsProcessingUtils::mapLayerFromString( identifier, *this, false );
133}
134
136{
137 return tempLayerStore.takeMapLayer( tempLayerStore.mapLayer( id ) );
138}
139
141{
142 return mLogLevel;
143}
144
146{
147 mLogLevel = level;
148}
149
151{
152 QVariantMap res;
153 if ( mDistanceUnit != Qgis::DistanceUnit::Unknown )
154 res.insert( QStringLiteral( "distance_units" ), QgsUnitTypes::encodeUnit( mDistanceUnit ) );
155 if ( mAreaUnit != Qgis::AreaUnit::Unknown )
156 res.insert( QStringLiteral( "area_units" ), QgsUnitTypes::encodeUnit( mAreaUnit ) );
157 if ( !mEllipsoid.isEmpty() )
158 res.insert( QStringLiteral( "ellipsoid" ), mEllipsoid );
159 if ( mProject )
160 res.insert( QStringLiteral( "project_path" ), mProject->fileName() );
161
162 return res;
163}
164
165QStringList QgsProcessingContext::asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags ) const
166{
167 auto escapeIfNeeded = []( const QString & input ) -> QString
168 {
169 // play it safe and escape everything UNLESS it's purely alphanumeric characters (and a very select scattering of other common characters!)
170 const thread_local QRegularExpression nonAlphaNumericRx( QStringLiteral( "[^a-zA-Z0-9.\\-/_]" ) );
171 if ( nonAlphaNumericRx.match( input ).hasMatch() )
172 {
173 QString escaped = input;
174 escaped.replace( '\'', QLatin1String( "'\\''" ) );
175 return QStringLiteral( "'%1'" ).arg( escaped );
176 }
177 else
178 {
179 return input;
180 }
181 };
182
183 QStringList res;
184 if ( mDistanceUnit != Qgis::DistanceUnit::Unknown )
185 res << QStringLiteral( "--distance_units=%1" ).arg( QgsUnitTypes::encodeUnit( mDistanceUnit ) );
186 if ( mAreaUnit != Qgis::AreaUnit::Unknown )
187 res << QStringLiteral( "--area_units=%1" ).arg( QgsUnitTypes::encodeUnit( mAreaUnit ) );
188 if ( !mEllipsoid.isEmpty() )
189 res << QStringLiteral( "--ellipsoid=%1" ).arg( mEllipsoid );
190
192 {
193 res << QStringLiteral( "--project_path=%1" ).arg( escapeIfNeeded( mProject->fileName() ) );
194 }
195
196 return res;
197}
198
200{
201 return mCurrentTimeRange;
202}
203
204void QgsProcessingContext::setCurrentTimeRange( const QgsDateTimeRange &currentTimeRange )
205{
206 mCurrentTimeRange = currentTimeRange;
207}
208
210{
211 return mEllipsoid;
212}
213
214void QgsProcessingContext::setEllipsoid( const QString &ellipsoid )
215{
216 mEllipsoid = ellipsoid;
217}
218
220{
221 return mDistanceUnit;
222}
223
225{
226 mDistanceUnit = unit;
227}
228
230{
231 return mAreaUnit;
232}
233
235{
236 mAreaUnit = areaUnit;
237}
238
240{
241 return mPostProcessor;
242}
243
245{
246 if ( mPostProcessor && mPostProcessor != processor )
247 delete mPostProcessor;
248
249 mPostProcessor = processor;
250}
251
253{
254 if ( !layer )
255 return;
256
257 const bool preferFilenameAsLayerName = QgsProcessing::settingsPreferFilenameAsLayerName->value();
258
259 // note - for temporary layers, we don't use the filename, regardless of user setting (it will be meaningless!)
260 if ( ( !forceName && preferFilenameAsLayerName && !layer->isTemporary() ) || name.isEmpty() )
261 {
262 const QVariantMap sourceParts = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->source() );
263 const QString layerName = sourceParts.value( QStringLiteral( "layerName" ) ).toString();
264 // if output layer name exists, use that!
265 if ( !layerName.isEmpty() )
266 layer->setName( layerName );
267 else
268 {
269 const QString path = sourceParts.value( QStringLiteral( "path" ) ).toString();
270 if ( !path.isEmpty() )
271 {
272 const QFileInfo fi( path );
273 layer->setName( fi.baseName() );
274 }
275 else if ( !name.isEmpty() )
276 {
277 // fallback to parameter's name -- shouldn't happen!
278 layer->setName( name );
279 }
280 }
281 }
282 else
283 {
284 layer->setName( name );
285 }
286}
DistanceUnit
Units of distance.
Definition: qgis.h:3047
AreaUnit
Units of area.
Definition: qgis.h:3084
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.
InvalidGeometryCheck
Handling of features with invalid geometries.
@ GeometryNoCheck
No invalid geometry checking.
@ GeometryAbortOnInvalid
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...
@ GeometrySkipInvalid
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
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:56
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:73
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.
LogLevel
Logging level for algorithms to use when pushing feedback messages.
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.
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 setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
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 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.
Qgis::DistanceUnit distanceUnit() const
Returns the distance unit to use for distance calculations.
QgsProcessingContext()
Constructor for QgsProcessingContext.
void setLogLevel(LogLevel level)
Sets the logging level for algorithms to use when pushing feedback messages to users.
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
void setInvalidGeometryCheck(QgsFeatureRequest::InvalidGeometryCheck check)
Sets the behavior used for checking invalid geometries in input layers.
LogLevel logLevel() const
Returns the logging level for algorithms to use when pushing feedback messages to users.
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
std::function< void(const QgsFeature &) > defaultInvalidGeometryCallbackForCheck(QgsFeatureRequest::InvalidGeometryCheck check, QgsFeatureSource *source=nullptr) const
Returns the default callback function to use for a particular invalid geometry check.
void setCurrentTimeRange(const QgsDateTimeRange &currentTimeRange)
Sets the current time range to use for temporal operations.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
An interface for layer post-processing handlers for execution following a processing algorithm operat...
Utility functions for use with processing classes.
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.
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.