Class: QgsProcessingContext

class qgis.core.QgsProcessingContext

Bases: sip.wrapper

Constructor for QgsProcessingContext.

Contains information about the context in which a processing algorithm is executed.

Contextual information includes settings such as the associated project, and expression context.

Methods

addLayerToLoadOnCompletion

Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model.

copyThreadSafeSettings

Copies all settings which are safe for use across different threads from other to this context.

defaultEncoding

Returns the default encoding to use for newly created files.

expressionContext

Returns the expression context.

feedback

Returns the associated feedback object.

flags

Returns any flags set in the context.

getMapLayer

Returns a map layer from the context with a matching identifier.

invalidGeometryCheck

Returns the behavior used for checking invalid geometries in input layers.

layerToLoadOnCompletionDetails

Returns a reference to the details for a given layer which is loaded on completion of the algorithm or model.

layersToLoadOnCompletion

Returns a map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completion of the algorithm or model.

preferredRasterFormat

Returns the preferred raster format to use for vector outputs.

preferredVectorFormat

Returns the preferred vector format to use for vector outputs.

project

Returns the project in which the algorithm is being executed.

pushToThread

Pushes the thread affinity for the context (including all layers contained in the temporaryLayerStore()) into another thread.

setDefaultEncoding

Sets the default encoding to use for newly created files.

setExpressionContext

Sets the expression context.

setFeedback

Sets an associated feedback object.

setFlags

Sets flags for the context.

setInvalidGeometryCallback

Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid.

setInvalidGeometryCheck

Sets the behavior used for checking invalid geometries in input layers.

setLayersToLoadOnCompletion

Sets the map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completion of the algorithm or model.

setPreferredRasterFormat

Sets the preferred raster format to use for vector outputs.

setPreferredVectorFormat

Sets the preferred vector format to use for vector outputs.

setProject

Sets the project in which the algorithm will be executed.

setTransformContext

Sets the coordinate transform context.

setTransformErrorCallback

Sets a callback function to use when encountering a transform error when iterating features.

takeResultLayer

Takes the result map layer with matching id from the context and transfers ownership of it back to the caller.

takeResultsFrom

Takes the results from another context and merges them with the results currently stored in this context.

temporaryLayerStore

Returns a reference to the layer store used for storing temporary layers during algorithm execution.

thread

Returns the thread in which the context lives.

transformContext

Returns the coordinate transform context.

willLoadLayerOnCompletion

Returns True if the given layer (by ID or datasource) will be loaded into the current project upon completion of the algorithm or model.

class Flag

Bases: int

class Flags

Bases: sip.wrapper

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

class LayerDetails(name: str, project: QgsProject, outputName: str = '', layerTypeHint: QgsProcessingUtils.LayerHint = QgsProcessingUtils.LayerHint.UnknownType)

Bases: sip.wrapper

Constructor for LayerDetails.

QgsProcessingContext.LayerDetails() Default constructor

QgsProcessingContext.LayerDetails(QgsProcessingContext.LayerDetails)

Details for layers to load into projects.

New in version 3.0.

Parameters
  • name (str) –

  • project (QgsProject) –

  • outputName (str = '') –

  • layerTypeHint (QgsProcessingUtils.LayerHint = QgsProcessingUtils.LayerHint.UnknownType) –

layerTypeHint
name
outputName
postProcessor(self) → QgsProcessingLayerPostProcessorInterface

Layer post-processor. May be None if no post-processing is required.

New in version 3.2.

Return type

QgsProcessingLayerPostProcessorInterface

project
setOutputLayerName(self, layer: QgsMapLayer)

Sets a layer name to match this output, respecting any local user settings which affect this name.

New in version 3.10.1.

Parameters

layer (QgsMapLayer) –

setPostProcessor(self, processor: QgsProcessingLayerPostProcessorInterface)

Sets the layer post-processor. May be None if no post-processing is required.

Ownership of processor is transferred.

See also

postProcessor()

New in version 3.2.

Parameters

processor (QgsProcessingLayerPostProcessorInterface) –

addLayerToLoadOnCompletion(self, layer: str, details: QgsProcessingContext.LayerDetails)

Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model. The details parameter dictates the LayerDetails.

