Class: QgsProcessingModelAlgorithm

class qgis.core.QgsProcessingModelAlgorithm

Bases: QgsProcessingAlgorithm

Model based algorithm with processing.

New in version 3.0.

QgsProcessingModelAlgorithm(name: str = ‘’, group: str = ‘’, groupId: str = ‘’) Constructor for QgsProcessingModelAlgorithm.

Methods

activateChildAlgorithm

Attempts to activate the child algorithm with matching id.

addChildAlgorithm

Adds a new child algorithm to the model.

addGroupBox

Adds a new group box to the model.

addModelParameter

Adds a new parameter to the model, with the specified definition and graphical component.

addOutput

Adds an output definition to the algorithm.

addParameter

Adds a parameter definition to the algorithm.

asPythonCode

Attempts to convert the model to executable Python code, and returns the generated lines of code.

asPythonCommand

param parameters:

availableDependenciesForChildAlgorithm

Returns details of available dependencies for the child algorithm with matching id.

availableSourcesForChild

Returns a list of possible sources which can be used for the parameters for a child algorithm in the model.

canExecute

rtype:

Tuple[bool, str]

changeParameterName

Changes a model parameter's internal name from oldName to newName.

childAlgorithm

Returns the child algorithm with matching id.

childAlgorithms

Returns the map of child algorithms contained in the model.

childAlgorithmsDependOnParameter

Returns True if any child algorithms depend on the model parameter with the specified name.

createExpressionContext

param parameters:

createExpressionContextScopeForChildAlgorithm

Creates a new expression context scope for a child algorithm within the model.

createInstance

rtype:

QgsProcessingAlgorithm

deactivateChildAlgorithm

Deactivates the child algorithm with matching id.

dependentChildAlgorithms

Returns a list of the child algorithm IDs depending on the child algorithm with the specified childId.

dependsOnChildAlgorithms

Returns a list of the child algorithm IDs on which the child algorithm with the specified childId depends upon.

designerParameterValues

Returns the parameter values to use as default values when running this model through the designer dialog.

displayName

rtype:

str

fromFile

Reads the model from a file, at the specified path.

group

rtype:

str

groupBoxes

Returns a list of the group boxes within the model.

groupId

rtype:

str

helpContent

Returns the model's help contents (a free-form map of values describing the algorithm's use and metadata).

helpUrl

rtype:

str

icon

rtype:

QIcon

initAlgorithm

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

loadVariant

Loads this model from a QVariantMap, wrapped in a QVariant variant.

modelNameMatchesFilePath

Returns True if the model name matches the current model sourceFilePath().

modelParameterFromChildIdAndOutputName

Given a child algorithm ID and output name, attempts to match it to a parameter definition from the overall model.

name

rtype:

str

orderedParameters

Returns an ordered list of parameters for the model.

otherParametersDependOnParameter

Returns True if any other model parameters depend on the parameter with the specified name (e.g. field parameters where name is the parent layer parameter).

parameterAsAnnotationLayer

Evaluates the parameter with matching name to an annotation layer.

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.

parameterAsConnectionName

Evaluates the parameter with matching name to a connection name string.

parameterAsCrs

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

parameterAsDatabaseTableName

Evaluates the parameter with matching name to a database table name string.

parameterAsDateTime

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

parameterAsDouble

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

parameterAsEnum

Evaluates the parameter with matching name to a enum value.

parameterAsEnumString

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

parameterAsEnumStrings

Evaluates the parameter with matching name to list of static enum strings.

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.

parameterAsGeometry

Evaluates the parameter with matching name to a geometry.

parameterAsGeometryCrs

Returns the coordinate reference system associated with a geometry parameter value.

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.

parameterAsPointCloudLayer

Evaluates the parameter with matching name to a point cloud layer.

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.

parameterAsSchema

Evaluates the parameter with matching name to a database schema name string.

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.

parameterComponent

Returns the parameter component with matching name.

parameterComponents

Returns the map of parameter components used by the model.

postProcessAlgorithm

Allows the algorithm to perform any required cleanup tasks.

