Class: QgsProcessingUtils¶
Utility functions for use with processing classes.
Enums
Layer type hints. |
Static Methods
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner). |
|
Combines the extent of several map layers. |
|
Returns a list of annotation layers from a project which are compatible with the processing framework. |
|
Returns a list of map layers from a project which are compatible with the processing framework. |
|
Returns a list of mesh layers from a project which are compatible with the processing framework. |
|
Returns a list of plugin layers from a project which are compatible with the processing framework. |
|
Returns a list of point cloud layers from a project which are compatible with the processing framework. |
|
Returns a list of raster layers from a project which are compatible with the processing framework. |
|
Returns a list of tiled scene layers from a project which are compatible with the processing framework. |
|
Returns a list of vector layers from a project which are compatible with the processing framework. |
|
Returns a list of vector tile layers from a project which are compatible with the processing framework. |
|
Converts a source vector layer to a file path of a vector layer of compatible format. |
|
Converts a source vector layer to a file path and layer name of a vector layer of compatible format. |
|
Creates a feature sink ready for adding features. |
|
Decodes a provider key and layer uri from an encoded string, for use with |
|
Returns the default point cloud extension to use, in the absence of all other constraints (e.g. provider based support for extensions). |
|
Returns the default raster extension to use, in the absence of all other constraints (e.g. provider based support for extensions). |
|
Returns the default vector extension to use, in the absence of all other constraints (e.g. provider based support for extensions). |
|
Returns the default vector tile extension to use, in the absence of all other constraints (e.g. provider based support for extensions). |
|
Encodes a provider key and layer uri to a single string, for use with |
|
Returns a list of field indices parsed from the given list of field names. |
|
Returns a HTML formatted version of the help text encoded in a variant map for a specified algorithm. |
|
Converts an input parameter value for use in source iterating mode, where one individual sink is created per input feature. |
|
Returns a temporary filename for a given file, putting it into a temporary folder (creating that folder in the process), but not changing the basename. |
|
Returns a subset of fields based on the indices of desired fields. |
|
Returns a string representation of the source for a layer. |
|
Interprets a string as a map layer within the supplied context. |
|
Normalizes a layer source string for safe comparison across different operating system environments. |
|
Returns the index of the output matching name for a specified algorithm. |
|
Pre-processes a set of parameter values for the qgis_process command. |
|
Removes any raw pointer values from an input map, replacing them with appropriate string values where possible. |
|
Returns the default encoding. |
|
Converts a string to a Python string literal. |
|
Returns a session specific processing temporary folder for use in processing algorithms. |
|
Converts a variant value to a coordinate reference system. |
|
Converts a variant to a Python literal. |
|
Converts a variant value to a new feature source. |
- class qgis.core.QgsProcessingUtils[source]¶
Bases:
object
- class LayerHint(*values)¶
Bases:
IntEnum
Layer type hints.
Added in version 3.4.
UnknownType
: Unknown layer typeVector
: Vector layer typeRaster
: Raster layer typeMesh
: Mesh layer type, since QGIS 3.6PointCloud
: Point cloud layer type, since QGIS 3.22Annotation
: Annotation layer type, since QGIS 3.22VectorTile
: Vector tile layer type, since QGIS 3.32TiledScene
: Tiled scene layer type, since QGIS 3.34
- static combineFields(fieldsA: QgsFields, fieldsB: QgsFields, fieldsBPrefix: str | None = '') QgsFields [source]¶
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
Duplicate field names will be altered to “name_2”, “name_3”, etc, finding the first non-duplicate name.
Note
Some output file formats (e.g. shapefiles) have restrictions on the maximum length of field names, so be aware that the results of calling this method may be truncated when saving to these formats.
- static combineLayerExtents(layers: Iterable[QgsMapLayer], crs: QgsCoordinateReferenceSystem, context: QgsProcessingContext) QgsRectangle [source]¶
Combines the extent of several map
layers
. If specified, the targetcrs
will be used to transform the layer’s extent to the desired output reference system using the specifiedcontext
.Added in version 3.8.
- Parameters:
layers (Iterable[QgsMapLayer])
context (
QgsProcessingContext
)
- Return type:
- static combineLayerExtents(layers: Iterable[QgsMapLayer], crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem()) QgsRectangle [source]
Combines the extent of several map
layers
. If specified, the targetcrs
will be used to transform the layer’s extent to the desired output reference system.Deprecated since version 3.40: Use version with
QgsProcessingContext
argument instead.- Parameters:
layers (Iterable[QgsMapLayer])
crs (
QgsCoordinateReferenceSystem
= QgsCoordinateReferenceSystem())
- Return type:
- static compatibleAnnotationLayers(project: QgsProject | None, sort: bool = True) List[QgsAnnotationLayer] ¶
Returns a list of annotation layers from a
project
which are compatible with the processing framework.If the
sort
argument isTrue
then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
See also
See also
See also
See also
See also
See also
Added in version 3.22.
- Parameters:
project (Optional[QgsProject])
sort (bool = True)
- Return type:
List[QgsAnnotationLayer]
- static compatibleLayers(project: QgsProject | None, sort: bool = True) List[QgsMapLayer] ¶
Returns a list of map layers from a
project
which are compatible with the processing framework.If the
sort
argument isTrue
then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
- Parameters:
project (Optional[QgsProject])
sort (bool = True)
- Return type:
List[QgsMapLayer]
- static compatibleMeshLayers(project: QgsProject | None, sort: bool = True) List[QgsMeshLayer] ¶
Returns a list of mesh layers from a
project
which are compatible with the processing framework.If the
sort
argument isTrue
then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
See also
See also
See also
See also
See also
See also
Added in version 3.6.
- Parameters:
project (Optional[QgsProject])
sort (bool = True)
- Return type:
List[QgsMeshLayer]
- static compatiblePluginLayers(project: QgsProject | None, sort: bool = True) List[QgsPluginLayer] ¶
Returns a list of plugin layers from a
project
which are compatible with the processing framework.If the
sort
argument isTrue
then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
See also
See also
See also
See also
See also
See also
Added in version 3.22.
- Parameters:
project (Optional[QgsProject])
sort (bool = True)
- Return type:
List[QgsPluginLayer]
- static compatiblePointCloudLayers(project: QgsProject | None, sort: bool = True) List[QgsPointCloudLayer] ¶
Returns a list of point cloud layers from a
project
which are compatible with the processing framework.If the
sort
argument isTrue
then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
See also
See also
See also
See also
See also
See also
Added in version 3.22.
- Parameters:
project (Optional[QgsProject])
sort (bool = True)
- Return type:
List[QgsPointCloudLayer]
- static compatibleRasterLayers(project: QgsProject | None, sort: bool = True) List[QgsRasterLayer] ¶
Returns a list of raster layers from a
project
which are compatible with the processing framework.If the
sort
argument isTrue
then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
See also
See also
See also
See also
See also
See also
- Parameters:
project (Optional[QgsProject])
sort (bool = True)
- Return type:
List[QgsRasterLayer]
- static compatibleTiledSceneLayers(project: QgsProject | None, sort: bool = True) List[QgsTiledSceneLayer] ¶
Returns a list of tiled scene layers from a
project
which are compatible with the processing framework.If the
sort
argument isTrue
then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
See also
See also
See also
See also
See also
See also
Added in version 3.34.
- Parameters:
project (Optional[QgsProject])
sort (bool = True)
- Return type:
List[QgsTiledSceneLayer]
- static compatibleVectorLayers(project: QgsProject | None, sourceTypes: Iterable[int] = [], sort: bool = True) List[QgsVectorLayer] ¶
Returns a list of vector layers from a
project
which are compatible with the processing framework.The
sourceTypes
list should be filled with a list ofQgsProcessing
.SourceType values. If thesourceTypes
list is non-empty then the layers will be sorted so that only layers with the specified source type included in the list will be returned. Leaving thesourceTypes
list empty will cause all vector layers, regardless of their geometry type, to be returned.If the
sort
argument isTrue
then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
See also
See also
See also
See also
See also
See also
- Parameters:
project (Optional[QgsProject])
sourceTypes (Iterable[int] = [])
sort (bool = True)
- Return type:
List[QgsVectorLayer]
- static compatibleVectorTileLayers(project: QgsProject | None, sort: bool = True) List[QgsVectorTileLayer] ¶
Returns a list of vector tile layers from a
project
which are compatible with the processing framework.If the
sort
argument isTrue
then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
See also
See also
See also
See also
See also
See also
Added in version 3.32.
- Parameters:
project (Optional[QgsProject])
sort (bool = True)
- Return type:
List[QgsVectorTileLayer]
- static convertToCompatibleFormat(layer: QgsVectorLayer | None, selectedFeaturesOnly: bool, baseName: str | None, compatibleFormats: Iterable[str | None], preferredFormat: str | None, context: QgsProcessingContext, feedback: QgsProcessingFeedback | None, featureLimit: int = -1, filterExpression: str | None = '') str [source]¶
Converts a source vector
layer
to a file path of a vector layer of compatible format.If the specified
layer
is not of the format listed in thecompatibleFormats
argument, then the layer will first be exported to a compatible format in a temporary location usingbaseName
. 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.The
featureLimit
argument can be used to specify a limit on the number of features read from the layer.Since QGIS 3.32, the optional
filterExpression
argument can be used to specify a expression to use to filter the features read from the layer.When an algorithm is capable of handling multi-layer input files (such as Geopackage), it is preferable to use
convertToCompatibleFormatAndLayerName()
which may avoid conversion in more situations.- Parameters:
layer (Optional[QgsVectorLayer])
selectedFeaturesOnly (bool)
baseName (Optional[str])
compatibleFormats (Iterable[Optional[str]])
preferredFormat (Optional[str])
context (QgsProcessingContext)
feedback (Optional[QgsProcessingFeedback])
featureLimit (int = -1)
filterExpression (Optional[str] = '')
- Return type:
str
- static convertToCompatibleFormatAndLayerName(layer: QgsVectorLayer | None, selectedFeaturesOnly: bool, baseName: str | None, compatibleFormats: Iterable[str | None], preferredFormat: str | None, context: QgsProcessingContext, feedback: QgsProcessingFeedback | None, featureLimit: int = -1, filterExpression: str | None = '')[source]¶
Converts a source vector
layer
to a file path and layer name of a vector layer of compatible format.If the specified
layer
is not of the format listed in thecompatibleFormats
argument, then the layer will first be exported to a compatible format in a temporary location usingbaseName
. The function will then return the path to that temporary file.compatibleFormats
should consist entirely of lowercase file extensions, e.g. ‘shp’.The
featureLimit
argument can be used to specify a limit on the number of features read from the layer.The
preferredFormat
argument is used to specify to desired file extension to use when a temporary layer export is required. This defaults to shapefiles.This method should be preferred over
convertToCompatibleFormat()
when an algorithm is able to correctly handle files with multiple layers. UnlikeconvertToCompatibleFormat()
, it will not force a conversion in this case and will return the target layer name in thelayerName
argument.- Parameters:
layer (Optional[QgsVectorLayer]) – source layer to convert (if required)
selectedFeaturesOnly (bool) –
True
if only selected features from the layer should be usedbaseName (Optional[str]) – base file name for converted layer, if required
compatibleFormats (Iterable[Optional[str]]) – a list of lowercase file extensions compatible with the algorithm
preferredFormat (Optional[str]) – preferred format extension to use if conversion if required
context (QgsProcessingContext) – processing context
feedback (Optional[QgsProcessingFeedback]) – feedback object
featureLimit (int = -1) – can be used to place a limit on the maximum number of features read from the layer
filterExpression (Optional[str] = '') -> (str) – optional expression for filtering features read from the layer (since QGIS 3.32)
- Returns:
path to source layer, or nearly converted compatible layer
layerName: the target layer name for multi-layer sources (e.g. Geopackage)
See also
Added in version 3.10.
- static createFeatureSink(destination: str | None, context: QgsProcessingContext, fields: QgsFields, geometryType: Qgis.WkbType, crs: QgsCoordinateReferenceSystem, createOptions: Dict[str, Any] = {})¶
Creates a feature sink ready for adding features. The
destination
specifies a destination URI for the resultant layer. It may be updated in place to reflect the actual destination for the layer.Sink parameters such as desired
fields
,geometryType
andcrs
must be specified.The
createOptions
map can be used to specify additional sink creation options, which are passed to the underlying provider when creating new layers. Known options also include ‘fileEncoding’, which is used to specify a file encoding to use for created files. If ‘fileEncoding’ is not specified, the default encoding from thecontext
will be used.If a layer is created for the feature sink, the layer will automatically be added to the
context
’s temporary layer store.Note
this version of the
createFeatureSink()
function has an API designed around use from the SIP bindings. c++ code should call the othercreateFeatureSink()
version.- Parameters:
destination (Optional[str])
context (QgsProcessingContext)
fields (QgsFields)
geometryType (Qgis.WkbType)
createOptions (Dict[str, Any] = {}) -> (Optional[QgsFeatureSink])
- static decodeProviderKeyAndUri(string: str | None)[source]¶
Decodes a provider key and layer
uri
from an encoded string, for use withencodeProviderKeyAndUri()
- Parameters:
string (Optional[str]) -> (bool) – encoded string, as returned by
encodeProviderKeyAndUri()
- Returns:
True
ifstring
was successfully decodedproviderKey: ID key for corresponding data provider
uri: decoded layer uri
Added in version 3.14.
- static defaultPointCloudExtension() str [source]¶
Returns the default point cloud extension to use, in the absence of all other constraints (e.g. provider based support for extensions).
This method returns a fallback value of “las”.
See also
See also
See also
Added in version 3.24.
- Return type:
str
- static defaultRasterExtension() str [source]¶
Returns the default raster extension to use, in the absence of all other constraints (e.g. provider based support for extensions).
This method returns the user-set default extension from the processing settings, or a fallback value of “tif”.
See also
See also
See also
Added in version 3.10.
- Return type:
str
- static defaultVectorExtension() str [source]¶
Returns the default vector extension to use, in the absence of all other constraints (e.g. provider based support for extensions).
This method returns the user-set default extension from the processing settings, or a fallback value of “gpkg”.
See also
See also
See also
Added in version 3.10.
- Return type:
str
- static defaultVectorTileExtension() str [source]¶
Returns the default vector tile extension to use, in the absence of all other constraints (e.g. provider based support for extensions).
This method returns a fallback value of “mbtiles”.
See also
See also
See also
Added in version 3.32.
- Return type:
str
- static encodeProviderKeyAndUri(providerKey: str | None, uri: str | None) str [source]¶
Encodes a provider key and layer
uri
to a single string, for use withdecodeProviderKeyAndUri()
Added in version 3.14.
- Parameters:
providerKey (Optional[str])
uri (Optional[str])
- Return type:
str
- static fieldNamesToIndices(fieldNames: Iterable[str | None], fields: QgsFields) List[int] ¶
Returns a list of field indices parsed from the given list of field names. Unknown field names are ignored. If the list of field names is empty, it is assumed that all fields are required.
Added in version 3.2.
- Parameters:
fieldNames (Iterable[Optional[str]])
fields (QgsFields)
- Return type:
List[int]
- static formatHelpMapAsHtml(map: Dict[str, Any], algorithm: QgsProcessingAlgorithm | None) str [source]¶
Returns a HTML formatted version of the help text encoded in a variant
map
for a specifiedalgorithm
.- Parameters:
map (Dict[str, Any])
algorithm (Optional[QgsProcessingAlgorithm])
- Return type:
str
- static generateIteratingDestination(input: Any, id: Any, context: QgsProcessingContext) Any [source]¶
Converts an
input
parameter value for use in source iterating mode, where one individual sink is created per input feature. Theid
parameter represents the unique ID for this output, which is embedded into the resulting parameter value.- Parameters:
input (Any)
id (Any)
context (QgsProcessingContext)
- Return type:
Any
- static generateTempFilename(basename: str | None, context: QgsProcessingContext | None = None) str [source]¶
Returns a temporary filename for a given file, putting it into a temporary folder (creating that folder in the process), but not changing the
basename
.See also
- Parameters:
basename (Optional[str])
context (Optional[QgsProcessingContext] = None)
- Return type:
str
- static indicesToFields(indices: Iterable[int], fields: QgsFields) QgsFields [source]¶
Returns a subset of fields based on the indices of desired fields.
Added in version 3.2.
- static layerToStringIdentifier(layer: QgsMapLayer | None, layerName: str | None = '') str [source]¶
Returns a string representation of the source for a
layer
. The returned value is suitable for storage for subsequent executions of an algorithm using the same layer source.Added in version 3.34.
- Parameters:
layer (Optional[QgsMapLayer])
layerName (Optional[str] = '')
- Return type:
str
- static mapLayerFromString(string: str | None, context: QgsProcessingContext, allowLoadingNewLayers: bool = True, typeHint: QgsProcessingUtils.LayerHint = QgsProcessingUtils.LayerHint.UnknownType, flags: QgsProcessing.LayerOptionsFlags | QgsProcessing.LayerOptionsFlag = QgsProcessing.LayerOptionsFlags()) QgsMapLayer | None [source]¶
Interprets a string as a map layer within the supplied
context
.The method will attempt to load a layer matching the passed
string
. E.g. if the string matches a layer ID or name within the context’s project or temporary layer store then this layer will be returned. If the string is a file path andallowLoadingNewLayers
isTrue
, then the layer at this file path will be loaded and added to the context’s temporary layer store. Ownership of the layer remains with thecontext
or the context’s current project.The
typeHint
can be used to dictate the type of map layer expected.- Parameters:
string (Optional[str])
context (QgsProcessingContext)
allowLoadingNewLayers (bool = True)
typeHint (QgsProcessingUtils.LayerHint = QgsProcessingUtils.LayerHint.UnknownType)
flags (Union[QgsProcessing.LayerOptionsFlags, QgsProcessing.LayerOptionsFlag] = QgsProcessing.LayerOptionsFlags())
- Return type:
Optional[QgsMapLayer]
- static normalizeLayerSource(source: str | None) str [source]¶
Normalizes a layer
source
string for safe comparison across different operating system environments.- Parameters:
source (Optional[str])
- Return type:
str
- static outputDefinitionIndex(algorithm: QgsProcessingAlgorithm | None, name: str | None) int [source]¶
Returns the index of the output matching
name
for a specifiedalgorithm
. Matching is done in a case-insensitive manner.Added in version 3.44.
- Parameters:
algorithm (Optional[QgsProcessingAlgorithm])
name (Optional[str])
- Return type:
int
- static preprocessQgisProcessParameters(parameters: Dict[str, Any], error: str | None)[source]¶
Pre-processes a set of
parameter
values for the qgis_process command.In particular, this function upgrades encoded data defined values to
QgsProperty
objects.Added in version 3.30.
- Parameters:
parameters (Dict[str, Any])
error (Optional[str]) -> (Dict[str, Any])
- static removePointerValuesFromMap(map: Dict[str, Any]) Dict[str, Any] [source]¶
Removes any raw pointer values from an input
map
, replacing them with appropriate string values where possible.Added in version 3.26.
- Parameters:
map (Dict[str, Any])
- Return type:
Dict[str, Any]
- static resolveDefaultEncoding(defaultEncoding: str | None = '') str [source]¶
Returns the default encoding.
The default encoding could be the one from “/Processing/encoding” or when it’s not an allowed encoding name like “System”, the default encoding system (mostly UTF-8 on Unix-like, windows-1252 on Windows).
Added in version 3.32.
- Parameters:
defaultEncoding (Optional[str] = '')
- Return type:
str
- static stringToPythonLiteral(string: str | None) str [source]¶
Converts a string to a Python string literal. E.g. by replacing ‘ with '.
See also
- Parameters:
string (Optional[str])
- Return type:
str
- static tempFolder(context: QgsProcessingContext | None = None) str [source]¶
Returns a session specific processing temporary folder for use in processing algorithms.
See also
- Parameters:
context (Optional[QgsProcessingContext] = None)
- Return type:
str
- static variantToCrs(value: Any, context: QgsProcessingContext, fallbackValue: Any = None) QgsCoordinateReferenceSystem [source]¶
Converts a variant
value
to a coordinate reference system.The optional
fallbackValue
can be used to specify a “default” value which is used ifvalue
cannot be successfully converted to a CRS.Added in version 3.12.
- Parameters:
value (Any)
context (QgsProcessingContext)
fallbackValue (Any = None)
- Return type:
- static variantToPythonLiteral(value: Any) str [source]¶
Converts a variant to a Python literal.
See also
Added in version 3.6.
- Parameters:
value (Any)
- Return type:
str
- static variantToSource(value: Any, context: QgsProcessingContext, fallbackValue: Any = None) QgsProcessingFeatureSource | None [source]¶
Converts a variant
value
to a new feature source.Sources will either be taken from
context
’s active project, or loaded from external sources and stored temporarily in thecontext
.The optional
fallbackValue
can be used to specify a “default” value which is used ifvalue
cannot be successfully converted to a source.This function creates a new object and the caller takes responsibility for deleting the returned object.
- Parameters:
value (Any)
context (QgsProcessingContext)
fallbackValue (Any = None)
- Return type:
Optional[QgsProcessingFeatureSource]