Parameters
copyThreadSafeSettings(self, other: QgsProcessingContext)

Copies all settings which are safe for use across different threads from other to this context.

Parameters

other (QgsProcessingContext) –

defaultEncoding(self) → str

Returns the default encoding to use for newly created files.

Return type

str

expressionContext(self) → QgsExpressionContext

Returns the expression context.

Return type

QgsExpressionContext

feedback(self) → QgsProcessingFeedback

Returns the associated feedback object.

See also

setFeedback()

Return type

QgsProcessingFeedback

flags(self) → QgsProcessingContext.Flags

Returns any flags set in the context.

See also

setFlags()

Return type

QgsProcessingContext.Flags

getMapLayer(self, identifier: str) → QgsMapLayer

Returns a map layer from the context with a matching identifier. This method considers layer IDs, names and sources when matching the identifier (see QgsProcessingUtils.mapLayerFromString() for details).

Ownership is not transferred and remains with the context.

Parameters

identifier (str) –

Return type

QgsMapLayer

invalidGeometryCheck(self) → QgsFeatureRequest.InvalidGeometryCheck

Returns the behavior used for checking invalid geometries in input layers.

Return type

QgsFeatureRequest.InvalidGeometryCheck

layerToLoadOnCompletionDetails(self, layer: str) → QgsProcessingContext.LayerDetails

Returns a reference to the details for a given layer which is loaded on completion of the algorithm or model.

Warning

First check willLoadLayerOnCompletion(), or calling this method will incorrectly add layer as a layer to load on completion.

New in version 3.2.

Parameters

layer (str) –

Return type

QgsProcessingContext.LayerDetails

layersToLoadOnCompletion(self) → Dict[str, QgsProcessingContext.LayerDetails]

Returns a map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completion of the algorithm or model.

Return type

Dict[str, QgsProcessingContext.LayerDetails]

preferredRasterFormat(self) → str

Returns the preferred raster format to use for vector outputs.

This method returns a file extension to use when creating raster outputs (e.g. “tif”). Generally, it is preferable to use the extension associated with a particular parameter, which can be retrieved through QgsProcessingDestinationParameter.defaultFileExtension() However, in some cases, a specific parameter may not be available to call this method on (e.g. for an algorithm which has only an output folder parameter and which creates multiple output layers in that folder). In this case, the format returned by this function should be used when creating these outputs.

It is the algorithm’s responsibility to check whether the returned format is acceptable for the algorithm, and to provide an appropriate fallback when the returned format is not usable.

New in version 3.10.

Return type

str

preferredVectorFormat(self) → str

Returns the preferred vector format to use for vector outputs.

This method returns a file extension to use when creating vector outputs (e.g. “shp”). Generally, it is preferable to use the extension associated with a particular parameter, which can be retrieved through QgsProcessingDestinationParameter.defaultFileExtension() However, in some cases, a specific parameter may not be available to call this method on (e.g. for an algorithm which has only an output folder parameter and which creates multiple output layers in that folder). In this case, the format returned by this function should be used when creating these outputs.

It is the algorithm’s responsibility to check whether the returned format is acceptable for the algorithm, and to provide an appropriate fallback when the returned format is not usable.

New in version 3.10.

Return type

str

project(self) → QgsProject

Returns the project in which the algorithm is being executed.

See also

setProject()

Return type

QgsProject

pushToThread(self, thread: QThread)

Pushes the thread affinity for the context (including all layers contained in the temporaryLayerStore()) into another thread. This method is only safe to call when the current thread matches the existing thread affinity for the context (see thread()).

See also

thread()

Parameters

thread (QThread) –

setDefaultEncoding(self, encoding: str)

Sets the default encoding to use for newly created files.

Parameters

encoding (str) –

setExpressionContext(self, context: QgsExpressionContext)

Sets the expression context.

Parameters

context (QgsExpressionContext) –

setFeedback(self, feedback: QgsProcessingFeedback)

Sets an associated feedback object. This allows context related functions to report feedback and errors to users and processing logs. While ideally this feedback object should outlive the context, only a weak pointer to feedback is stored and no errors will occur if feedback is deleted before the context. Ownership of feedback is not transferred.

