Class: QgsProcessingAlgorithm

class qgis.core.QgsProcessingAlgorithm

Bases: sip.wrapper

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.

Abstract base class for processing algorithms.

Methods

addOutput

Adds an output definition to the algorithm.

addParameter

Adds a parameter definition to the algorithm.

asPythonCommand

Returns a Python command string which can be executed to run the algorithm using the specified parameters.

canExecute

Returns True if the algorithm can execute.

checkParameterValues

Checks the supplied parameter values to verify that they satisfy the requirements of this algorithm in the supplied context.

countVisibleParameters

Returns the number of visible (non-hidden) parameters defined by this algorithm.

create

Creates a copy of the algorithm, ready for execution.

createCustomParametersWidget

If an algorithm subclass implements a custom parameters widget, a copy of this widget should be constructed and returned by this method.

createExpressionContext

Creates an expression context relating to the algorithm.

createInstance

Creates a new instance of the algorithm class.

destinationParameterDefinitions

Returns a list of destination parameters definitions utilized by the algorithm.

displayName

Returns the translated algorithm name, which should be used for any user-visible display of the algorithm name.

flags

Returns the flags indicating how and when the algorithm operates and should be exposed to users.

group

Returns the name of the group this algorithm belongs to.

groupId

Returns the unique ID of the group this algorithm belongs to.

hasHtmlOutputs

Returns True if this algorithm generates HTML outputs.

helpString

Returns a localised help string for the algorithm.

helpUrl

Returns a url pointing to the algorithm’s help page.

icon

Returns an icon for the algorithm.

id