prepareAlgorithm

Prepares the algorithm to run using the specified parameters.

processAlgorithm

param parameters:

removeChildAlgorithm

Attempts to remove the child algorithm with matching id.

removeGroupBox

Removes the group box with matching uuid from the model.

removeModelParameter

Removes an existing model parameter by name.

removeParameter

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

safeName

Makes a name "safe", by replacing any non-alphanumeric characters with underscores.

setChildAlgorithm

Sets the child algorithm within the model.

setChildAlgorithms

Sets the map of child algorithms contained in the model.

setDesignerParameterValues

Sets the parameter values to use as default values when running this model through the designer dialog.

setGroup

Sets the model group.

setHelpContent

Sets the model's help contents (a free-form map of values describing the algorithm's use and metadata).

setName

Sets the model name.

setParameterComponent

Sets a parameter component for the model.

setParameterComponents

Sets the map of parameter components used by the model.

setParameterOrder

Sets the order for showing parameters for the model.

setSourceFilePath

Sets the source file path for the model, if available.

setVariables

Sets the map of custom expression context variables for this model.

shortDescription

rtype:

str

shortHelpString

rtype:

str

sourceFilePath

Returns the source file path for the model, if available.

supportInPlaceEdit

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

svgIconPath

rtype:

str

toFile

Writes the model to a file, at the specified path.

toVariant

Saves this model to a QVariantMap, wrapped in a QVariant.

updateDestinationParameters

Updates the model's parameter definitions to include all relevant destination parameters as required by child algorithm ModelOutputs.

updateModelParameter

Replaces the definition of an existing parameter (by parameter name) with a new definition.

validate

Validates the model, returning True if all child algorithms in the model are valid.

validateChildAlgorithm

Validates the child algorithm with matching ID, returning True if all mandatory inputs to the algorithm have valid values.

variables

Returns the map of custom expression context variables defined for this model.

variablesForChildAlgorithm

Returns a map of variable name to variable definition for expression context variables which are available for use by child algorithm during model execution.

writeFeatureError

Returns a user-friendly string to use as an error when a feature cannot be written into a sink.

Attributes

staticMetaObject

class VariableDefinition

Bases: sip.wrapper

Definition of a expression context variable available during model execution.

New in version 3.0.

QgsProcessingModelAlgorithm.VariableDefinition(value: Any = None, source: QgsProcessingModelChildParameterSource = QgsProcessingModelChildParameterSource.fromStaticValue(QVariant()), description: str = ‘’) Constructor for a new VariableDefinition with the specified value and original parameter source, and description.

QgsProcessingModelAlgorithm.VariableDefinition(QgsProcessingModelAlgorithm.VariableDefinition)

description
source
value
activateChildAlgorithm(self, id: str) bool

Attempts to activate the child algorithm with matching id. If any child algorithms on which the child depends are not active, then the child will not be activated and False will be returned.

Parameters:

id (str) –

Return type:

bool

addChildAlgorithm(self, algorithm: QgsProcessingModelChildAlgorithm) str

Adds a new child algorithm to the model. If a child algorithm already exists in the model with the same child ID then algorithm will be assigned a new autogenerated unique ID. The assigned child ID will be returned.

See also

childAlgorithm()

Parameters:

algorithm (QgsProcessingModelChildAlgorithm) –

Return type:

str

addGroupBox(self, groupBox: QgsProcessingModelGroupBox)

Adds a new group box to the model.

If an existing group box with the same uuid already exists then its definition will be replaced.

See also

groupBoxes()

New in version 3.14.

Parameters:

groupBox (QgsProcessingModelGroupBox) –

addModelParameter(self, definition: QgsProcessingParameterDefinition, component: QgsProcessingModelParameter)

Adds a new parameter to the model, with the specified definition and graphical component. Ownership of definition is transferred to the model.

Parameters:
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()

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

asPythonCode(self, outputType: QgsProcessing.PythonOutputType, indentSize: int) List[str]

Attempts to convert the model to executable Python code, and returns the generated lines of code.

The outputType argument dictates the desired script type.

