33 auto callback = [
this](
const QgsFeature & feature )
36 mFeedback->reportError( QObject::tr(
"Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
38 mTransformErrorCallback = callback;
39 mExpressionContext.setLoadedLayerStore( &tempLayerStore );
44 for (
auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
46 delete it.value().postProcessor();
52 mExpressionContext = context;
59 for (
auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
61 if ( !layers.contains( it.key() ) || layers.value( it.key() ).postProcessor() != it.value().postProcessor() )
62 delete it.value().postProcessor();
64 mLayersToLoadOnCompletion = layers;
69 if ( mLayersToLoadOnCompletion.contains( layer ) && mLayersToLoadOnCompletion.value( layer ).postProcessor() != details.
postProcessor() )
70 delete mLayersToLoadOnCompletion.value( layer ).postProcessor();
72 mLayersToLoadOnCompletion.insert( layer, details );
77 mInvalidGeometryCheck = check;
78 mUseDefaultInvalidGeometryCallback =
true;
84 if ( mUseDefaultInvalidGeometryCallback )
87 return mInvalidGeometryCallback;
92 const QString sourceName = source ? source->
sourceName() : QString();
97 auto callback = [sourceName](
const QgsFeature & feature )
99 if ( !sourceName.isEmpty() )
100 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 ) );
102 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() ) );
109 auto callback = [
this, sourceName](
const QgsFeature & feature )
113 if ( !sourceName.isEmpty() )
114 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 ) );
116 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() ) );
131 mModelResult = context.mModelResult;
132 context.mLayersToLoadOnCompletion.clear();
143 return tempLayerStore.takeMapLayer( tempLayerStore.mapLayer(
id ) );
158 return mTemporaryFolderOverride;
163 mTemporaryFolderOverride = folder;
168 return mMaximumThreads;
173 mMaximumThreads = threads;
183 if ( !mEllipsoid.isEmpty() )
184 res.insert( QStringLiteral(
"ellipsoid" ), mEllipsoid );
186 res.insert( QStringLiteral(
"project_path" ), mProject->fileName() );
193 auto escapeIfNeeded = [](
const QString & input ) -> QString
196 const thread_local QRegularExpression nonAlphaNumericRx( QStringLiteral(
"[^a-zA-Z0-9.\\-/_]" ) );
197 if ( nonAlphaNumericRx.match( input ).hasMatch() )
199 QString escaped = input;
200 escaped.replace(
'\'', QLatin1String(
"'\\''" ) );
201 return QStringLiteral(
"'%1'" ).arg( escaped );
214 if ( !mEllipsoid.isEmpty() )
215 res << QStringLiteral(
"--ellipsoid=%1" ).arg( mEllipsoid );
219 res << QStringLiteral(
"--project_path=%1" ).arg( escapeIfNeeded( mProject->fileName() ) );
227 return mCurrentTimeRange;
247 return mDistanceUnit;
252 mDistanceUnit = unit;
267 return mPostProcessor;
272 if ( mPostProcessor && mPostProcessor != processor )
273 delete mPostProcessor;
275 mPostProcessor = processor;
289 const QString layerName = sourceParts.value( QStringLiteral(
"layerName" ) ).toString();
291 if ( !layerName.isEmpty() )
295 const QString path = sourceParts.value( QStringLiteral(
"path" ) ).toString();
296 if ( !path.isEmpty() )
298 const QFileInfo fi( path );
299 layer->
setName( fi.baseName() );
301 else if ( !
name.isEmpty() )
317 return mModelConfig.get();
322 mModelConfig = std::move( config );
327 return std::move( mModelConfig );
332 mModelResult.clear();
DistanceUnit
Units of distance.
@ Unknown
Unknown distance unit.
@ Unknown
Unknown areal unit.
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
@ 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.
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...
Base class for all map layer types.
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.
bool forceName
Set to true if LayerDetails::name should always be used as the loaded layer name, regardless of the u...
QgsProcessingLayerPostProcessorInterface * postProcessor() const
Layer post-processor.
QString name
Friendly name for layer, possibly for use when loading layer into project.
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.
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.
void clearModelResult()
Clears model results previously populated when the context was used to run a model algorithm.
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.
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 ¤tTimeRange)
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.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
QgsTemporalRange< QDateTime > QgsDateTimeRange
QgsRange which stores a range of date times.