Class: QgsProcessingContext¶
- class qgis.core.QgsProcessingContext¶
Bases:
sip.wrapper
Contains information about the context in which a processing algorithm is executed.
Contextual information includes settings such as the associated project, and expression context.
New in version 3.0.
QgsProcessingContext() Constructor for QgsProcessingContext.
Enums
Bases:
enum.IntEnum
Methods
Adds a
layer
to load (by ID or datasource) into the canvas upon completion of the algorithm or model.Returns the area unit to use for area calculations.
Returns list of the equivalent qgis_process arguments representing the settings from the context.
Copies all settings which are safe for use across different threads from
other
to this context.Returns the current time range to use for temporal operations.
Returns the default encoding to use for newly created files.
Returns the distance unit to use for distance calculations.
Returns the ellipsoid to use for distance and area calculations.
Exports the context's settings to a variant map.
Returns the expression context.
Returns the associated feedback object.
Returns any flags set in the context.
Returns a map layer from the context with a matching
identifier
.Returns the behavior used for checking invalid geometries in input layers.
Returns a reference to the details for a given
layer
which is loaded on completion of the algorithm or model.Returns a map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completion of the algorithm or model.
Returns the logging level for algorithms to use when pushing feedback messages to users.
Returns the (optional) number of threads to use when running algorithms.
Returns the preferred raster format to use for vector outputs.
Returns the preferred vector format to use for vector outputs.
Returns the project in which the algorithm is being executed.
Pushes the thread affinity for the context (including all layers contained in the
temporaryLayerStore()
) into anotherthread
.Sets the
unit
to use for area calculations.Sets the
current
time range to use for temporal operations.Sets the default
encoding
to use for newly created files.Sets the
unit
to use for distance calculations.Sets a specified
ellipsoid
to use for distance and area calculations.Sets the expression
context
.Sets an associated
feedback
object.Sets
flags
for the context.Sets a callback function to use when encountering an invalid geometry and
invalidGeometryCheck()
is set to GeometryAbortOnInvalid.Sets the behavior used for checking invalid geometries in input layers.
Sets the map of
layers
(by ID or datasource) to LayerDetails, to load into the canvas upon completion of the algorithm or model.Sets the logging
level
for algorithms to use when pushing feedback messages to users.Sets the (optional) number of
threads
to use when running algorithms.Sets the preferred raster
format
to use for vector outputs.Sets the preferred vector
format
to use for vector outputs.Sets the
project
in which the algorithm will be executed.Sets the (optional) temporary
folder
to use when running algorithms.Sets the coordinate transform
context
.Sets a callback function to use when encountering a transform error when iterating features.
Takes the result map layer with matching
id
from the context and transfers ownership of it back to the caller.Takes the results from another
context
and merges them with the results currently stored in this context.Returns the (optional) temporary folder to use when running algorithms.
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
Returns the thread in which the context lives.
Returns the coordinate transform context.
Returns
True
if the givenlayer
(by ID or datasource) will be loaded into the current project upon completion of the algorithm or model.Attributes
- DefaultLevel = 0¶
- class Flag¶
Bases:
int
- class Flags¶
- class Flags(Union[QgsProcessingContext.Flags, QgsProcessingContext.Flag])
- class Flags(QgsProcessingContext.Flags)
Bases:
sip.wrapper
- class LayerDetails¶
Bases:
sip.wrapper
Details for layers to load into projects.
New in version 3.0.
QgsProcessingContext.LayerDetails(name: str, project:
QgsProject
, outputName: str = ‘’, layerTypeHint: QgsProcessingUtils.LayerHint = QgsProcessingUtils.LayerHint.UnknownType) Constructor for LayerDetails.QgsProcessingContext.LayerDetails() Default constructor
QgsProcessingContext.LayerDetails(QgsProcessingContext.LayerDetails)
- forceName¶
- groupName¶
- layerSortKey¶
- layerTypeHint¶
- name¶
- outputName¶
- postProcessor(self) QgsProcessingLayerPostProcessorInterface ¶
Layer post-processor. May be
None
if no post-processing is required.See also
New in version 3.2.
- Return type:
- 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
New in version 3.2.
- Parameters:
processor (QgsProcessingLayerPostProcessorInterface) –
- class LogLevel¶
Bases:
int
- class ProcessArgumentFlag(value)¶
Bases:
enum.IntEnum
Flags controlling the results given by
asQgisProcessArguments()
.New in version 3.24.
IncludeProjectPath
: Include the associated project path argument
- IncludeProjectPath = 1¶
- class ProcessArgumentFlags¶
- class ProcessArgumentFlags(Union[QgsProcessingContext.ProcessArgumentFlags, QgsProcessingContext.ProcessArgumentFlag])
- class ProcessArgumentFlags(QgsProcessingContext.ProcessArgumentFlags)
Bases:
sip.wrapper
- Unused = 1¶
- Verbose = 1¶
- 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. Thedetails
parameter dictates the LayerDetails.See also
See also
See also
See also
- Parameters:
layer (str) –
details (QgsProcessingContext.LayerDetails) –
- areaUnit(self) Qgis.AreaUnit ¶
Returns the area unit to use for area calculations.
See also
See also
New in version 3.16.
- Return type:
- asQgisProcessArguments(self, flags: QgsProcessingContext.ProcessArgumentFlags | QgsProcessingContext.ProcessArgumentFlag = QgsProcessingContext.ProcessArgumentFlags()) List[str] ¶
Returns list of the equivalent qgis_process arguments representing the settings from the context.
New in version 3.24.
- Parameters:
flags (Union[QgsProcessingContext.ProcessArgumentFlags) –
- Return type:
List[str]
- copyThreadSafeSettings(self, other: QgsProcessingContext)¶
Copies all settings which are safe for use across different threads from
other
to this context.- Parameters:
other (QgsProcessingContext) –
- currentTimeRange(self) QgsDateTimeRange ¶
Returns the current time range to use for temporal operations.
See also
New in version 3.18.
- Return type:
- defaultEncoding(self) str ¶
Returns the default encoding to use for newly created files.
See also
- Return type:
str
- distanceUnit(self) Qgis.DistanceUnit ¶
Returns the distance unit to use for distance calculations.
See also
See also
New in version 3.16.
- Return type:
- ellipsoid(self) str ¶
Returns the ellipsoid to use for distance and area calculations.
See also
New in version 3.16.
- Return type:
str
- exportToMap(self) Dict[str, Any] ¶
Exports the context’s settings to a variant map.
New in version 3.24.
- Return type:
Dict[str, Any]
- expressionContext(self) QgsExpressionContext ¶
Returns the expression context.
- Return type:
- feedback(self) QgsProcessingFeedback ¶
Returns the associated feedback object.
See also
- Return type:
- flags(self) QgsProcessingContext.Flags ¶
Returns any flags set in the context.
See also
- Return type:
- 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 theidentifier
(seeQgsProcessingUtils.mapLayerFromString()
for details).Ownership is not transferred and remains with the context.
See also
- Parameters:
identifier (str) –
- Return type:
- invalidGeometryCheck(self) QgsFeatureRequest.InvalidGeometryCheck ¶
Returns the behavior used for checking invalid geometries in input layers.
See also
- Return type:
- 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 addlayer
as a layer to load on completion.See also
See also
See also
See also
New in version 3.2.
- Parameters:
layer (str) –
- Return type:
- 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.
See also
See also
See also
See also
- Return type:
Dict[str, QgsProcessingContext.LayerDetails]
- logLevel(self) QgsProcessingContext.LogLevel ¶
Returns the logging level for algorithms to use when pushing feedback messages to users.
See also
New in version 3.20.
- Return type:
- maximumThreads(self) int ¶
Returns the (optional) number of threads to use when running algorithms.
Warning
Not all algorithms which support multithreaded execution will respect this setting, depending on the multi-threading framework in use. Multithreaded algorithms must check this value and adapt their thread handling accordingly – the setting will not be automatically applied.
See also
New in version 3.32.
- Return type:
int
- 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.
See also
See also
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.
See also
See also
New in version 3.10.
- Return type:
str
- project(self) QgsProject ¶
Returns the project in which the algorithm is being executed.
See also
- Return type:
- pushToThread(self, thread: QThread)¶
Pushes the thread affinity for the context (including all layers contained in the
temporaryLayerStore()
) into anotherthread
. This method is only safe to call when the current thread matches the existing thread affinity for the context (seethread()
).See also
- Parameters:
thread (QThread) –
- setAreaUnit(self, areaUnit: Qgis.AreaUnit)¶
Sets the
unit
to use for area calculations.If not explicitly set, the unit will default to the
project()
’s area unit setting.See also
See also
New in version 3.16.
- Parameters:
areaUnit (Qgis.AreaUnit) –
- setCurrentTimeRange(self, currentTimeRange: QgsDateTimeRange)¶
Sets the
current
time range to use for temporal operations.See also
New in version 3.18.
- Parameters:
currentTimeRange (QgsDateTimeRange) –
- setDefaultEncoding(self, encoding: str)¶
Sets the default
encoding
to use for newly created files.See also
- Parameters:
encoding (str) –
- setDistanceUnit(self, unit: Qgis.DistanceUnit)¶
Sets the
unit
to use for distance calculations.If not explicitly set, the unit will default to the
project()
’s distance unit setting.See also
See also
New in version 3.16.
- Parameters:
unit (Qgis.DistanceUnit) –
- setEllipsoid(self, ellipsoid: str)¶
Sets a specified
ellipsoid
to use for distance and area calculations.If not explicitly set, the ellipsoid will default to the
project()
’s ellipsoid setting.See also
New in version 3.16.
- Parameters:
ellipsoid (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 tofeedback
is stored and no errors will occur if feedback is deleted before the context. Ownership offeedback
is not transferred.See also
- Parameters:
feedback (QgsProcessingFeedback) –
- setFlags(self, flags: QgsProcessingContext.Flags | QgsProcessingContext.Flag)¶
Sets
flags
for the context.See also
- 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 theinvalidGeometryCallback()
to a default implementation.See also
- 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.See also
See also
See also
See also
- Parameters:
layers (Dict[str) –
- setLogLevel(self, level: QgsProcessingContext.LogLevel)¶
Sets the logging
level
for algorithms to use when pushing feedback messages to users.See also
New in version 3.20.
- Parameters:
level (QgsProcessingContext.LogLevel) –
- setMaximumThreads(self, threads: int)¶
Sets the (optional) number of
threads
to use when running algorithms.If set, this overrides the standard global Processing number of threads setting. Note that if algorithm implementation does not support multhreaded execution, this setting will be ignored.
Warning
Not all algorithms which support multithreaded execution will respect this setting, depending on the multi-threading framework in use. Multithreaded algorithms must check this value and adapt their thread handling accordingly – the setting will not be automatically applied.
See also
New in version 3.32.
- Parameters:
threads (int) –
- 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.See also
See also
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.See also
See also
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()
,ellipsoid()
,distanceUnit()
andareaUnit()
to match the project’s settings.See also
- Parameters:
project (QgsProject) –
- setTemporaryFolder(self, folder: str)¶
Sets the (optional) temporary
folder
to use when running algorithms.If set, this overrides the standard global Processing temporary folder and should be used for all temporary files created during algorithm execution.
See also
New in version 3.32.
- Parameters:
folder (str) –
- setTransformContext(self, context: QgsCoordinateTransformContext)¶
Sets the coordinate transform
context
.Note that setting a project for the context will automatically set the coordinate transform context.
See also
- 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
- Parameters:
id (str) –
- Return type:
- 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 thetemporaryLayerStore()
andlayersToLoadOnCompletion()
. This is only safe to call when both this context and the othercontext
share the samethread()
affinity, and that thread is the current thread.- Parameters:
context (QgsProcessingContext) –
- temporaryFolder(self) str ¶
Returns the (optional) temporary folder to use when running algorithms.
If set, this overrides the standard global Processing temporary folder and should be used for all temporary files created during algorithm execution.
See also
New in version 3.32.
- Return type:
str
- temporaryLayerStore(self) QgsMapLayerStore ¶
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
- Return type:
- thread(self) QThread ¶
Returns the thread in which the context lives.
See also
- Return type:
QThread
- transformContext(self) QgsCoordinateTransformContext ¶
Returns the coordinate transform context.
See also
- Return type:
- willLoadLayerOnCompletion(self, layer: str) bool ¶
Returns
True
if the givenlayer
(by ID or datasource) will be loaded into the current project upon completion of the algorithm or model.See also
See also
See also
See also
New in version 3.2.
- Parameters:
layer (str) –
- Return type:
bool