QgsProcessingUtils class
Utility functions for use with processing classes.
Contents
- Reference
Public types
- enum LayerHint { UnknownType, Vector, Raster, Mesh }
- Layer type hints.
Public static functions
- static auto combineFields(const QgsFields& fieldsA, const QgsFields& fieldsB) -> QgsFields
- Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
- static auto combineLayerExtents(const QList<QgsMapLayer*>& layers, const QgsCoordinateReferenceSystem& crs = QgsCoordinateReferenceSystem()) -> QgsRectangle
- Combines the extent of several map layers.
- static auto compatibleLayers(QgsProject* project, bool sort = true) -> QList<QgsMapLayer*>
- Returns a list of map layers from a project which are compatible with the processing framework.
- static auto compatibleMeshLayers(QgsProject* project, bool sort = true) -> QList<QgsMeshLayer*>
- Returns a list of mesh layers from a project which are compatible with the processing framework.
- static auto compatibleRasterLayers(QgsProject* project, bool sort = true) -> QList<QgsRasterLayer*>
- Returns a list of raster layers from a project which are compatible with the processing framework.
- static auto compatibleVectorLayers(QgsProject* project, const QList<int>& sourceTypes = QList<int>(), bool sort = true) -> QList<QgsVectorLayer*>
- Returns a list of vector layers from a project which are compatible with the processing framework.
- static auto convertToCompatibleFormat(const QgsVectorLayer* layer, bool selectedFeaturesOnly, const QString& baseName, const QStringList& compatibleFormats, const QString& preferredFormat, QgsProcessingContext& context, QgsProcessingFeedback* feedback) -> QString
- Converts a source vector layer to a file path to a vector layer of compatible format.
-
static auto createFeatureSink(QString& destination,
QgsProcessingContext& context,
const QgsFields& fields,
QgsWkbTypes::
Type geometryType, const QgsCoordinateReferenceSystem& crs, const QVariantMap& createOptions = QVariantMap(), QgsFeatureSink::SinkFlags sinkFlags = nullptr) -> QgsFeatureSink* - Creates a feature sink ready for adding features.
-
static void createFeatureSinkPython(QgsFeatureSink** sink,
QString& destination,
QgsProcessingContext& context,
const QgsFields& fields,
QgsWkbTypes::
Type geometryType, const QgsCoordinateReferenceSystem& crs, const QVariantMap& createOptions = QVariantMap()) - Creates a feature sink ready for adding features.
- static auto fieldNamesToIndices(const QStringList& fieldNames, const QgsFields& fields) -> QList<int>
- Returns a list of field indices parsed from the given list of field names.
- static auto formatHelpMapAsHtml(const QVariantMap& map, const QgsProcessingAlgorithm* algorithm) -> QString
- Returns a HTML formatted version of the help text encoded in a variant map for a specified algorithm.
- static auto generateIteratingDestination(const QVariant& input, const QVariant& id, QgsProcessingContext& context) -> QVariant
- Converts an input parameter value for use in source iterating mode, where one individual sink is created per input feature.
- static auto generateTempFilename(const QString& basename) -> QString
- 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.
- static auto indicesToFields(const QList<int>& indices, const QgsFields& fields) -> QgsFields
- Returns a subset of fields based on the indices of desired fields.
- static auto mapLayerFromString(const QString& string, QgsProcessingContext& context, bool allowLoadingNewLayers = true, LayerHint typeHint = UnknownType) -> QgsMapLayer*
- Interprets a string as a map layer within the supplied context.
- static auto normalizeLayerSource(const QString& source) -> QString
- Normalizes a layer source string for safe comparison across different operating system environments.
- static auto stringToPythonLiteral(const QString& string) -> QString
- Converts a string to a Python string literal.
- static auto tempFolder() -> QString
- Returns a session specific processing temporary folder for use in processing algorithms.
- static auto variantToSource(const QVariant& value, QgsProcessingContext& context, const QVariant& fallbackValue = QVariant()) -> QgsProcessingFeatureSource*
- Converts a variant value to a new feature source.
Enum documentation
enum QgsProcessingUtils:: LayerHint
Layer type hints.
| Enumerators | |
|---|---|
| UnknownType |
Unknown layer type. |
| Vector |
Vector layer type. |
| Raster |
Raster layer type. |
| Mesh |
Mesh layer type. |
Function documentation
static QgsFields QgsProcessingUtils:: combineFields(const QgsFields& fieldsA,
const QgsFields& fieldsB)
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.
static QgsRectangle QgsProcessingUtils:: combineLayerExtents(const QList<QgsMapLayer*>& layers,
const QgsCoordinateReferenceSystem& crs = QgsCoordinateReferenceSystem())
Combines the extent of several map layers.
If specified, the target crs will be used to transform the layer's extent to the desired output reference system.
static QList<QgsMapLayer*> QgsProcessingUtils:: compatibleLayers(QgsProject* project,
bool sort = true)
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 their QgsMapLayer::
static QList<QgsMeshLayer*> QgsProcessingUtils:: compatibleMeshLayers(QgsProject* project,
bool sort = true)
Returns a list of mesh layers from a project which are compatible with the processing framework.
If the sort argument is true then the layers will be sorted by their QgsMapLayer::
static QList<QgsRasterLayer*> QgsProcessingUtils:: compatibleRasterLayers(QgsProject* project,
bool sort = true)
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 their QgsMapLayer::
static QList<QgsVectorLayer*> QgsProcessingUtils:: compatibleVectorLayers(QgsProject* project,
const QList<int>& sourceTypes = QList<int>(),
bool sort = true)
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::
If the sort argument is true then the layers will be sorted by their QgsMapLayer::
static QString QgsProcessingUtils:: convertToCompatibleFormat(const QgsVectorLayer* layer,
bool selectedFeaturesOnly,
const QString& baseName,
const QStringList& compatibleFormats,
const QString& preferredFormat,
QgsProcessingContext& context,
QgsProcessingFeedback* feedback)
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 the compatibleFormats argument, then the layer will first be exported to a compatible format in a temporary location using baseName. 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.
static QgsFeatureSink* QgsProcessingUtils:: createFeatureSink(QString& destination,
QgsProcessingContext& context,
const QgsFields& fields,
QgsWkbTypes:: Type geometryType,
const QgsCoordinateReferenceSystem& crs,
const QVariantMap& createOptions = QVariantMap(),
QgsFeatureSink::SinkFlags sinkFlags = nullptr)
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 encoding, fields, geometryType and crs 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 the context 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.
The caller takes responsibility for deleting the returned sink.
static void QgsProcessingUtils:: createFeatureSinkPython(QgsFeatureSink** sink,
QString& destination,
QgsProcessingContext& context,
const QgsFields& fields,
QgsWkbTypes:: Type geometryType,
const QgsCoordinateReferenceSystem& crs,
const QVariantMap& createOptions = QVariantMap())
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 and crs 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 the context 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.
static QList<int> QgsProcessingUtils:: fieldNamesToIndices(const QStringList& fieldNames,
const QgsFields& fields)
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.
static QVariant QgsProcessingUtils:: generateIteratingDestination(const QVariant& input,
const QVariant& id,
QgsProcessingContext& context)
Converts an input parameter value for use in source iterating mode, where one individual sink is created per input feature.
The id parameter represents the unique ID for this output, which is embedded into the resulting parameter value.
static QString QgsProcessingUtils:: generateTempFilename(const QString& basename)
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.
static QgsFields QgsProcessingUtils:: indicesToFields(const QList<int>& indices,
const QgsFields& fields)
Returns a subset of fields based on the indices of desired fields.
static QgsMapLayer* QgsProcessingUtils:: mapLayerFromString(const QString& string,
QgsProcessingContext& context,
bool allowLoadingNewLayers = true,
LayerHint typeHint = UnknownType)
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 and allowLoadingNewLayers 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 the context or the context's current project.
The typeHint can be used to dictate the type of map layer expected.
static QString QgsProcessingUtils:: stringToPythonLiteral(const QString& string)
Converts a string to a Python string literal.
E.g. by replacing ' with \'.
static QString QgsProcessingUtils:: tempFolder()
Returns a session specific processing temporary folder for use in processing algorithms.
static QgsProcessingFeatureSource* QgsProcessingUtils:: variantToSource(const QVariant& value,
QgsProcessingContext& context,
const QVariant& fallbackValue = QVariant())
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 the context.
The optional fallbackValue can be used to specify a "default" value which is used if value cannot be successfully converted to a source.
This function creates a new object and the caller takes responsibility for deleting the returned object.