Class: QgsProcessingFeatureBasedAlgorithm¶
- class qgis.core.QgsProcessingFeatureBasedAlgorithm¶
Bases:
QgsProcessingAlgorithm
An abstract
QgsProcessingAlgorithm
base class for processing algorithms which operate “feature-by-feature”.Feature based algorithms are algorithms which operate on individual features in isolation. These are algorithms where one feature is output for each input feature, and the output feature result for each input feature is not dependent on any other features present in the source.
For instance, algorithms like “centroids” and “buffers” are feature based algorithms since the centroid or buffer of a feature is calculated for each feature in isolation. An algorithm like “dissolve” is NOT suitable for a feature based algorithm as the dissolved output depends on multiple input features and these features cannot be processed in isolation.
Using
QgsProcessingFeatureBasedAlgorithm
as the base class for feature based algorithms allows shortcutting much of the common algorithm code for handling iterating over sources and pushing features to output sinks. It also allows the algorithm execution to be optimised in future (for instance allowing automatic multi-thread processing of the algorithm, or use of the algorithm in “chains”, avoiding the need for temporary outputs in multi-step models).New in version 3.0.
QgsProcessingFeatureBasedAlgorithm() Constructor for QgsProcessingFeatureBasedAlgorithm.
Methods
Adds an output
definition
to the algorithm.Adds a parameter
definition
to the algorithm.Creates a new instance of the algorithm class.
- rtype:
QgsProcessingAlgorithm.Flags
- param configuration:
Initializes any extra parameters added by the algorithm subclass.
Returns the valid input layer types for the source layer for this algorithm.
Returns the translated description of the parameter corresponding to the input layer.
Returns the name of the parameter corresponding to the input layer.
Returns a user-friendly string to use as an error when a raster layer input could not be loaded.
Returns a user-friendly string to use as an error when a sink parameter could not be created.
Returns a user-friendly string to use as an error when a source parameter could not be loaded.
Maps the input source coordinate reference system (
inputCrs
) to a corresponding output CRS generated by the algorithm.Maps the input source fields (
inputFields
) to corresponding output fields generated by the algorithm.Returns the layer type for layers generated by this algorithm, if this is possible to determine in advance.
Returns the translated, user visible name for any layers created by this algorithm.
Maps the input WKB geometry type (
inputWkbType
) to the corresponding output WKB type generated by the algorithm.Evaluates the parameter with matching
name
to an annotation layer.Evaluates the parameter with matching
name
to a static boolean value.Evaluates the parameter with matching
name
to a static boolean value.Evaluates the parameter with matching
name
to a color, or returns an invalid color if the parameter was not set.Evaluates the parameter with matching
name
to a source vector layer file path of compatible format.Evaluates the parameter with matching
name
to a source vector layer file path and layer name of compatible format.Evaluates the parameter with matching
name
to a connection name string.Evaluates the parameter with matching
name
to a coordinate reference system.Evaluates the parameter with matching
name
to a database table name string.Evaluates the parameter with matching
name
to a DateTime, or returns an invalid date time if the parameter was not set.Evaluates the parameter with matching
name
to a static double value.Evaluates the parameter with matching
name
to a enum value.Evaluates the parameter with matching
name
to a static enum string.Evaluates the parameter with matching
name
to list of static enum strings.Evaluates the parameter with matching
name
to list of enum values.Evaluates the parameter with matching
name
to an expression.Evaluates the parameter with matching
name
to a rectangular extent.Returns the coordinate reference system associated with an extent parameter value.
Evaluates the parameter with matching
name
to a rectangular extent, and returns a geometry covering this extent.Evaluates the parameter with matching
name
to a list of fields.Evaluates the parameter with matching
name
to a file/folder name.Evaluates the parameter with matching
name
to a list of files (forQgsProcessingParameterMultipleLayers
inQgsProcessing
:TypeFile mode).Evaluates the parameter with matching
name
to a file based output destination.Evaluates the parameter with matching
name
to a geometry.Returns the coordinate reference system associated with a geometry parameter value.
Evaluates the parameter with matching
name
to a static integer value.Evaluates the parameter with matching
name
to a list of integer values.Evaluates the parameter with matching
name
to a map layer.Evaluates the parameter with matching
name
to a list of map layers.Evaluates the parameter with matching
name
to a print layout.Evaluates the parameter with matching
name
to a print layout item, taken from the specifiedlayout
.Evaluates the parameter with matching
name
to a matrix/table of values.Evaluates the parameter with matching
name
to a mesh layer.Evaluates the parameter with matching
name
to a output layer destination.Evaluates the parameter with matching
name
to a point.Evaluates the parameter with matching
name
to a point cloud layer.Returns the coordinate reference system associated with an point parameter value.
Evaluates the parameter with matching
name
to a range of values.Evaluates the parameter with matching
name
to a raster layer.Evaluates the parameter with matching
name
to a database schema name string.Evaluates the parameter with matching
name
to a feature sink.Evaluates the parameter with matching
name
to a feature source.Evaluates the parameter with matching
name
to a static string value.Evaluates the parameter with matching
name
to a vector layer.Allows the algorithm to perform any required cleanup tasks.
Prepares the algorithm to run using the specified
parameters
.Read the source from
parameters
andcontext
and set it- param parameters:
Processes an individual input
feature
from the source.Removes the parameter with matching
name
from the algorithm, and deletes any existing definition.Returns the feature request used for fetching features to process from the source layer.
Returns the feature sink flags to be used for the output.
- param sink:
Returns the source's coordinate reference system.
Returns the processing feature source flags to be used in the algorithm.
Checks whether this algorithm supports in-place editing on the given
layer
Default implementation for feature based algorithms run some basic compatibility checks based on the geometry type of the layer.Returns a user-friendly string to use as an error when a feature cannot be written into a sink.
- addOutput(self, outputDefinition: QgsProcessingOutputDefinition) bool ¶
Adds an output
definition
to the algorithm. Ownership of the definition is transferred to the algorithm. ReturnsTrue
if the output could be successfully added, orFalse
if the output could not be added (e.g. as a result of a duplicate name).This should usually be called from a subclass’
initAlgorithm()
implementation.Note that in some cases output creation can be automatically performed when calling
addParameter()
. See the notes inaddParameter()
for a description of when this occurs.See also
See also
- addParameter(self, parameterDefinition: QgsProcessingParameterDefinition, createOutput: bool = True) bool ¶
Adds a parameter
definition
to the algorithm. Ownership of the definition is transferred to the algorithm. ReturnsTrue
if parameter could be successfully added, orFalse
if the parameter could not be added (e.g. as a result of a duplicate name).This should usually be called from a subclass’
initAlgorithm()
implementation.If the
createOutput
argument isTrue
, then a corresponding output definition will also be created (and added to the algorithm) where appropriate. E.g. when adding aQgsProcessingParameterVectorDestination
andcreateOutput
isTrue
, then aQgsProcessingOutputVectorLayer
output will be created and added to the algorithm. There is no need to calladdOutput()
to manually add a corresponding output for this vector. IfcreateOutput
isFalse
then this automatic output creation will not occur.See also
See also
- createInstance(self) QgsProcessingAlgorithm ¶
Creates a new instance of the algorithm class.
This method should return a ‘pristine’ instance of the algorithm class.
- flags(self) QgsProcessingAlgorithm.Flags ¶
- Return type:
- initAlgorithm(self, configuration: Dict[str, Any] = {})¶
- Parameters:
configuration (Dict[str) –
- initParameters(self, configuration: Dict[str, Any] = {})¶
Initializes any extra parameters added by the algorithm subclass. There is no need to declare the input source or output sink, as these are automatically created by QgsProcessingFeatureBasedAlgorithm.
- Parameters:
configuration (Dict[str) –
- inputLayerTypes(self) List[int] ¶
Returns the valid input layer types for the source layer for this algorithm. By default vector layers with any geometry types (excluding non-spatial, geometryless layers) are accepted.
- Return type:
List[int]
- inputParameterDescription(self) str ¶
Returns the translated description of the parameter corresponding to the input layer.
By default this is a translated “Input layer” string.
New in version 3.12.
- Return type:
str
- inputParameterName(self) str ¶
Returns the name of the parameter corresponding to the input layer.
By default this is the standard “INPUT” parameter name.
New in version 3.12.
- Return type:
str
- invalidRasterError(parameters: Dict[str, Any], name: str) str ¶
Returns a user-friendly string to use as an error when a raster layer input could not be loaded.
The
parameters
argument should give the algorithms parameter map, and thename
should correspond to the invalid source parameter name.See also
See also
New in version 3.2.
- invalidSinkError(parameters: Dict[str, Any], name: str) str ¶
Returns a user-friendly string to use as an error when a sink parameter could not be created.
The
parameters
argument should give the algorithms parameter map, and thename
should correspond to the invalid source parameter name.See also
See also
New in version 3.2.
- invalidSourceError(parameters: Dict[str, Any], name: str) str ¶
Returns a user-friendly string to use as an error when a source parameter could not be loaded.
The
parameters
argument should give the algorithms parameter map, and thename
should correspond to the invalid source parameter name.See also
See also
New in version 3.2.
- outputCrs(self, inputCrs: QgsCoordinateReferenceSystem) QgsCoordinateReferenceSystem ¶
Maps the input source coordinate reference system (
inputCrs
) to a corresponding output CRS generated by the algorithm. The default behavior is that the algorithm maintains the same CRS as the input source.This is called once by the base class when creating the output sink for the algorithm (i.e. it is not called once per feature processed).
- Parameters:
inputCrs (QgsCoordinateReferenceSystem) –
- Return type:
- outputFields(self, inputFields: QgsFields) QgsFields ¶
Maps the input source fields (
inputFields
) to corresponding output fields generated by the algorithm. The default behavior is that the algorithm maintains the same fields as are input. Algorithms which add, remove or modify existing fields should override this method and implement logic here to indicate which fields are output by the algorithm.This is called once by the base class when creating the output sink for the algorithm (i.e. it is not called once per feature processed).
- outputLayerType(self) QgsProcessing.SourceType ¶
Returns the layer type for layers generated by this algorithm, if this is possible to determine in advance.
- Return type:
- outputName(self) str ¶
Returns the translated, user visible name for any layers created by this algorithm. This name will be used as the default name when loading the resultant layer into a QGIS project.
- Return type:
str
- outputWkbType(self, inputWkbType: Qgis.WkbType) Qgis.WkbType ¶
Maps the input WKB geometry type (
inputWkbType
) to the corresponding output WKB type generated by the algorithm. The default behavior is that the algorithm maintains the same WKB type. This is called once by the base class when creating the output sink for the algorithm (i.e. it is not called once per feature processed).- Parameters:
inputWkbType (Qgis.WkbType) –
- Return type:
- parameterAsAnnotationLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsAnnotationLayer ¶
Evaluates the parameter with matching
name
to an annotation layer.Annotation layers will be taken from
context
’s active project. Callers do not need to handle deletion of the returned layer.Warning
Working with annotation layers is generally not thread safe (unless the layers are from a
QgsProject
loaded directly in a background thread). Ensure your algorithm returns the QgsProcessingAlgorithm.FlagNoThreading flag or only accesses annotation layers from aprepareAlgorithm()
orpostProcessAlgorithm()
step.New in version 3.22.
- parameterAsBool(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) bool ¶
Evaluates the parameter with matching
name
to a static boolean value.
- parameterAsBoolean(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) bool ¶
Evaluates the parameter with matching
name
to a static boolean value.New in version 3.8.
- parameterAsColor(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QColor ¶
Evaluates the parameter with matching
name
to a color, or returns an invalid color if the parameter was not set.New in version 3.10.
- parameterAsCompatibleSourceLayerPath(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, compatibleFormats: Iterable[str], preferredFormat: str = '', feedback: QgsProcessingFeedback = None) str ¶
Evaluates the parameter with matching
name
to a source vector layer file path of compatible format.If the parameter is evaluated to an existing layer, and that 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. 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.When an algorithm is capable of handling multi-layer input files (such as Geopackage), it is preferable to use
parameterAsCompatibleSourceLayerPathAndLayerName()
which may avoid conversion in more situations.
- parameterAsCompatibleSourceLayerPathAndLayerName(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, compatibleFormats: Iterable[str], preferredFormat: str = '', feedback: QgsProcessingFeedback = None) Tuple[str, str] ¶
Evaluates the parameter with matching
name
to a source vector layer file path and layer name of compatible format.If the parameter is evaluated to an existing layer, and that 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. 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, because shapefiles are the future (don’t believe the geopackage hype!).This method should be preferred over
parameterAsCompatibleSourceLayerPath()
when an algorithm is able to correctly handle files with multiple layers. UnlikeparameterAsCompatibleSourceLayerPath()
, it will not force a conversion in this case and will return the target layer name in thelayerName
argument.- Parameters:
parameters – input parameter value map
name – name of target parameter
context – processing context
compatibleFormats – a list of lowercase file extensions compatible with the algorithm
preferredFormat – preferred format extension to use if conversion if required
feedback – feedback object
- Returns:
path to source layer, or nearly converted compatible layer
layerName: will be set to the target layer name for multi-layer sources (e.g. Geopackage)
New in version 3.10.
- parameterAsConnectionName(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) str ¶
Evaluates the parameter with matching
name
to a connection name string.New in version 3.14.
- parameterAsCrs(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsCoordinateReferenceSystem ¶
Evaluates the parameter with matching
name
to a coordinate reference system.
- parameterAsDatabaseTableName(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) str ¶
Evaluates the parameter with matching
name
to a database table name string.New in version 3.14.
- parameterAsDateTime(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QDateTime ¶
Evaluates the parameter with matching
name
to a DateTime, or returns an invalid date time if the parameter was not set.New in version 3.14.
- parameterAsDouble(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) float ¶
Evaluates the parameter with matching
name
to a static double value.
- parameterAsEnum(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) int ¶
Evaluates the parameter with matching
name
to a enum value.
- parameterAsEnumString(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) str ¶
Evaluates the parameter with matching
name
to a static enum string.New in version 3.18.
- parameterAsEnumStrings(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) List[str] ¶
Evaluates the parameter with matching
name
to list of static enum strings.New in version 3.18.
- parameterAsEnums(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) List[int] ¶
Evaluates the parameter with matching
name
to list of enum values.
- parameterAsExpression(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) str ¶
Evaluates the parameter with matching
name
to an expression.
- parameterAsExtent(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem()) QgsRectangle ¶
Evaluates the parameter with matching
name
to a rectangular extent.If
crs
is set, and the original coordinate reference system of the parameter can be determined, then the extent will be automatically reprojected so that it is in the specifiedcrs
. In this case the extent of the reproject rectangle will be returned.See also
- parameterAsExtentCrs(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsCoordinateReferenceSystem ¶
Returns the coordinate reference system associated with an extent parameter value.
See also
- parameterAsExtentGeometry(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem()) QgsGeometry ¶
Evaluates the parameter with matching
name
to a rectangular extent, and returns a geometry covering this extent.If
crs
is set, and the original coordinate reference system of the parameter can be determined, then the extent will be automatically reprojected so that it is in the specifiedcrs
. UnlikeparameterAsExtent()
, the reprojected rectangle returned by this function will no longer be a rectangle itself (i.e. this method returns the geometry of the actual reprojected rectangle, whileparameterAsExtent()
returns just the extent of the reprojected rectangle).See also
- parameterAsFields(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) List[str] ¶
Evaluates the parameter with matching
name
to a list of fields.
- parameterAsFile(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) str ¶
Evaluates the parameter with matching
name
to a file/folder name.
- parameterAsFileList(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) List[str] ¶
Evaluates the parameter with matching
name
to a list of files (forQgsProcessingParameterMultipleLayers
inQgsProcessing
:TypeFile mode).New in version 3.10.
- parameterAsFileOutput(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) str ¶
Evaluates the parameter with matching
name
to a file based output destination.
- parameterAsGeometry(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem()) QgsGeometry ¶
Evaluates the parameter with matching
name
to a geometry.If
crs
is set then the geometry will be automatically reprojected so that it is in the specifiedcrs
.See also
- parameterAsGeometryCrs(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsCoordinateReferenceSystem ¶
Returns the coordinate reference system associated with a geometry parameter value.
See also
- parameterAsInt(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) int ¶
Evaluates the parameter with matching
name
to a static integer value.
- parameterAsInts(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) List[int] ¶
Evaluates the parameter with matching
name
to a list of integer values.New in version 3.4.
- parameterAsLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsMapLayer ¶
Evaluates the parameter with matching
name
to a map layer.Layers will either be taken from
context
’s active project, or loaded from external sources and stored temporarily in thecontext
. In either case, callers do not need to handle deletion of the returned layer.
- parameterAsLayerList(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) List[QgsMapLayer] ¶
Evaluates the parameter with matching
name
to a list of map layers.
- parameterAsLayout(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsPrintLayout ¶
Evaluates the parameter with matching
name
to a print layout.Warning
This method is not safe to run in a background thread, so it must either be used within a prepareAlgorithm implementation (which runs in the main thread), or the algorithm must return the FlagNoThreading flag.
New in version 3.8.
- parameterAsLayoutItem(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, layout: QgsPrintLayout) QgsLayoutItem ¶
Evaluates the parameter with matching
name
to a print layout item, taken from the specifiedlayout
.Warning
This method is not safe to run in a background thread, so it must either be used within a prepareAlgorithm implementation (which runs in the main thread), or the algorithm must return the FlagNoThreading flag.
New in version 3.8.
- parameterAsMatrix(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) List[Any] ¶
Evaluates the parameter with matching
name
to a matrix/table of values. Tables are collapsed to a 1 dimensional list.
- parameterAsMeshLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsMeshLayer ¶
Evaluates the parameter with matching
name
to a mesh layer.Layers will either be taken from
context
’s active project, or loaded from external sources and stored temporarily in thecontext
. In either case, callers do not need to handle deletion of the returned layer.New in version 3.6.
- parameterAsOutputLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) str ¶
Evaluates the parameter with matching
name
to a output layer destination.
- parameterAsPoint(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem()) QgsPointXY ¶
Evaluates the parameter with matching
name
to a point.If
crs
is set then the point will be automatically reprojected so that it is in the specifiedcrs
.See also
- parameterAsPointCloudLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsPointCloudLayer ¶
Evaluates the parameter with matching
name
to a point cloud layer.Layers will either be taken from
context
’s active project, or loaded from external sources and stored temporarily in thecontext
. In either case, callers do not need to handle deletion of the returned layer.New in version 3.22.
- parameterAsPointCrs(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsCoordinateReferenceSystem ¶
Returns the coordinate reference system associated with an point parameter value.
See also
- parameterAsRange(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) List[float] ¶
Evaluates the parameter with matching
name
to a range of values.
- parameterAsRasterLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsRasterLayer ¶
Evaluates the parameter with matching
name
to a raster layer.Layers will either be taken from
context
’s active project, or loaded from external sources and stored temporarily in thecontext
. In either case, callers do not need to handle deletion of the returned layer.
- parameterAsSchema(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) str ¶
Evaluates the parameter with matching
name
to a database schema name string.New in version 3.14.
- parameterAsSink(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext, fields: QgsFields, geometryType: Qgis.WkbType = Qgis.WkbType.NoGeometry, crs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem(), sinkFlags: QgsFeatureSink.SinkFlags | QgsFeatureSink.SinkFlag = QgsFeatureSink.SinkFlags(), createOptions: Dict[str, Any] = {}, datasourceOptions: Iterable[str] = [], layerOptions: Iterable[str] = []) Tuple[QgsFeatureSink, str] ¶
Evaluates the parameter with matching
name
to a feature sink.Sinks will either be taken from
context
’s active project, or created from external providers and stored temporarily in thecontext
.The
fields
,geometryType
andcrs
parameters dictate the properties of the resulting feature sink.The
destinationIdentifier
argument will be set to a string which can be used to retrieve the layer corresponding to the sink, e.g. via callingQgsProcessingUtils.mapLayerFromString()
.The
createOptions
argument is used to pass on creation options such as layer name.The
datasourceOptions
andlayerOptions
arguments is used to pass on GDAL-specific format driver options.This function creates a new object and the caller takes responsibility for deleting the returned object.
- Raises:
- parameterAsSource(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsProcessingFeatureSource ¶
Evaluates the parameter with matching
name
to a feature source.Sources will either be taken from
context
’s active project, or loaded from external sources and stored temporarily in thecontext
.This function creates a new object and the caller takes responsibility for deleting the returned object.
- parameterAsString(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) str ¶
Evaluates the parameter with matching
name
to a static string value.
- parameterAsVectorLayer(self, parameters: Dict[str, Any], name: str, context: QgsProcessingContext) QgsVectorLayer ¶
Evaluates the parameter with matching
name
to a vector layer.Layers will either be taken from
context
’s active project, or loaded from external sources and stored temporarily in thecontext
. In either case, callers do not need to handle deletion of the returned layer.
- postProcessAlgorithm(self, context: QgsProcessingContext, feedback: QgsProcessingFeedback) Dict[str, Any] ¶
Allows the algorithm to perform any required cleanup tasks. The returned variant map includes the results evaluated by the algorithm. These may be output layer references, or calculated values such as statistical calculations.
The
context
argument specifies the context in which the algorithm was run.Postprocess progress should be reported using the supplied
feedback
object. Additionally, well-behaved algorithms should periodically checkfeedback
to determine whether the post processing should be canceled and exited early.postProcessAlgorithm should be used to handle any thread-sensitive cleanup which is required by the algorithm. It will always be called from the same thread that
context
has thread affinity with. While this will generally be the main thread, it is not guaranteed. For instance, algorithms which are run as a step in a larger model or as a subcomponent of a script-based algorithm will call postProcessAlgorithm from the same thread as that model/script it being executed in.postProcessAlgorithm will not be called if the
prepareAlgorithm()
step failed (returnedFalse
), or if an exception was raised by theprocessAlgorithm()
step.- Returns:
A map of algorithm outputs. These may be output layer references, or calculated values such as statistical calculations. Implementations which return a non-empty map will override any results returned by
processAlgorithm()
.
See also
See also
- prepareAlgorithm(self, parameters: Dict[str, Any], context: QgsProcessingContext, feedback: QgsProcessingFeedback) bool ¶
Prepares the algorithm to run using the specified
parameters
. Algorithms should implement their logic for evaluating parameter values here. The evaluated parameter results should be stored in member variables ready for a call toprocessAlgorithm()
.The
context
argument specifies the context in which the algorithm is being run.prepareAlgorithm should be used to handle any thread-sensitive preparation which is required by the algorithm. It will always be called from the same thread that
context
has thread affinity with. While this will generally be the main thread, it is not guaranteed. For instance, algorithms which are run as a step in a larger model or as a subcomponent of a script-based algorithm will call prepareAlgorithm from the same thread as that model/script it being executed in.Note that the processAlgorithm step uses a temporary context with affinity for the thread in which the algorithm is executed, making it safe for processAlgorithm implementations to load sources and sinks without issue. Implementing prepareAlgorithm is only required if special thread safe handling is required by the algorithm.
Algorithm preparation progress should be reported using the supplied
feedback
object. Additionally, well-behaved algorithms should periodically checkfeedback
to determine whether the algorithm should be canceled and exited early.If the preparation was successful algorithms must return
True
. If aFalse
value is returned this indicates that the preparation could not be completed, and the algorithm execution will be canceled.- Returns:
True
if preparation was successful.
See also
See also
- prepareSource(self, parameters: Dict[str, Any], context: QgsProcessingContext)¶
Read the source from
parameters
andcontext
and set itNew in version 3.4.
- Parameters:
parameters (Dict[str) –
context (QgsProcessingContext) –
- processAlgorithm(self, parameters: Dict[str, Any], context: QgsProcessingContext, feedback: QgsProcessingFeedback) Dict[str, Any] ¶
- Parameters:
parameters (Dict[str) –
context (QgsProcessingContext) –
feedback (QgsProcessingFeedback) –
- Return type:
Dict[str, Any]
- processFeature(self, feature: QgsFeature, context: QgsProcessingContext, feedback: QgsProcessingFeedback) List[QgsFeature] ¶
Processes an individual input
feature
from the source. Algorithms should implement their logic in this method for performing the algorithm’s operation (e.g. replacing the feature’s geometry with the centroid of the original feature geometry for a ‘centroid’ type algorithm).Implementations should return a list containing the modified feature. Returning an empty an list will indicate that this feature should be ‘skipped’, and will not be added to the algorithm’s output. Subclasses can use this approach to filter the incoming features as desired.
Additionally, multiple features can be returned for a single input feature. Each returned feature will be added to the algorithm’s output. This allows for “explode” type algorithms where a single input feature results in multiple output features.
The provided
feedback
object can be used to push messages to the log and for giving feedback to users. Note that handling of progress reports and algorithm cancellation is handled by the base class and subclasses do not need to reimplement this logic.Algorithms can throw a
QgsProcessingException
if a fatal error occurred which should prevent the algorithm execution from continuing. This can be annoying for users though as it can break valid model execution - so use with extreme caution, and consider usingfeedback
to instead report non-fatal processing failures for features instead.- Parameters:
feature (QgsFeature) –
context (QgsProcessingContext) –
feedback (QgsProcessingFeedback) –
- Return type:
List[QgsFeature]
- removeParameter(self, name: str)¶
Removes the parameter with matching
name
from the algorithm, and deletes any existing definition.
- request(self) QgsFeatureRequest ¶
Returns the feature request used for fetching features to process from the source layer. The default implementation requests all attributes and geometry.
- Return type:
- sinkFlags(self) QgsFeatureSink.SinkFlags ¶
Returns the feature sink flags to be used for the output.
New in version 3.4.1.
- Return type:
- sinkProperties(self, sink: str, parameters: Dict[str, Any], context: QgsProcessingContext, sourceProperties: Dict[str, QgsProcessingAlgorithm.VectorProperties]) QgsProcessingAlgorithm.VectorProperties ¶
- Parameters:
sink (str) –
parameters (Dict[str) –
context (QgsProcessingContext) –
sourceProperties (Dict[str) –
- Return type:
- sourceCrs(self) QgsCoordinateReferenceSystem ¶
Returns the source’s coordinate reference system. This will only return a valid CRS when called from a subclasses’
processFeature()
implementation.- Return type:
- sourceFlags(self) QgsProcessingFeatureSource.Flag ¶
Returns the processing feature source flags to be used in the algorithm.
- Return type:
- supportInPlaceEdit(self, layer: QgsMapLayer) bool ¶
Checks whether this algorithm supports in-place editing on the given
layer
Default implementation for feature based algorithms run some basic compatibility checks based on the geometry type of the layer.- Return type:
bool
- Returns:
True
if the algorithm supports in-place editing
New in version 3.4.
- Parameters:
layer (QgsMapLayer) –
- writeFeatureError(sink: QgsFeatureSink, parameters: Dict[str, Any], name: str) str ¶
Returns a user-friendly string to use as an error when a feature cannot be written into a sink.
The
sink
argument is the sink into which the feature cannot be written.The
parameters
argument should give the algorithms parameter map, and thename
should correspond to the sink parameter name.New in version 3.22.