See also

setFeedback()

Parameters

feedback (QgsProcessingFeedback) –

setFlags(self, flags: Union[QgsProcessingContext.Flags, QgsProcessingContext.Flag])

Sets flags for the context.

See also

flags()

Parameters

flags (Union[QgsProcessingContext.Flags) –

setInvalidGeometryCallback(self, Callable[..., None])

Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid. This function will be called using the feature with invalid geometry as a parameter.

See also

invalidGeometryCallback()

New in version 3.0.

setInvalidGeometryCheck(self, check: QgsFeatureRequest.InvalidGeometryCheck)

Sets the behavior used for checking invalid geometries in input layers. Settings this to anything but QgsFeatureRequest.GeometryNoCheck will also reset the invalidGeometryCallback() to a default implementation.

Parameters

check (QgsFeatureRequest.InvalidGeometryCheck) –

setLayersToLoadOnCompletion(self, layers: Dict[str, QgsProcessingContext.LayerDetails])

Sets the map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completion of the algorithm or model.

Parameters

layers (Dict[str) –

setPreferredRasterFormat(self, format: str)

Sets the preferred raster format to use for vector outputs.

This method sets a file extension to use when creating raster outputs (e.g. “tif”). Generally, it is preferable to use the extension associated with a particular parameter, which can be retrieved through QgsProcessingDestinationParameter.defaultFileExtension() However, in some cases, a specific parameter may not be available to call this method on (e.g. for an algorithm which has only an output folder parameter and which creates multiple output layers in that folder). In this case, the format set by this function will be used when creating these outputs.

New in version 3.10.

Parameters

format (str) –

setPreferredVectorFormat(self, format: str)

Sets the preferred vector format to use for vector outputs.

This method sets a file extension to use when creating vector outputs (e.g. “shp”). Generally, it is preferable to use the extension associated with a particular parameter, which can be retrieved through QgsProcessingDestinationParameter.defaultFileExtension() However, in some cases, a specific parameter may not be available to call this method on (e.g. for an algorithm which has only an output folder parameter and which creates multiple output layers in that folder). In this case, the format set by this function will be used when creating these outputs.

New in version 3.10.

Parameters

format (str) –

setProject(self, project: QgsProject)

Sets the project in which the algorithm will be executed.

This also automatically sets the transformContext() to match the project’s transform context.

See also

project()

Parameters

project (QgsProject) –

setTransformContext(self, context: QgsCoordinateTransformContext)

Sets the coordinate transform context.

Note that setting a project for the context will automatically set the coordinate transform context.

Parameters

context (QgsCoordinateTransformContext) –

setTransformErrorCallback(self, Callable[..., None])

Sets a callback function to use when encountering a transform error when iterating features. This function will be called using the feature which encountered the transform error as a parameter.

See also

transformErrorCallback()

New in version 3.0.

takeResultLayer(self, id: str) → QgsMapLayer

Takes the result map layer with matching id from the context and transfers ownership of it back to the caller. This method can be used to remove temporary layers which are not required for further processing from a context.

See also

getMapLayer()

Parameters

id (str) –

Return type

QgsMapLayer

takeResultsFrom(self, context: QgsProcessingContext)

Takes the results from another context and merges them with the results currently stored in this context. This includes settings like any layers loaded in the temporaryLayerStore() and layersToLoadOnCompletion(). This is only safe to call when both this context and the other context share the same thread() affinity, and that thread is the current thread.

Parameters

context (QgsProcessingContext) –

temporaryLayerStore(self) → QgsMapLayerStore

Returns a reference to the layer store used for storing temporary layers during algorithm execution.

Return type

QgsMapLayerStore

thread(self) → QThread

Returns the thread in which the context lives.

See also

pushToThread()

Return type

QThread

transformContext(self) → QgsCoordinateTransformContext

Returns the coordinate transform context.

Return type

QgsCoordinateTransformContext

willLoadLayerOnCompletion(self, layer: str) → bool

Returns True if the given layer (by ID or datasource) will be loaded into the current project upon completion of the algorithm or model.

New in version 3.2.

Parameters

layer (str) –

Return type

bool