Returns the unique ID for the algorithm, which is a combination of the algorithm provider’s ID and the algorithms unique name (e.g.

initAlgorithm

Initializes the algorithm using the specified configuration.

invalidRasterError

Returns a user-friendly string to use as an error when a raster layer input could not be loaded.

invalidSinkError

Returns a user-friendly string to use as an error when a sink parameter could not be created.

invalidSourceError

Returns a user-friendly string to use as an error when a source parameter could not be loaded.

name

Returns the algorithm name, used for identifying the algorithm.

outputDefinition

Returns a matching output by name.

outputDefinitions

Returns an ordered list of output definitions utilized by the algorithm.

parameterAsBool

Evaluates the parameter with matching name to a static boolean value.

parameterAsBoolean

Evaluates the parameter with matching name to a static boolean value.

parameterAsColor

Evaluates the parameter with matching name to a color, or returns an invalid color if the parameter was not set.

parameterAsCompatibleSourceLayerPath

Evaluates the parameter with matching name to a source vector layer file path of compatible format.

parameterAsCompatibleSourceLayerPathAndLayerName

Evaluates the parameter with matching name to a source vector layer file path and layer name of compatible format.

parameterAsCrs

Evaluates the parameter with matching name to a coordinate reference system.

parameterAsDouble

Evaluates the parameter with matching name to a static double value.

parameterAsEnum

Evaluates the parameter with matching name to a enum value.

parameterAsEnums

Evaluates the parameter with matching name to list of enum values.

parameterAsExpression

Evaluates the parameter with matching name to an expression.

parameterAsExtent

Evaluates the parameter with matching name to a rectangular extent.

parameterAsExtentCrs

Returns the coordinate reference system associated with an extent parameter value.

parameterAsExtentGeometry

Evaluates the parameter with matching name to a rectangular extent, and returns a geometry covering this extent.

parameterAsFields

Evaluates the parameter with matching name to a list of fields.

parameterAsFile

Evaluates the parameter with matching name to a file/folder name.

parameterAsFileList

Evaluates the parameter with matching name to a list of files (for QgsProcessingParameterMultipleLayers in QgsProcessing:TypeFile mode).

parameterAsFileOutput

Evaluates the parameter with matching name to a file based output destination.

parameterAsInt

Evaluates the parameter with matching name to a static integer value.

parameterAsInts

Evaluates the parameter with matching name to a list of integer values.

parameterAsLayer

Evaluates the parameter with matching name to a map layer.

parameterAsLayerList

Evaluates the parameter with matching name to a list of map layers.

parameterAsLayout

Evaluates the parameter with matching name to a print layout.

parameterAsLayoutItem

Evaluates the parameter with matching name to a print layout item, taken from the specified layout.

parameterAsMatrix

Evaluates the parameter with matching name to a matrix/table of values.

parameterAsMeshLayer

Evaluates the parameter with matching name to a mesh layer.

parameterAsOutputLayer

Evaluates the parameter with matching name to a output layer destination.

parameterAsPoint

Evaluates the parameter with matching name to a point.

parameterAsPointCrs

Returns the coordinate reference system associated with an point parameter value.

parameterAsRange

Evaluates the parameter with matching name to a range of values.

parameterAsRasterLayer

Evaluates the parameter with matching name to a raster layer.

parameterAsSink

Evaluates the parameter with matching name to a feature sink.

parameterAsSource

Evaluates the parameter with matching name to a feature source.

parameterAsString

Evaluates the parameter with matching name to a static string value.

parameterAsVectorLayer

Evaluates the parameter with matching name to a vector layer.

parameterDefinition

Returns a matching parameter by name.

parameterDefinitions

Returns an ordered list of parameter definitions utilized by the algorithm.

postProcess

Should be called in the main thread following the completion of runPrepared().

postProcessAlgorithm

Allows the algorithm to perform any required cleanup tasks.

prepare

Prepares the algorithm for execution.

prepareAlgorithm

Prepares the algorithm to run using the specified parameters.

preprocessParameters

Pre-processes a set of parameters, allowing the algorithm to clean their values.

processAlgorithm

Runs the algorithm using the specified parameters.

provider

Returns the provider to which this algorithm belongs.

removeParameter

Removes the parameter with matching name from the algorithm, and deletes any existing definition.

run

Executes the algorithm using the specified parameters.

runPrepared

Runs the algorithm, which has been prepared by an earlier call to prepare().

setProvider

Associates this algorithm with its provider.

shortDescription

Returns an optional translated short description of the algorithm.

shortHelpString

Returns a localised short helper string for the algorithm.

supportInPlaceEdit

Checks whether this algorithm supports in-place editing on the given layer Default implementation returns False.

svgIconPath

Returns a path to an SVG version of the algorithm’s icon.

tags

Returns a list of tags which relate to the algorithm, and are used to assist users in searching for suitable algorithms.

validateInputCrs

Checks whether the coordinate reference systems for the specified set of parameters are valid for the algorithm.

Attributes

FlagCanCancel

FlagDeprecated

FlagDisplayNameIsLiteral

FlagHideFromModeler

FlagHideFromToolbox

FlagKnownIssues

FlagNoThreading

FlagRequiresMatchingCrs

FlagSupportsBatch

FlagSupportsInPlaceEdits

class Flag

Bases: int

FlagCanCancel = 16
FlagDeprecated = 6
FlagDisplayNameIsLiteral = 128
FlagHideFromModeler = 4
FlagHideFromToolbox = 2
FlagKnownIssues = 512
FlagNoThreading = 64
FlagRequiresMatchingCrs = 32
FlagSupportsBatch = 8
FlagSupportsInPlaceEdits = 256
class Flags

Bases: sip.wrapper

QgsProcessingAlgorithm.Flags(Union[QgsProcessingAlgorithm.Flags, QgsProcessingAlgorithm.Flag]) QgsProcessingAlgorithm.Flags(QgsProcessingAlgorithm.Flags)

addOutput(self, outputDefinition: QgsProcessingOutputDefinition) → bool

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.

See also

addParameter()

See also

initAlgorithm()

Parameters

outputDefinition (QgsProcessingOutputDefinition) –

Return type

bool

addParameter(self, parameterDefinition: QgsProcessingParameterDefinition, createOutput: bool = True) → bool

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.

See also

initAlgorithm()

See also

addOutput()

Parameters
Return type

bool

asPythonCommand(self, parameters: Dict[str, Any], context: QgsProcessingContext) → str

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.

Parameters
Return type

str

canExecute(self) → Tuple[bool, str]

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.

Return type

Tuple[bool, str]

checkParameterValues(self, parameters: Dict[str, Any], context: QgsProcessingContext) → Tuple[bool, str]

Checks the supplied parameter values to verify that they satisfy the requirements of this algorithm in the supplied context. The message parameter will be filled with explanatory text if validation fails. Overridden implementations should also check this base class implementation.

Return type

Tuple[bool, str]

Returns

True if parameters are acceptable for the algorithm.

Parameters
countVisibleParameters(self) → int

Returns the number of visible (non-hidden) parameters defined by this algorithm.

Return type

int

create(self, configuration: Dict[str, Any] = {})QgsProcessingAlgorithm

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.

Raises a QgsProcessingException if a new algorithm instance could not be created, e.g. if there is an issue with the subclass’ createInstance() method.

See also

initAlgorithm()

Parameters

configuration (Dict[str) –

Return type

QgsProcessingAlgorithm

createCustomParametersWidget(self, parent: QWidget = None) → QWidget

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 None, which indicates that an autogenerated parameters widget should be used.

Parameters

parent (QWidget = None) –

Return type

QWidget

createExpressionContext(self, parameters: Dict[str, Any], context: QgsProcessingContext, source: QgsProcessingFeatureSource = None)QgsExpressionContext

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.

Parameters
Return type

QgsExpressionContext

createInstance(self)QgsProcessingAlgorithm

Creates a new instance of the algorithm class.

This method should return a ‘pristine’ instance of the algorithm class.

Return type

QgsProcessingAlgorithm

destinationParameterDefinitions(self) → List[QgsProcessingParameterDefinition]

Returns a list of destination parameters definitions utilized by the algorithm.

Return type

List[QgsProcessingParameterDefinition]

displayName(self) → str

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”).

See also

name()

Return type

str

flags(self) → QgsProcessingAlgorithm.Flags

Returns the flags indicating how and when the algorithm operates and should be exposed to users. Default flags are FlagSupportsBatch and FlagCanCancel.

Return type

QgsProcessingAlgorithm.Flags

group(self) → str

Returns the name of the group this algorithm belongs to. This string should be localised.

See also

groupId()

See also

tags()

Return type

str

groupId(self) → str

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.

See also

group()

Return type

str

hasHtmlOutputs(self) → bool

Returns True if this algorithm generates HTML outputs.

Return type

bool

helpString(self) → str

Returns a localised help string for the algorithm. Algorithm subclasses should implement either helpString() or helpUrl().

See also

helpUrl()

Deprecated since version Unused,: will be removed in QGIS 4.0

Return type

str

helpUrl(self) → str

Returns a url pointing to the algorithm’s help page.

See also

helpString()

Return type

str

icon(self) → QIcon

Returns an icon for the algorithm.

See also

svgIconPath()

Return type

QIcon

id(self) → str

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” ).

