QgsProcessingAlgorithm class
Abstract base class for processing algorithms.
Contents
Derived classes
- class QgsProcessingFeatureBasedAlgorithm
- An abstract QgsProcessingAlgorithm base class for processing algorithms which operate "feature-by-feature".
Public types
- enum Flag { FlagHideFromToolbox = 1 << 1, FlagHideFromModeler = 1 << 2, FlagSupportsBatch = 1 << 3, FlagCanCancel = 1 << 4, FlagRequiresMatchingCrs = 1 << 5, FlagNoThreading = 1 << 6, FlagDisplayNameIsLiteral = 1 << 7, FlagSupportsInPlaceEdits = 1 << 8, FlagDeprecated = FlagHideFromToolbox | FlagHideFromModeler }
- Flags indicating how and when an algorithm operates and should be exposed to users.
Constructors, destructors, conversion operators
- QgsProcessingAlgorithm() defaulted
- Constructor for QgsProcessingAlgorithm.
- QgsProcessingAlgorithm(const QgsProcessingAlgorithm& other) deleted
- Algorithms cannot be copied - create() should be used instead.
Public functions
- auto asPythonCommand(const QVariantMap& parameters, QgsProcessingContext& context) const -> QString virtual
- Returns a Python command string which can be executed to run the algorithm using the specified parameters.
- auto canExecute(QString* errorMessage = nullptr) const -> bool virtual
- Returns true if the algorithm can execute.
- auto checkParameterValues(const QVariantMap& parameters, QgsProcessingContext& context, QString* message = nullptr) const -> bool virtual
- Checks the supplied parameter values to verify that they satisfy the requirements of this algorithm in the supplied context.
- auto countVisibleParameters() const -> int
- Returns the number of visible (non-hidden) parameters defined by this algorithm.
- auto create(const QVariantMap& configuration = QVariantMap()) const -> QgsProcessingAlgorithm*
- Creates a copy of the algorithm, ready for execution.
- auto createCustomParametersWidget(QWidget* parent = nullptr) const -> QWidget* virtual
- If an algorithm subclass implements a custom parameters widget, a copy of this widget should be constructed and returned by this method.
- auto createExpressionContext(const QVariantMap& parameters, QgsProcessingContext& context, QgsProcessingFeatureSource* source = nullptr) const -> QgsExpressionContext
- Creates an expression context relating to the algorithm.
- auto destinationParameterDefinitions() const -> QgsProcessingParameterDefinitions
- Returns a list of destination parameters definitions utilized by the algorithm.
- auto displayName() const -> QString pure virtual
- Returns the translated algorithm name, which should be used for any user-visible display of the algorithm name.
- auto flags() const -> Flags virtual
- Returns the flags indicating how and when the algorithm operates and should be exposed to users.
- auto group() const -> QString virtual
- Returns the name of the group this algorithm belongs to.
- auto groupId() const -> QString virtual
- Returns the unique ID of the group this algorithm belongs to.
- auto hasHtmlOutputs() const -> bool
- Returns true if this algorithm generates HTML outputs.
- auto helpString() const -> Q_DECL_DEPRECATED QString deprecated virtual
- Returns a localised help string for the algorithm.
- auto helpUrl() const -> QString virtual
- Returns a url pointing to the algorithm's help page.
- auto icon() const -> QIcon virtual
- Returns an icon for the algorithm.
- auto id() const -> QString
- Returns the unique ID for the algorithm, which is a combination of the algorithm provider's ID and the algorithms unique name (e.g.
- auto name() const -> QString pure virtual
- Returns the algorithm name, used for identifying the algorithm.
- auto operator=(const QgsProcessingAlgorithm& other) -> QgsProcessingAlgorithm& deleted
- Algorithms cannot be copied- create() should be used instead.
- auto outputDefinition(const QString& name) const -> const QgsProcessingOutputDefinition*
- Returns a matching output by name.
- auto outputDefinitions() const -> QgsProcessingOutputDefinitions
- Returns an ordered list of output definitions utilized by the algorithm.
- auto parameterDefinition(const QString& name) const -> const QgsProcessingParameterDefinition*
- Returns a matching parameter by name.
- auto parameterDefinitions() const -> QgsProcessingParameterDefinitions
- Returns an ordered list of parameter definitions utilized by the algorithm.
- auto postProcess(QgsProcessingContext& context, QgsProcessingFeedback* feedback) -> QVariantMap
- Should be called in the main thread following the completion of runPrepared().
- auto prepare(const QVariantMap& parameters, QgsProcessingContext& context, QgsProcessingFeedback* feedback) -> bool
- Prepares the algorithm for execution.
- auto preprocessParameters(const QVariantMap& parameters) -> QVariantMap virtual
- Pre-processes a set of parameters, allowing the algorithm to clean their values.
- auto provider() const -> QgsProcessingProvider*
- Returns the provider to which this algorithm belongs.
- auto run(const QVariantMap& parameters, QgsProcessingContext& context, QgsProcessingFeedback* feedback, bool* ok = nullptr, const QVariantMap& configuration = QVariantMap()) const -> QVariantMap
- Executes the algorithm using the specified parameters.
- auto runPrepared(const QVariantMap& parameters, QgsProcessingContext& context, QgsProcessingFeedback* feedback) -> QVariantMap
- Runs the algorithm, which has been prepared by an earlier call to prepare().
- void setProvider(QgsProcessingProvider* provider)
- Associates this algorithm with its provider.
- auto shortDescription() const -> QString virtual
- Returns an optional translated short description of the algorithm.
- auto shortHelpString() const -> QString virtual
- Returns a localised short helper string for the algorithm.
- auto svgIconPath() const -> QString virtual
- Returns a path to an SVG version of the algorithm's icon.
- auto tags() const -> QStringList virtual
- Returns a list of tags which relate to the algorithm, and are used to assist users in searching for suitable algorithms.
- auto validateInputCrs(const QVariantMap& parameters, QgsProcessingContext& context) const -> bool virtual
- Checks whether the coordinate reference systems for the specified set of parameters are valid for the algorithm.
Protected static functions
- static auto invalidRasterError(const QVariantMap& parameters, const QString& name) -> QString
- Returns a user-friendly string to use as an error when a raster layer input could not be loaded.
- static auto invalidSinkError(const QVariantMap& parameters, const QString& name) -> QString
- Returns a user-friendly string to use as an error when a sink parameter could not be created.
- static auto invalidSourceError(const QVariantMap& parameters, const QString& name) -> QString
- Returns a user-friendly string to use as an error when a source parameter could not be loaded.
Protected functions
- auto addOutput(QgsProcessingOutputDefinition* outputDefinition) -> bool
- Adds an output definition to the algorithm.
- auto addParameter(QgsProcessingParameterDefinition* parameterDefinition, bool createOutput = true) -> bool
- Adds a parameter definition to the algorithm.
- auto createInstance() const -> QgsProcessingAlgorithm* pure virtual
- Creates a new instance of the algorithm class.
- void initAlgorithm(const QVariantMap& configuration = QVariantMap()) pure virtual
- Initializes the algorithm using the specified configuration.
- auto parameterAsBool(const QVariantMap& parameters, const QString& name, const QgsProcessingContext& context) const -> bool
- Evaluates the parameter with matching name to a static boolean value.
- auto parameterAsCompatibleSourceLayerPath(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context, const QStringList& compatibleFormats, const QString& preferredFormat = QString("shp"), QgsProcessingFeedback* feedback = nullptr) -> QString
- Evaluates the parameter with matching name to a source vector layer file path of compatible format.
- auto parameterAsCrs(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QgsCoordinateReferenceSystem
- Evaluates the parameter with matching name to a coordinate reference system.
- auto parameterAsDouble(const QVariantMap& parameters, const QString& name, const QgsProcessingContext& context) const -> double
- Evaluates the parameter with matching name to a static double value.
- auto parameterAsEnum(const QVariantMap& parameters, const QString& name, const QgsProcessingContext& context) const -> int
- Evaluates the parameter with matching name to a enum value.
- auto parameterAsEnums(const QVariantMap& parameters, const QString& name, const QgsProcessingContext& context) const -> QList<int>
- Evaluates the parameter with matching name to list of enum values.
- auto parameterAsExpression(const QVariantMap& parameters, const QString& name, const QgsProcessingContext& context) const -> QString
- Evaluates the parameter with matching name to an expression.
- auto parameterAsExtent(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context, const QgsCoordinateReferenceSystem& crs = QgsCoordinateReferenceSystem()) const -> QgsRectangle
- Evaluates the parameter with matching name to a rectangular extent.
- auto parameterAsExtentCrs(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) -> QgsCoordinateReferenceSystem
- Returns the coordinate reference system associated with an extent parameter value.
- auto parameterAsExtentGeometry(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context, const QgsCoordinateReferenceSystem& crs = QgsCoordinateReferenceSystem()) -> QgsGeometry
- Evaluates the parameter with matching name to a rectangular extent, and returns a geometry covering this extent.
- auto parameterAsFields(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QStringList
- Evaluates the parameter with matching name to a list of fields.
- auto parameterAsFile(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QString
- Evaluates the parameter with matching name to a file/folder name.
- auto parameterAsFileOutput(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QString
- Evaluates the parameter with matching name to a file based output destination.
- auto parameterAsInt(const QVariantMap& parameters, const QString& name, const QgsProcessingContext& context) const -> int
- Evaluates the parameter with matching name to a static integer value.
- auto parameterAsInts(const QVariantMap& parameters, const QString& name, const QgsProcessingContext& context) const -> QList<int>
- Evaluates the parameter with matching name to a list of integer values.
- auto parameterAsLayer(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QgsMapLayer*
- Evaluates the parameter with matching name to a map layer.
- auto parameterAsLayerList(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QList<QgsMapLayer*>
- Evaluates the parameter with matching name to a list of map layers.
- auto parameterAsMatrix(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QVariantList
- Evaluates the parameter with matching name to a matrix/table of values.
- auto parameterAsMeshLayer(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QgsMeshLayer*
- Evaluates the parameter with matching name to a mesh layer.
- auto parameterAsOutputLayer(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QString
- Evaluates the parameter with matching name to a output layer destination.
- auto parameterAsPoint(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context, const QgsCoordinateReferenceSystem& crs = QgsCoordinateReferenceSystem()) const -> QgsPointXY
- Evaluates the parameter with matching name to a point.
- auto parameterAsPointCrs(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) -> QgsCoordinateReferenceSystem
- Returns the coordinate reference system associated with an point parameter value.
- auto parameterAsRange(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QList<double>
- Evaluates the parameter with matching name to a range of values.
- auto parameterAsRasterLayer(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QgsRasterLayer*
- Evaluates the parameter with matching name to a raster layer.
-
auto parameterAsSink(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context,
QString& destinationIdentifier,
const QgsFields& fields,
QgsWkbTypes::
Type geometryType = QgsWkbTypes:: NoGeometry, const QgsCoordinateReferenceSystem& crs = QgsCoordinateReferenceSystem(), QgsFeatureSink::SinkFlags sinkFlags = nullptr) const -> QgsFeatureSink* - Evaluates the parameter with matching name to a feature sink.
- auto parameterAsSource(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QgsProcessingFeatureSource*
- Evaluates the parameter with matching name to a feature source.
- auto parameterAsString(const QVariantMap& parameters, const QString& name, const QgsProcessingContext& context) const -> QString
- Evaluates the parameter with matching name to a static string value.
- auto parameterAsVectorLayer(const QVariantMap& parameters, const QString& name, QgsProcessingContext& context) const -> QgsVectorLayer*
- Evaluates the parameter with matching name to a vector layer.
- auto postProcessAlgorithm(QgsProcessingContext& context, QgsProcessingFeedback* feedback) -> QVariantMap virtual
- Allows the algorithm to perform any required cleanup tasks.
- auto prepareAlgorithm(const QVariantMap& parameters, QgsProcessingContext& context, QgsProcessingFeedback* feedback) -> bool virtual
- Prepares the algorithm to run using the specified parameters.
- auto processAlgorithm(const QVariantMap& parameters, QgsProcessingContext& context, QgsProcessingFeedback* feedback) -> QVariantMap pure virtual
- Runs the algorithm using the specified parameters.
- void removeParameter(const QString& name)
- Removes the parameter with matching name from the algorithm, and deletes any existing definition.
- auto supportInPlaceEdit(const QgsMapLayer* layer) const -> bool virtual
- Checks whether this algorithm supports in-place editing on the given layer Default implementation returns false.
Enum documentation
enum QgsProcessingAlgorithm:: Flag
Flags indicating how and when an algorithm operates and should be exposed to users.
| Enumerators | |
|---|---|
| FlagHideFromToolbox |
Algorithm should be hidden from the toolbox. |
| FlagHideFromModeler |
Algorithm should be hidden from the modeler. |
| FlagSupportsBatch |
Algorithm supports batch mode. |
| FlagCanCancel |
Algorithm can be canceled. |
| FlagRequiresMatchingCrs |
Algorithm requires that all input layers have matching coordinate reference systems. |
| FlagNoThreading |
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which manipulate the current project, layer selections, or with external dependencies which are not thread-safe. |
| FlagDisplayNameIsLiteral |
Algorithm's display name is a static literal string, and should not be translated or automatically formatted. For use with algorithms named after commands, e.g. GRASS 'v.in.ogr'. |
| FlagSupportsInPlaceEdits |
Algorithm supports in-place editing. |
| FlagDeprecated |
Algorithm is deprecated. |
Function documentation
QgsProcessingAlgorithm:: QgsProcessingAlgorithm() defaulted
Constructor for QgsProcessingAlgorithm.
initAlgorithm() should be called after creating an algorithm to ensure it can correctly configure its parameterDefinitions() and outputDefinitions(). Alternatively, calling create() will return a pre-initialized copy of the algorithm.
QString QgsProcessingAlgorithm:: asPythonCommand(const QVariantMap& parameters,
QgsProcessingContext& context) const virtual
Returns a Python command string which can be executed to run the algorithm using the specified parameters.
Algorithms which cannot be run from a Python command should return an empty string.
bool QgsProcessingAlgorithm:: canExecute(QString* errorMessage = nullptr) const virtual
Returns true if the algorithm can execute.
Algorithm subclasses can return false here to indicate that they are not able to execute, e.g. as a result of unmet external dependencies. If specified, the errorMessage argument will be filled with a localised error message describing why the algorithm cannot execute.
bool QgsProcessingAlgorithm:: checkParameterValues(const QVariantMap& parameters,
QgsProcessingContext& context,
QString* message = nullptr) const virtual
Checks the supplied parameter values to verify that they satisfy the requirements of this algorithm in the supplied context.
| Returns | true if parameters are acceptable for the algorithm. |
|---|
The message parameter will be filled with explanatory text if validation fails. Overridden implementations should also check this base class implementation.
QgsProcessingAlgorithm* QgsProcessingAlgorithm:: create(const QVariantMap& configuration = QVariantMap()) const
Creates a copy of the algorithm, ready for execution.
This method returns a new, preinitialized copy of the algorithm, ready for executing.
The configuration argument allows passing of a map of configuration settings to the algorithm, allowing it to dynamically adjust its initialized parameters and outputs according to this configuration. This is generally used only for algorithms in a model, allowing them to adjust their behavior at run time according to some user configuration.
QWidget* QgsProcessingAlgorithm:: createCustomParametersWidget(QWidget* parent = nullptr) const virtual
If an algorithm subclass implements a custom parameters widget, a copy of this widget should be constructed and returned by this method.
The base class implementation returns nullptr, which indicates that an autogenerated parameters widget should be used.
QgsExpressionContext QgsProcessingAlgorithm:: createExpressionContext(const QVariantMap& parameters,
QgsProcessingContext& context,
QgsProcessingFeatureSource* source = nullptr) const
Creates an expression context relating to the algorithm.
This can be called by algorithms to create a new expression context ready for evaluating expressions within the algorithm. Optionally, a source can be specified which will be used to populate the context if it implements the QgsExpressionContextGenerator interface.
QgsProcessingParameterDefinitions QgsProcessingAlgorithm:: destinationParameterDefinitions() const
Returns a list of destination parameters definitions utilized by the algorithm.
QString QgsProcessingAlgorithm:: displayName() const pure virtual
Returns the translated algorithm name, which should be used for any user-visible display of the algorithm name.
Algorithm display names should be short, e.g. ideally no more than 3 or 4 words. The name should use sentence case (e.g. "Raster layer statistics", not "Raster Layer Statistics").
Flags QgsProcessingAlgorithm:: flags() const virtual
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Default flags are FlagSupportsBatch and FlagCanCancel.
QString QgsProcessingAlgorithm:: group() const virtual
Returns the name of the group this algorithm belongs to.
This string should be localised.
QString QgsProcessingAlgorithm:: groupId() const virtual
Returns the unique ID of the group this algorithm belongs to.
This string should be fixed for the algorithm, and must not be localised. The group id should be unique within each provider. Group id should contain lowercase alphanumeric characters only and no spaces or other formatting characters.
Q_DECL_DEPRECATED QString QgsProcessingAlgorithm:: helpString() const virtual
Returns a localised help string for the algorithm.
Algorithm subclasses should implement either helpString() or helpUrl().
QString QgsProcessingAlgorithm:: helpUrl() const virtual
Returns a url pointing to the algorithm's help page.
QIcon QgsProcessingAlgorithm:: icon() const virtual
Returns an icon for the algorithm.
QString QgsProcessingAlgorithm:: id() const
Returns the unique ID for the algorithm, which is a combination of the algorithm provider's ID and the algorithms unique name (e.g.
"qgis:mergelayers" ).
QString QgsProcessingAlgorithm:: name() const pure virtual
Returns the algorithm name, used for identifying the algorithm.
This string should be fixed for the algorithm, and must not be localised. The name should be unique within each provider. Names should contain lowercase alphanumeric characters only and no spaces or other formatting characters.
const QgsProcessingOutputDefinition* QgsProcessingAlgorithm:: outputDefinition(const QString& name) const
Returns a matching output by name.
Matching is done in a case-insensitive manner.
QgsProcessingOutputDefinitions QgsProcessingAlgorithm:: outputDefinitions() const
Returns an ordered list of output definitions utilized by the algorithm.
const QgsProcessingParameterDefinition* QgsProcessingAlgorithm:: parameterDefinition(const QString& name) const
Returns a matching parameter by name.
Matching is done in a case-insensitive manner, but exact case matches will be preferred.
QgsProcessingParameterDefinitions QgsProcessingAlgorithm:: parameterDefinitions() const
Returns an ordered list of parameter definitions utilized by the algorithm.
QVariantMap QgsProcessingAlgorithm:: postProcess(QgsProcessingContext& context,
QgsProcessingFeedback* feedback)
Should be called in the main thread following the completion of runPrepared().
This method allows the algorithm to perform any required cleanup tasks. The returned variant map includes the results evaluated by the algorithm.
bool QgsProcessingAlgorithm:: prepare(const QVariantMap& parameters,
QgsProcessingContext& context,
QgsProcessingFeedback* feedback)
Prepares the algorithm for execution.
This must be run in the main thread, and allows the algorithm to pre-evaluate input parameters in a thread-safe manner. This must be called before calling runPrepared() (which is safe to do in any thread).
QVariantMap QgsProcessingAlgorithm:: preprocessParameters(const QVariantMap& parameters) virtual
Pre-processes a set of parameters, allowing the algorithm to clean their values.
This method is automatically called after users enter parameters, e.g. via the algorithm dialog. This method should NOT be called manually by algorithms.
QVariantMap QgsProcessingAlgorithm:: run(const QVariantMap& parameters,
QgsProcessingContext& context,
QgsProcessingFeedback* feedback,
bool* ok = nullptr,
const QVariantMap& configuration = QVariantMap()) const
Executes the algorithm using the specified parameters.
| Returns | A map of algorithm outputs. These may be output layer references, or calculated values such as statistical calculations. |
|---|
This method internally creates a copy of the algorithm before running it, so it is safe to call on algorithms directly retrieved from QgsProcessingRegistry and QgsProcessingProvider.
The context argument specifies the context in which the algorithm is being run.
Algorithm progress should be reported using the supplied feedback object.
If specified, ok will be set to true if algorithm was successfully run.
QVariantMap QgsProcessingAlgorithm:: runPrepared(const QVariantMap& parameters,
QgsProcessingContext& context,
QgsProcessingFeedback* feedback)
Runs the algorithm, which has been prepared by an earlier call to prepare().
This method is safe to call from any thread. Returns true if the algorithm was successfully executed. After runPrepared() has finished, the postProcess() method should be called from the main thread to allow the algorithm to perform any required cleanup tasks and return its final result.
void QgsProcessingAlgorithm:: setProvider(QgsProcessingProvider* provider)
Associates this algorithm with its provider.
No transfer of ownership is involved.
QString QgsProcessingAlgorithm:: shortDescription() const virtual
Returns an optional translated short description of the algorithm.
This should be at most a single sentence, e.g. "Converts 2D features to 3D by sampling a DEM raster."
QString QgsProcessingAlgorithm:: shortHelpString() const virtual
Returns a localised short helper string for the algorithm.
This string should provide a basic description about what the algorithm does and the parameters and outputs associated with it.
QString QgsProcessingAlgorithm:: svgIconPath() const virtual
Returns a path to an SVG version of the algorithm's icon.
QStringList QgsProcessingAlgorithm:: tags() const virtual
Returns a list of tags which relate to the algorithm, and are used to assist users in searching for suitable algorithms.
These tags should be localised.
bool QgsProcessingAlgorithm:: validateInputCrs(const QVariantMap& parameters,
QgsProcessingContext& context) const virtual
Checks whether the coordinate reference systems for the specified set of parameters are valid for the algorithm.
For instance, the base implementation performs checks to ensure that all input CRS are equal Returns true if parameters have passed the CRS check.
static QString QgsProcessingAlgorithm:: invalidRasterError(const QVariantMap& parameters,
const QString& name) protected
Returns a user-friendly string to use as an error when a raster layer input could not be loaded.
The parameters argument should give the algorithms parameter map, and the name should correspond to the invalid source parameter name.
static QString QgsProcessingAlgorithm:: invalidSinkError(const QVariantMap& parameters,
const QString& name) protected
Returns a user-friendly string to use as an error when a sink parameter could not be created.
The parameters argument should give the algorithms parameter map, and the name should correspond to the invalid source parameter name.
static QString QgsProcessingAlgorithm:: invalidSourceError(const QVariantMap& parameters,
const QString& name) protected
Returns a user-friendly string to use as an error when a source parameter could not be loaded.
The parameters argument should give the algorithms parameter map, and the name should correspond to the invalid source parameter name.
bool QgsProcessingAlgorithm:: addOutput(QgsProcessingOutputDefinition* outputDefinition) protected
Adds an output definition to the algorithm.
Ownership of the definition is transferred to the algorithm. Returns true if the output could be successfully added, or false if the output could not be added (e.g. as a result of a duplicate name).
This should usually be called from a subclass' initAlgorithm() implementation.
Note that in some cases output creation can be automatically performed when calling addParameter(). See the notes in addParameter() for a description of when this occurs.
bool QgsProcessingAlgorithm:: addParameter(QgsProcessingParameterDefinition* parameterDefinition,
bool createOutput = true) protected
Adds a parameter definition to the algorithm.
Ownership of the definition is transferred to the algorithm. Returns true if parameter could be successfully added, or false if the parameter could not be added (e.g. as a result of a duplicate name).
This should usually be called from a subclass' initAlgorithm() implementation.
If the createOutput argument is true, then a corresponding output definition will also be created (and added to the algorithm) where appropriate. E.g. when adding a QgsProcessingParameterVectorDestination and createOutput is true, then a QgsProcessingOutputVectorLayer output will be created and added to the algorithm. There is no need to call addOutput() to manually add a corresponding output for this vector. If createOutput is false then this automatic output creation will not occur.
QgsProcessingAlgorithm* QgsProcessingAlgorithm:: createInstance() const pure virtual protected
Creates a new instance of the algorithm class.
This method should return a 'pristine' instance of the algorithm class.
void QgsProcessingAlgorithm:: initAlgorithm(const QVariantMap& configuration = QVariantMap()) pure virtual protected
Initializes the algorithm using the specified configuration.
This should be called directly after creating algorithms and before retrieving any parameterDefinitions() or outputDefinitions().
Subclasses should use their implementations to add all required input parameter and output definitions (which can be dynamically adjusted according to configuration).
Dynamic configuration can be used by algorithms which alter their behavior when used inside processing models. For instance, a "feature router" type algorithm which sends input features to one of any number of outputs sinks based on some preconfigured filter parameters can use the init method to create these outputs based on the specified configuration.
QString QgsProcessingAlgorithm:: parameterAsCompatibleSourceLayerPath(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context,
const QStringList& compatibleFormats,
const QString& preferredFormat = QString("shp"),
QgsProcessingFeedback* feedback = nullptr) protected
Evaluates the parameter with matching name to a source vector layer file path of compatible format.
If the parameter is evaluated to an existing layer, and that layer is not of the format listed in the compatibleFormats argument, then the layer will first be exported to a compatible format in a temporary location. The function will then return the path to that temporary file.
compatibleFormats should consist entirely of lowercase file extensions, e.g. 'shp'.
The preferredFormat argument is used to specify to desired file extension to use when a temporary layer export is required.
QgsRectangle QgsProcessingAlgorithm:: parameterAsExtent(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context,
const QgsCoordinateReferenceSystem& crs = QgsCoordinateReferenceSystem()) const protected
Evaluates the parameter with matching name to a rectangular extent.
If crs is set, and the original coordinate reference system of the parameter can be determined, then the extent will be automatically reprojected so that it is in the specified crs. In this case the extent of the reproject rectangle will be returned.
QgsCoordinateReferenceSystem QgsProcessingAlgorithm:: parameterAsExtentCrs(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context) protected
Returns the coordinate reference system associated with an extent parameter value.
QgsGeometry QgsProcessingAlgorithm:: parameterAsExtentGeometry(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context,
const QgsCoordinateReferenceSystem& crs = QgsCoordinateReferenceSystem()) protected
Evaluates the parameter with matching name to a rectangular extent, and returns a geometry covering this extent.
If crs is set, and the original coordinate reference system of the parameter can be determined, then the extent will be automatically reprojected so that it is in the specified crs. Unlike parameterAsExtent(), the reprojected rectangle returned by this function will no longer be a rectangle itself (i.e. this method returns the geometry of the actual reprojected rectangle, while parameterAsExtent() returns just the extent of the reprojected rectangle).
QList<int> QgsProcessingAlgorithm:: parameterAsInts(const QVariantMap& parameters,
const QString& name,
const QgsProcessingContext& context) const protected
Evaluates the parameter with matching name to a list of integer values.
QgsMapLayer* QgsProcessingAlgorithm:: parameterAsLayer(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context) const protected
Evaluates the parameter with matching name to a map layer.
Layers will either be taken from context's active project, or loaded from external sources and stored temporarily in the context. In either case, callers do not need to handle deletion of the returned layer.
QVariantList QgsProcessingAlgorithm:: parameterAsMatrix(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context) const protected
Evaluates the parameter with matching name to a matrix/table of values.
Tables are collapsed to a 1 dimensional list.
QgsMeshLayer* QgsProcessingAlgorithm:: parameterAsMeshLayer(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context) const protected
Evaluates the parameter with matching name to a mesh layer.
Layers will either be taken from context's active project, or loaded from external sources and stored temporarily in the context. In either case, callers do not need to handle deletion of the returned layer.
QgsPointXY QgsProcessingAlgorithm:: parameterAsPoint(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context,
const QgsCoordinateReferenceSystem& crs = QgsCoordinateReferenceSystem()) const protected
Evaluates the parameter with matching name to a point.
If crs is set then the point will be automatically reprojected so that it is in the specified crs.
QgsCoordinateReferenceSystem QgsProcessingAlgorithm:: parameterAsPointCrs(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context) protected
Returns the coordinate reference system associated with an point parameter value.
QgsRasterLayer* QgsProcessingAlgorithm:: parameterAsRasterLayer(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context) const protected
Evaluates the parameter with matching name to a raster layer.
Layers will either be taken from context's active project, or loaded from external sources and stored temporarily in the context. In either case, callers do not need to handle deletion of the returned layer.
QgsFeatureSink* QgsProcessingAlgorithm:: parameterAsSink(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context,
QString& destinationIdentifier,
const QgsFields& fields,
QgsWkbTypes:: Type geometryType = QgsWkbTypes:: NoGeometry,
const QgsCoordinateReferenceSystem& crs = QgsCoordinateReferenceSystem(),
QgsFeatureSink::SinkFlags sinkFlags = nullptr) const protected
Evaluates the parameter with matching name to a feature sink.
Sinks will either be taken from context's active project, or created from external providers and stored temporarily in the context.
The fields, geometryType and crs parameters dictate the properties of the resulting feature sink.
The destinationIdentifier argument will be set to a string which can be used to retrieve the layer corresponding to the sink, e.g. via calling QgsProcessingUtils::
This function creates a new object and the caller takes responsibility for deleting the returned object.
QgsProcessingFeatureSource* QgsProcessingAlgorithm:: parameterAsSource(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context) const protected
Evaluates the parameter with matching name to a feature source.
Sources will either be taken from context's active project, or loaded from external sources and stored temporarily in the context.
This function creates a new object and the caller takes responsibility for deleting the returned object.
QgsVectorLayer* QgsProcessingAlgorithm:: parameterAsVectorLayer(const QVariantMap& parameters,
const QString& name,
QgsProcessingContext& context) const protected
Evaluates the parameter with matching name to a vector layer.
Layers will either be taken from context's active project, or loaded from external sources and stored temporarily in the context. In either case, callers do not need to handle deletion of the returned layer.
QVariantMap QgsProcessingAlgorithm:: postProcessAlgorithm(QgsProcessingContext& context,
QgsProcessingFeedback* feedback) virtual protected
Allows the algorithm to perform any required cleanup tasks.
| Returns | A map of algorithm outputs. These may be output layer references, or calculated values such as statistical calculations. Implementations which return a non-empty map will override any results returned by processAlgorithm(). |
|---|
The returned variant map includes the results evaluated by the algorithm. These may be output layer references, or calculated values such as statistical calculations.
The context argument specifies the context in which the algorithm was run.
Postprocess progress should be reported using the supplied feedback object. Additionally, well-behaved algorithms should periodically check feedback to determine whether the post processing should be canceled and exited early.
postProcessAlgorithm should be used to handle any thread-sensitive cleanup which is required by the algorithm. It will always be called from the same thread that context has thread affinity with. While this will generally be the main thread, it is not guaranteed. For instance, algorithms which are run as a step in a larger model or as a subcomponent of a script-based algorithm will call postProcessAlgorithm from the same thread as that model/script it being executed in.
postProcessAlgorithm will not be called if the prepareAlgorithm() step failed (returned false), or if an exception was raised by the processAlgorithm() step.
bool QgsProcessingAlgorithm:: prepareAlgorithm(const QVariantMap& parameters,
QgsProcessingContext& context,
QgsProcessingFeedback* feedback) virtual protected
Prepares the algorithm to run using the specified parameters.
| Returns | true if preparation was successful. |
|---|
Algorithms should implement their logic for evaluating parameter values here. The evaluated parameter results should be stored in member variables ready for a call to processAlgorithm().
The context argument specifies the context in which the algorithm is being run.
prepareAlgorithm should be used to handle any thread-sensitive preparation which is required by the algorithm. It will always be called from the same thread that context has thread affinity with. While this will generally be the main thread, it is not guaranteed. For instance, algorithms which are run as a step in a larger model or as a subcomponent of a script-based algorithm will call prepareAlgorithm from the same thread as that model/script it being executed in.
Note that the processAlgorithm step uses a temporary context with affinity for the thread in which the algorithm is executed, making it safe for processAlgorithm implementations to load sources and sinks without issue. Implementing prepareAlgorithm is only required if special thread safe handling is required by the algorithm.
Algorithm preparation progress should be reported using the supplied feedback object. Additionally, well-behaved algorithms should periodically check feedback to determine whether the algorithm should be canceled and exited early.
If the preparation was successful algorithms must return true. If a false value is returned this indicates that the preparation could not be completed, and the algorithm execution will be canceled.
QVariantMap QgsProcessingAlgorithm:: processAlgorithm(const QVariantMap& parameters,
QgsProcessingContext& context,
QgsProcessingFeedback* feedback) pure virtual protected
Runs the algorithm using the specified parameters.
| Returns | A map of algorithm outputs. These may be output layer references, or calculated values such as statistical calculations. Unless the algorithm subclass overrides the postProcessAlgorithm() step this returned map will be used as the output for the algorithm. |
|---|
Algorithms should implement their custom processing logic here.
The context argument gives a temporary context with thread affinity matching the thread in which the algorithm is being run. This is a cut-back copy of the context passed to the prepareAlgorithm() and postProcessAlgorithm() steps, but it is generally safe for most algorithms to utilize this context for loading layers and creating sinks. Any loaded layers or sinks created within this temporary context will be transferred back to the main execution context upon successful completion of the processAlgorithm() step.
Algorithm progress should be reported using the supplied feedback object. Additionally, well-behaved algorithms should periodically check feedback to determine whether the algorithm should be canceled and exited early.
This method will not be called if the prepareAlgorithm() step failed (returned false).
c++ implementations of processAlgorithm can throw the QgsProcessingException exception to indicate that a fatal error occurred within the execution. Python based subclasses should raise GeoAlgorithmExecutionException for the same purpose.
bool QgsProcessingAlgorithm:: supportInPlaceEdit(const QgsMapLayer* layer) const virtual protected
Checks whether this algorithm supports in-place editing on the given layer Default implementation returns false.
| Returns | true if the algorithm supports in-place editing |
|---|