The indentSize arguments specifies the size of indented lines.

Parameters:
Return type:

List[str]

asPythonCommand(self, parameters: Dict[str, Any], context: QgsProcessingContext) str
Parameters:
Return type:

str

availableDependenciesForChildAlgorithm(self, childId: str) List[QgsProcessingModelChildDependency]

Returns details of available dependencies for the child algorithm with matching id.

New in version 3.14.

Parameters:

childId (str) –

Return type:

List[QgsProcessingModelChildDependency]

availableSourcesForChild(self, childId: str, parameterTypes: Iterable[str] = [], outputTypes: Iterable[str] = [], dataTypes: Iterable[int] = []) List[QgsProcessingModelChildParameterSource]

Returns a list of possible sources which can be used for the parameters for a child algorithm in the model. Returned sources are those which match either one of the specified parameterTypes (see QgsProcessingParameterDefinition.type() ) or one of the specified outputTypes (see QgsProcessingOutputDefinition.type() ). If specified, an optional list of dataTypes can be used to filter the returned sources to those with compatible data types for the parameter/outputs.

Parameters:
  • childId (str) –

  • parameterTypes (Iterable[str] = []) –

  • outputTypes (Iterable[str] = []) –

  • dataTypes (Iterable[int] = []) –

Return type:

List[QgsProcessingModelChildParameterSource]

canExecute(self) Tuple[bool, str]
Return type:

Tuple[bool, str]

changeParameterName(self, oldName: str, newName: str)

Changes a model parameter’s internal name from oldName to newName.

This method will automatically update all model components to relink using the new name.

New in version 3.26.

Parameters:
  • oldName (str) –

  • newName (str) –

childAlgorithm(self, id: str) QgsProcessingModelChildAlgorithm

Returns the child algorithm with matching id. If no child algorithm exists with this ID a new algorithm will be added to the model and returned.

Parameters:

id (str) –

Return type:

QgsProcessingModelChildAlgorithm

childAlgorithms(self) Dict[str, QgsProcessingModelChildAlgorithm]

Returns the map of child algorithms contained in the model. The keys are the child algorithm ids (see QgsProcessingModelAlgorithm.ChildAlgorithm.childId()).

See also

childAlgorithm()

Return type:

Dict[str, QgsProcessingModelChildAlgorithm]

childAlgorithmsDependOnParameter(self, name: str) bool

Returns True if any child algorithms depend on the model parameter with the specified name.

Parameters:

name (str) –

Return type:

bool

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

QgsExpressionContext

createExpressionContextScopeForChildAlgorithm(self, childId: str, context: QgsProcessingContext, modelParameters: Dict[str, Any] = {}, results: Dict[str, Any] = {}) QgsExpressionContextScope

Creates a new expression context scope for a child algorithm within the model.

