Class: QgsProcessingUtils¶
-
class
qgis.core.
QgsProcessingUtils
¶ Bases:
sip.wrapper
Utility functions for use with processing classes.
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 map 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 vector layers from a
project
which are compatible with the processing framework.Converts a source vector
layer
to a file path to a vector layer of compatible format.Creates a feature sink ready for adding features.
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 specifiedalgorithm
.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.
Interprets a string as a map layer within the supplied
context
.Normalizes a layer
source
string for safe comparison across different operating system environments.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 new feature source.Attributes
-
class
LayerHint
¶ Bases:
int
-
Raster
= 2¶
-
UnknownType
= 0¶
-
Vector
= 1¶
-
combineFields
(fieldsA: QgsFields, fieldsB: QgsFields) → QgsFields¶ 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.
-
combineLayerExtents
(layers: Iterable[QgsMapLayer], crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem()) → QgsRectangle¶ 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.- Parameters
layers (Iterable[QgsMapLayer]) –
crs (
QgsCoordinateReferenceSystem
= QgsCoordinateReferenceSystem()) –
- Return type
-
compatibleLayers
(project: QgsProject, 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 is true then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
- Parameters
project (QgsProject) –
sort (bool = True) –
- Return type
List[QgsMapLayer]
-
compatibleRasterLayers
(project: QgsProject, sort: bool = True) → object¶ Returns a list of raster layers from a
project
which are compatible with the processing framework.If the
sort
argument is true then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
- Parameters
project (QgsProject) –
sort (bool = True) –
- Return type
object
-
compatibleVectorLayers
(project: QgsProject, 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 of QgsProcessing.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 is true then the layers will be sorted by theirQgsMapLayer.name()
value.See also
See also
- Parameters
project (QgsProject) –
sourceTypes (Iterable[int] = []) –
sort (bool = True) –
- Return type
List[QgsVectorLayer]
-
convertToCompatibleFormat
(layer: QgsVectorLayer, selectedFeaturesOnly: bool, baseName: str, compatibleFormats: Iterable[str], preferredFormat: str, context: QgsProcessingContext, feedback: QgsProcessingFeedback) → str¶ Converts a source vector
layer
to a file path to 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.- Parameters
layer (QgsVectorLayer) –
selectedFeaturesOnly (bool) –
baseName (str) –
compatibleFormats (Iterable[str]) –
preferredFormat (str) –
context (QgsProcessingContext) –
feedback (QgsProcessingFeedback) –
- Return type
str
-
createFeatureSink
(destination: str, context: QgsProcessingContext, fields: QgsFields, geometryType: QgsWkbTypes.Type, crs: QgsCoordinateReferenceSystem, createOptions: Dict[str, Any] = {}) → Tuple[QgsFeatureSink, str]¶ 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 other createFeatureSink() version.
Note
available in Python bindings as createFeatureSink()
- Parameters
destination (str) –
context (QgsProcessingContext) –
fields (QgsFields) –
geometryType (QgsWkbTypes.Type) –
crs (QgsCoordinateReferenceSystem) –
createOptions (Dict[str) –
- Return type
Tuple[
QgsFeatureSink
, str]
-
fieldNamesToIndices
(fieldNames: Iterable[str], 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.
New in version 3.2.
- Parameters
fieldNames (Iterable[str]) –
fields (QgsFields) –
- Return type
List[int]
-
formatHelpMapAsHtml
(map: Dict[str, Any], algorithm: QgsProcessingAlgorithm) → str¶ Returns a HTML formatted version of the help text encoded in a variant
map
for a specifiedalgorithm
.- Parameters
map (Dict[str) –
algorithm (QgsProcessingAlgorithm) –
- Return type
str
-
generateIteratingDestination
(input: Any, id: Any, context: QgsProcessingContext) → Any¶ 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
-
generateTempFilename
(basename: str) → str¶ 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 (str) –
- Return type
str
-
indicesToFields
(indices: Iterable[int], fields: QgsFields) → QgsFields¶ Returns a subset of fields based on the indices of desired fields.
New in version 3.2.
-
mapLayerFromString
(string: str, context: QgsProcessingContext, allowLoadingNewLayers: bool = True, typeHint: QgsProcessingUtils.LayerHint = QgsProcessingUtils.UnknownType) → QgsMapLayer¶ 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
is true, 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 (str) –
context (QgsProcessingContext) –
allowLoadingNewLayers (bool = True) –
typeHint (QgsProcessingUtils.LayerHint = QgsProcessingUtils.UnknownType) –
- Return type
-
normalizeLayerSource
(source: str) → str¶ Normalizes a layer
source
string for safe comparison across different operating system environments.- Parameters
source (str) –
- Return type
str
-
stringToPythonLiteral
(string: str) → str¶ Converts a string to a Python string literal. E.g. by replacing ‘ with ‘.
- Parameters
string (str) –
- Return type
str
-
tempFolder
() → str¶ Returns a session specific processing temporary folder for use in processing algorithms.
See also
- Return type
str
-
variantToSource
(value: Any, context: QgsProcessingContext, fallbackValue: Any = None) → QgsProcessingFeatureSource¶ 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
-
class