See also

name()

See also

provider()

Return type

str

initAlgorithm(self, configuration: Dict[str, Any] = {})

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.

See also

addParameter()

See also

addOutput()

Parameters

configuration (Dict[str) –

invalidRasterError(parameters: Dict[str, Any], name: str) → str

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.

New in version 3.2.

Parameters
  • parameters (Dict[str) –

  • name (str) –

Return type

str

invalidSinkError(parameters: Dict[str, Any], name: str) → str

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.

New in version 3.2.

Parameters
  • parameters (Dict[str) –

  • name (str) –

Return type

str

invalidSourceError(parameters: Dict[str, Any], name: str) → str

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.

New in version 3.2.

Parameters
  • parameters (Dict[str) –

  • name (str) –

Return type

str

name(self) → str

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.

See also

displayName()

See also

group()

See also

tags()

Return type

str

outputDefinition(self, name: str)QgsProcessingOutputDefinition

Returns a matching output by name. Matching is done in a case-insensitive manner.

Parameters

name (str) –

Return type

QgsProcessingOutputDefinition

outputDefinitions(self) → List[QgsProcessingOutputDefinition]

Returns an ordered list of output definitions utilized by the algorithm.

See also

addOutput()

Return type

List[QgsProcessingOutputDefinition]

parameterAsBool(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → bool

Evaluates the parameter with matching name to a static boolean value.

Parameters
Return type

bool

parameterAsBoolean(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → bool

Evaluates the parameter with matching name to a static boolean value.

New in version 3.8.

Parameters
Return type

bool

parameterAsColor(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → QColor

Evaluates the parameter with matching name to a color, or returns an invalid color if the parameter was not set.

New in version 3.10.

Parameters
Return type

QColor

parameterAsCompatibleSourceLayerPath(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, compatibleFormats: Iterable[str], preferredFormat: str = '', feedback: QgsProcessingFeedback = None) → str

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.

When an algorithm is capable of handling multi-layer input files (such as Geopackage), it is preferable to use parameterAsCompatibleSourceLayerPathAndLayerName() which may avoid conversion in more situations.

Parameters
Return type

str

parameterAsCompatibleSourceLayerPathAndLayerName(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, compatibleFormats: Iterable[str], preferredFormat: str = '', feedback: QgsProcessingFeedback = None) → Tuple[str, str]

Evaluates the parameter with matching name to a source vector layer file path and layer name 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. This defaults to shapefiles, because shapefiles are the future (don’t believe the geopackage hype!).

This method should be preferred over parameterAsCompatibleSourceLayerPath() when an algorithm is able to correctly handle files with multiple layers. Unlike parameterAsCompatibleSourceLayerPath(), it will not force a conversion in this case and will return the target layer name in the layerName argument.

Parameters
  • parameters (Dict[str) – input parameter value map

  • name (str) – name of target parameter

  • context (QgsProcessingContext) – processing context

  • compatibleFormats (Iterable[str]) – a list of lowercase file extensions compatible with the algorithm

  • preferredFormat (str = '') – preferred format extension to use if conversion if required

  • feedback (QgsProcessingFeedback = None) – feedback object

Return type

Tuple[str, str]

Returns

  • path to source layer, or nearly converted compatible layer

  • layerName: will be set to the target layer name for multi-layer sources (e.g. Geopackage)

New in version 3.10.

parameterAsCrs(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext)QgsCoordinateReferenceSystem

Evaluates the parameter with matching name to a coordinate reference system.

Parameters
Return type

QgsCoordinateReferenceSystem

parameterAsDouble(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → float

Evaluates the parameter with matching name to a static double value.

Parameters
Return type

float

parameterAsEnum(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → int

Evaluates the parameter with matching name to a enum value.

Parameters
Return type

int

parameterAsEnums(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → List[int]

Evaluates the parameter with matching name to list of enum values.

Parameters
Return type

List[int]

parameterAsExpression(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → str

Evaluates the parameter with matching name to an expression.

Parameters
Return type

str

parameterAsExtent(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem())QgsRectangle

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.

Parameters
Return type

QgsRectangle

parameterAsExtentCrs(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext)QgsCoordinateReferenceSystem

Returns the coordinate reference system associated with an extent parameter value.

Parameters
Return type

QgsCoordinateReferenceSystem

parameterAsExtentGeometry(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem())QgsGeometry

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).

Parameters
Return type

QgsGeometry

parameterAsFields(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → List[str]

Evaluates the parameter with matching name to a list of fields.

Parameters
Return type

List[str]

parameterAsFile(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → str

Evaluates the parameter with matching name to a file/folder name.

Parameters
Return type

str

parameterAsFileList(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → List[str]

Evaluates the parameter with matching name to a list of files (for QgsProcessingParameterMultipleLayers in QgsProcessing:TypeFile mode).

New in version 3.10.

Parameters
Return type

List[str]

parameterAsFileOutput(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → str

Evaluates the parameter with matching name to a file based output destination.

Parameters
Return type

str

parameterAsInt(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → int

Evaluates the parameter with matching name to a static integer value.

Parameters
Return type

int

parameterAsInts(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → List[int]

Evaluates the parameter with matching name to a list of integer values.

New in version 3.4.

Parameters
Return type

List[int]

parameterAsLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext)QgsMapLayer

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.

Parameters
Return type

QgsMapLayer

parameterAsLayerList(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → List[QgsMapLayer]

Evaluates the parameter with matching name to a list of map layers.

Parameters
Return type

List[QgsMapLayer]

parameterAsLayout(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext)QgsPrintLayout

Evaluates the parameter with matching name to a print layout.

Warning

This method is not safe to run in a background thread, so it must either be used within a prepareAlgorithm implementation (which runs in the main thread), or the algorithm must return the FlagNoThreading flag.

New in version 3.8.

Parameters
Return type

QgsPrintLayout

parameterAsLayoutItem(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, layout: QgsPrintLayout)QgsLayoutItem

Evaluates the parameter with matching name to a print layout item, taken from the specified layout.

Warning

This method is not safe to run in a background thread, so it must either be used within a prepareAlgorithm implementation (which runs in the main thread), or the algorithm must return the FlagNoThreading flag.

New in version 3.8.

Parameters
Return type

QgsLayoutItem

parameterAsMatrix(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → List[Any]

Evaluates the parameter with matching name to a matrix/table of values. Tables are collapsed to a 1 dimensional list.

Parameters
Return type

List[Any]

parameterAsMeshLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext)QgsMeshLayer

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.

New in version 3.6.

Parameters
Return type

QgsMeshLayer

parameterAsOutputLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → str

Evaluates the parameter with matching name to a output layer destination.

Parameters
Return type

str

parameterAsPoint(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem())QgsPointXY

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.

Parameters
Return type

QgsPointXY

parameterAsPointCrs(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext)QgsCoordinateReferenceSystem

Returns the coordinate reference system associated with an point parameter value.

Parameters
Return type

QgsCoordinateReferenceSystem

parameterAsRange(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → List[float]

Evaluates the parameter with matching name to a range of values.

Parameters
Return type

List[float]

parameterAsRasterLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext)QgsRasterLayer

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.

Parameters
Return type

QgsRasterLayer

parameterAsSink(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, fields: QgsFields, geometryType: QgsWkbTypes.Type = QgsWkbTypes.NoGeometry, crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem(), sinkFlags: Union[QgsFeatureSink.SinkFlags, QgsFeatureSink.SinkFlag] = 0) → Tuple[QgsFeatureSink, str]

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.mapLayerFromString()

This function creates a new object and the caller takes responsibility for deleting the returned object.

Raises

: – py:class:QgsProcessingException

Parameters
Return type

Tuple[QgsFeatureSink, str]

parameterAsSource(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext)QgsProcessingFeatureSource

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.

Parameters
Return type

QgsProcessingFeatureSource

parameterAsString(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) → str

Evaluates the parameter with matching name to a static string value.

Parameters
Return type

str

parameterAsVectorLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext)QgsVectorLayer

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.

Parameters
Return type

QgsVectorLayer

parameterDefinition(self, name: str)QgsProcessingParameterDefinition

Returns a matching parameter by name. Matching is done in a case-insensitive manner, but exact case matches will be preferred.

Parameters

name (str) –

Return type

QgsProcessingParameterDefinition

parameterDefinitions(self) → List[QgsProcessingParameterDefinition]

Returns an ordered list of parameter definitions utilized by the algorithm.

See also

addParameter()

Return type

List[QgsProcessingParameterDefinition]

postProcess(self, context: QgsProcessingContext, feedback: QgsProcessingFeedback) → Dict[str, Any]

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.

Note

This method modifies the algorithm instance, so it is not safe to call on algorithms directly retrieved from QgsProcessingRegistry and QgsProcessingProvider. Instead, a copy of the algorithm should be created with clone() and prepare()/runPrepared() called on the copy.

Parameters
Return type

Dict[str, Any]

postProcessAlgorithm(self, context: QgsProcessingContext, feedback: QgsProcessingFeedback) → Dict[str, Any]

Allows the algorithm to perform any required cleanup tasks. 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.

Return type

Dict[str, Any]

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().

Parameters
prepare(self, parameters: Dict[str, Any], context: QgsProcessingContext, feedback: QgsProcessingFeedback) → bool

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).

See also

runPrepared()

See also

postProcess()

Note

This method modifies the algorithm instance, so it is not safe to call on algorithms directly retrieved from QgsProcessingRegistry and QgsProcessingProvider. Instead, a copy of the algorithm should be created with clone() and prepare()/runPrepared() called on the copy.

Parameters
Return type

bool

prepareAlgorithm(self, parameters: Dict[str, Any], context: QgsProcessingContext, feedback: QgsProcessingFeedback) → bool

Prepares the algorithm to run using the specified parameters. 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.

Return type

bool

Returns

True if preparation was successful.

Parameters
preprocessParameters(self, parameters: Dict[str, Any]) → Dict[str, Any]

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.

Parameters

parameters (Dict[str) –

Return type

Dict[str, Any]

processAlgorithm(self, parameters: Dict[str, Any], context: QgsProcessingContext, feedback: QgsProcessingFeedback) → Dict[str, Any]

Runs the algorithm using the specified parameters. 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).

Implementations of processAlgorithm can throw the QgsProcessingException exception to indicate that a fatal error occurred within the execution.

Return type

Dict[str, Any]

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.

Parameters
provider(self)QgsProcessingProvider

Returns the provider to which this algorithm belongs.

Return type

QgsProcessingProvider

removeParameter(self, name: str)

Removes the parameter with matching name from the algorithm, and deletes any existing definition.

Parameters

name (str) –

run(self, parameters: Dict[str, Any], context: QgsProcessingContext, feedback: QgsProcessingFeedback, configuration: Dict[str, Any] = {}) → Tuple[Dict[str, Any], bool]

Executes the algorithm using the specified parameters. 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.

Return type

Tuple[Dict[str, Any], bool]

Returns

A map of algorithm outputs. These may be output layer references, or calculated values such as statistical calculations.

Note

this method can only be called from the main thread. Use prepare(), runPrepared() and postProcess() if you need to run algorithms from a background thread, or use the QgsProcessingAlgRunnerTask class.

Parameters
runPrepared(self, parameters: Dict[str, Any], context: QgsProcessingContext, feedback: QgsProcessingFeedback) → Dict[str, Any]

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.

See also

prepare()

See also

postProcess()

Note

This method modifies the algorithm instance, so it is not safe to call on algorithms directly retrieved from QgsProcessingRegistry and QgsProcessingProvider. Instead, a copy of the algorithm should be created with clone() and prepare()/runPrepared() called on the copy.

Parameters
Return type

Dict[str, Any]

setProvider(self, provider: QgsProcessingProvider)

Associates this algorithm with its provider. No transfer of ownership is involved.

Parameters

provider (QgsProcessingProvider) –

shortDescription(self) → str

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.”

New in version 3.2.

Return type

str

shortHelpString(self) → str

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.

See also

helpString()

See also

helpUrl()

Return type

str

supportInPlaceEdit(self, layer: QgsMapLayer) → bool

Checks whether this algorithm supports in-place editing on the given layer Default implementation returns False.

Return type

bool

Returns

True if the algorithm supports in-place editing

New in version 3.4.

Parameters

layer (QgsMapLayer) –

svgIconPath(self) → str

Returns a path to an SVG version of the algorithm’s icon.

See also

icon()

Return type

str

tags(self) → List[str]

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.

Return type

List[str]

validateInputCrs(self, parameters: Dict[str, Any], context: QgsProcessingContext) → bool

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.

Parameters
Return type

bool