Parameters:
  • childId (str) –

  • context (QgsProcessingContext) –

  • modelParameters (Dict[str) –

  • results (Dict[str) –

Return type:

QgsExpressionContextScope

createInstance(self) QgsProcessingAlgorithm
Return type:

QgsProcessingAlgorithm

deactivateChildAlgorithm(self, id: str)

Deactivates the child algorithm with matching id. All other child algorithms which depend on the child algorithm will also be deactivated.

Parameters:

id (str) –

dependentChildAlgorithms(self, childId: str, conditionalBranch: str = '') Set[str]

Returns a list of the child algorithm IDs depending on the child algorithm with the specified childId.

Optionally, a specific conditional branch which is created by the child algorithm can be specified in order to restrict the returned list to algorithms which depend on this specific branch.

Parameters:
  • childId (str) –

  • conditionalBranch (str = '') –

Return type:

Set[str]

dependsOnChildAlgorithms(self, childId: str) Set[str]

Returns a list of the child algorithm IDs on which the child algorithm with the specified childId depends upon.

Parameters:

childId (str) –

Return type:

Set[str]

designerParameterValues(self) Dict[str, Any]

Returns the parameter values to use as default values when running this model through the designer dialog.

This usually corresponds to the last set of parameter values used when the model was run through the designer.

New in version 3.14.

Return type:

Dict[str, Any]

displayName(self) str
Return type:

str

fromFile(self, path: str) bool

Reads the model from a file, at the specified path.

See also

toFile()

Parameters:

path (str) –

Return type:

bool

group(self) str
Return type:

str

groupBoxes(self) List[QgsProcessingModelGroupBox]

Returns a list of the group boxes within the model.

See also

addGroupBox()

New in version 3.14.

Return type:

List[QgsProcessingModelGroupBox]

groupId(self) str
Return type:

str

helpContent(self) Dict[str, Any]

Returns the model’s help contents (a free-form map of values describing the algorithm’s use and metadata).

See also

setHelpContent()

Return type:

Dict[str, Any]

helpUrl(self) str
Return type:

str

icon(self) QIcon
Return type:

QIcon

initAlgorithm(self, configuration: Dict[str, Any] = {})
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.

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.

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.

loadVariant(self, variant: Any) bool

Loads this model from a QVariantMap, wrapped in a QVariant variant. You can use QgsXmlUtils.readVariant to load it from an XML document.

See also

toVariant()

New in version 3.4.

Parameters:

variant (Any) –

Return type:

bool

modelNameMatchesFilePath(self) bool

Returns True if the model name matches the current model sourceFilePath().

Specifically, this method will return true if the complete base name of sourceFilePath() is identical (case-insensitive) to the model name.

New in version 3.24.

Return type:

bool

modelParameterFromChildIdAndOutputName(self, childId: str, childOutputName: str) QgsProcessingParameterDefinition

Given a child algorithm ID and output name, attempts to match it to a parameter definition from the overall model.

New in version 3.26.

Parameters:
  • childId (str) –

  • childOutputName (str) –

Return type:

QgsProcessingParameterDefinition

name(self) str
Return type:

str

orderedParameters(self) List[QgsProcessingModelParameter]

Returns an ordered list of parameters for the model.

New in version 3.14.

Return type:

List[QgsProcessingModelParameter]

otherParametersDependOnParameter(self, name: str) bool

Returns True if any other model parameters depend on the parameter with the specified name (e.g. field parameters where name is the parent layer parameter).

Parameters:

name (str) –

Return type:

bool

parameterAsAnnotationLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsAnnotationLayer

Evaluates the parameter with matching name to an annotation layer.

Annotation layers will be taken from context’s active project. Callers do not need to handle deletion of the returned layer.

Warning

Working with annotation layers is generally not thread safe (unless the layers are from a QgsProject loaded directly in a background thread). Ensure your algorithm returns the QgsProcessingAlgorithm.FlagNoThreading flag or only accesses annotation layers from a prepareAlgorithm() or postProcessAlgorithm() step.

New in version 3.22.

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

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

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.

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.

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.

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 – input parameter value map

  • name – name of target parameter

  • context – processing context

  • compatibleFormats – a list of lowercase file extensions compatible with the algorithm

  • preferredFormat – preferred format extension to use if conversion if required

  • feedback – feedback object

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.

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

Evaluates the parameter with matching name to a connection name string.

New in version 3.14.

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

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

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

Evaluates the parameter with matching name to a database table name string.

New in version 3.14.

parameterAsDateTime(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QDateTime

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

New in version 3.14.

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

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

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

Evaluates the parameter with matching name to a enum value.

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

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

New in version 3.18.

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

Evaluates the parameter with matching name to list of static enum strings.

New in version 3.18.

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

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

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

Evaluates the parameter with matching name to an expression.

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.

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

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

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

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

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

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

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

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.

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

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

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

Evaluates the parameter with matching name to a geometry.

If crs is set then the geometry will be automatically reprojected so that it is in the specified crs.

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

Returns the coordinate reference system associated with a geometry parameter value.

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

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

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.

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.

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

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

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.

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.

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.

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.

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

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

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.

parameterAsPointCloudLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsPointCloudLayer

Evaluates the parameter with matching name to a point cloud 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.22.

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

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

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

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

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.

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

Evaluates the parameter with matching name to a database schema name string.

New in version 3.14.

parameterAsSink(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, fields: QgsFields, geometryType: QgsWkbTypes.Type = QgsWkbTypes.NoGeometry, crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem(), sinkFlags: QgsFeatureSink.SinkFlags | QgsFeatureSink.SinkFlag = QgsFeatureSink.SinkFlags(), createOptions: Dict[str, Any] = {}, datasourceOptions: Iterable[str] = [], layerOptions: Iterable[str] = []) 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().

The createOptions argument is used to pass on creation options such as layer name.

The datasourceOptions and layerOptions arguments is used to pass on GDAL-specific format driver options.

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

Raises:

QgsProcessingException

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.

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

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

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.

parameterComponent(self, name: str) QgsProcessingModelParameter

Returns the parameter component with matching name. If no parameter component exists with this name a new component will be added to the model and returned.

Parameters:

name (str) –

Return type:

QgsProcessingModelParameter

parameterComponents(self) Dict[str, QgsProcessingModelParameter]

Returns the map of parameter components used by the model. The keys should match the algorithm’s parameter names (see parameterDefinitions() ).

Return type:

Dict[str, QgsProcessingModelParameter]

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.

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

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.

Returns:

True if preparation was successful.

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

Dict[str, Any]

removeChildAlgorithm(self, id: str) bool

Attempts to remove the child algorithm with matching id. Returns True if the algorithm could be removed, or False if the algorithm could not be removed (e.g. due to other child algorithms depending on it).

Parameters:

id (str) –

Return type:

bool

removeGroupBox(self, uuid: str)

Removes the group box with matching uuid from the model.

See also

addGroupBox()

See also

groupBoxes()

New in version 3.14.

Parameters:

uuid (str) –

removeModelParameter(self, name: str)

Removes an existing model parameter by name. The definition of the matching parameter is deleted.

Parameters:

name (str) –

removeParameter(self, name: str)

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

safeName(name: str, capitalize: bool = False) str

Makes a name “safe”, by replacing any non-alphanumeric characters with underscores.

If capitalize is True then the string will be converted to a camel case string.

New in version 3.26.

Parameters:
  • name (str) –

  • capitalize (bool = False) –

Return type:

str

setChildAlgorithm(self, algorithm: QgsProcessingModelChildAlgorithm)

Sets the child algorithm within the model. If a child algorithm already exists in the model with the same child ID then that algorithm will be replaced.

Parameters:

algorithm (QgsProcessingModelChildAlgorithm) –

setChildAlgorithms(self, childAlgorithms: Dict[str, QgsProcessingModelChildAlgorithm])

Sets the map of child algorithms contained in the model. The keys are the child algorithm ids (see QgsProcessingModelAlgorithm.ChildAlgorithm.childId()). All existing child algorithms will be replaced.

See also

childAlgorithm()

Parameters:

childAlgorithms (Dict[str) –

setDesignerParameterValues(self, values: Dict[str, Any])

Sets the parameter values to use as default values when running this model through the designer dialog.

This usually corresponds to the last set of parameter values used when the model was run through the designer.

New in version 3.14.

Parameters:

values (Dict[str) –

setGroup(self, group: str)

Sets the model group.

See also

group()

Parameters:

group (str) –

setHelpContent(self, contents: Dict[str, Any])

Sets the model’s help contents (a free-form map of values describing the algorithm’s use and metadata).

See also

helpContent()

Parameters:

contents (Dict[str) –

setName(self, name: str)

Sets the model name.

See also

name()

Parameters:

name (str) –

setParameterComponent(self, component: QgsProcessingModelParameter)

Sets a parameter component for the model. If a parameter component already exists in the model with the same parameter name then that component will be replaced.

Parameters:

component (QgsProcessingModelParameter) –

setParameterComponents(self, parameterComponents: Dict[str, QgsProcessingModelParameter])

Sets the map of parameter components used by the model. The keys should match the algorithm’s parameter names (see parameterDefinitions() ). All existing parameter components will be replaced.

Parameters:

parameterComponents (Dict[str) –

setParameterOrder(self, order: Iterable[str])

Sets the order for showing parameters for the model.

The order list should consist of parameter names corresponding to existing model parameterComponents().

New in version 3.14.

Parameters:

order (Iterable[str]) –

setSourceFilePath(self, path: str)

Sets the source file path for the model, if available.

See also

sourceFilePath()

Parameters:

path (str) –

setVariables(self, variables: Dict[str, Any])

Sets the map of custom expression context variables for this model.

These variables are added to the model’s expression context scope, allowing for preset “constant” expression variables to be utilized within the model.

See also

variables()

New in version 3.8.

Parameters:

variables (Dict[str) –

shortDescription(self) str
Return type:

str

shortHelpString(self) str
Return type:

str

sourceFilePath(self) str

Returns the source file path for the model, if available.

Return type:

str

staticMetaObject = <PyQt5.QtCore.QMetaObject object>
supportInPlaceEdit(self, layer: QgsMapLayer) bool

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

New in version 3.4.

svgIconPath(self) str
Return type:

str

toFile(self, path: str) bool

Writes the model to a file, at the specified path.

See also

fromFile()

Parameters:

path (str) –

Return type:

bool

toVariant(self) Any

Saves this model to a QVariantMap, wrapped in a QVariant. You can use QgsXmlUtils.writeVariant to save it to an XML document.

See also

loadVariant()

New in version 3.4.

Return type:

Any

updateDestinationParameters(self)

Updates the model’s parameter definitions to include all relevant destination parameters as required by child algorithm ModelOutputs. Must be called whenever child algorithm ModelOutputs are altered.

updateModelParameter(self, definition: QgsProcessingParameterDefinition)

Replaces the definition of an existing parameter (by parameter name) with a new definition. Ownership of definition is transferred to the model, and any existing parameter is deleted.

Parameters:

definition (QgsProcessingParameterDefinition) –

validate(self) Tuple[bool, List[str]]

Validates the model, returning True if all child algorithms in the model are valid.

Return type:

Tuple[bool, List[str]]

Returns:

  • True if the child is valid

  • issues: will be set to a list of issues encountered during the validation

New in version 3.14.

validateChildAlgorithm(self, childId: str) Tuple[bool, List[str]]

Validates the child algorithm with matching ID, returning True if all mandatory inputs to the algorithm have valid values.

Parameters:

childId (str) – ID for child to validate

Return type:

Tuple[bool, List[str]]

Returns:

  • True if the child is valid

  • issues: will be set to a list of issues encountered during the validation

New in version 3.14.

variables(self) Dict[str, Any]

Returns the map of custom expression context variables defined for this model.

These variables are added to the model’s expression context scope, allowing for preset “constant” expression variables to be utilized within the model.

See also

setVariables()

New in version 3.8.

Return type:

Dict[str, Any]

variablesForChildAlgorithm(self, childId: str, context: QgsProcessingContext, modelParameters: Dict[str, Any] = {}, results: Dict[str, Any] = {}) Dict[str, QgsProcessingModelAlgorithm.VariableDefinition]

Returns a map of variable name to variable definition for expression context variables which are available for use by child algorithm during model execution.

The child algorithm childId and processing context are manadatory. If modelParameters and results are not specified, then only the variable names and sources will be returned, but all variable values will be null. This can be used to determine in advance which variables will be available for a specific child algorithm, e.g. for use in expression builder widgets.

In order to calculate the actual variable value, the input model modelParameters and already executed child algorithm results must be passed.

Parameters:
  • childId (str) –

  • context (QgsProcessingContext) –

  • modelParameters (Dict[str) –

  • results (Dict[str) –

Return type:

Dict[str, QgsProcessingModelAlgorithm.VariableDefinition]

writeFeatureError(sink: QgsFeatureSink, parameters: Dict[str, Any], name: str) str

Returns a user-friendly string to use as an error when a feature cannot be written into a sink.

The sink argument is the sink into which the feature cannot be written.

The parameters argument should give the algorithms parameter map, and the name should correspond to the sink parameter name.

New in version 3.22.