Subgroup: Processing

Class: QgsProcessingFeatureBasedAlgorithm

class qgis.core.QgsProcessingFeatureBasedAlgorithm

Bases: qgis._core.QgsProcessingAlgorithm

Constructor for QgsProcessingFeatureBasedAlgorithm.

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: Methods

addOutput
addParameter
createInstance
initAlgorithm
initParameters Initializes any extra parameters added by the algorithm subclass.
inputLayerTypes Returns the valid input layer types for the source layer for this algorithm.
invalidRasterError
invalidSinkError
invalidSourceError
outputCrs Maps the input source coordinate reference system (inputCrs) to a corresponding output CRS generated by the algorithm.
outputFields Maps the input source fields (inputFields) to corresponding output fields generated by the algorithm.
outputLayerType Returns the layer type for layers generated by this algorithm, if this is possible to determine in advance.
outputName Returns the translated, user visible name for any layers created by this algorithm.
outputWkbType Maps the input WKB geometry type (inputWkbType) to the corresponding output WKB type generated by the algorithm.
parameterAsBool
parameterAsCompatibleSourceLayerPath
parameterAsCrs
parameterAsDouble
parameterAsEnum
parameterAsEnums
parameterAsExpression
parameterAsExtent
parameterAsExtentCrs
parameterAsExtentGeometry
parameterAsFields
parameterAsFile
parameterAsFileOutput
parameterAsInt
parameterAsLayer
parameterAsLayerList
parameterAsMatrix
parameterAsOutputLayer
parameterAsPoint
parameterAsPointCrs
parameterAsRange
parameterAsRasterLayer
parameterAsSink
parameterAsSource
parameterAsString
parameterAsVectorLayer
postProcessAlgorithm
prepareAlgorithm
processAlgorithm
processFeature Processes an individual input feature from the source.
removeParameter
request Returns the feature request used for fetching features to process from the source layer.
sourceCrs Returns the source’s coordinate reference system.
sourceFlags Returns the processing feature source flags to be used in the algorithm.

Signals

Attributes

addOutput()
addParameter()
createInstance()
initAlgorithm(self, configuration: Dict[str, Any] = QVariantMap())
initParameters(self, configuration: Dict[str, Any] = QVariantMap())

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.

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.

invalidRasterError()
invalidSinkError()
invalidSourceError()
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).

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.

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.

outputWkbType(self, inputWkbType: QgsWkbTypes.Type) → QgsWkbTypes.Type

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).

parameterAsBool()
parameterAsCompatibleSourceLayerPath()
parameterAsCrs()
parameterAsDouble()
parameterAsEnum()
parameterAsEnums()
parameterAsExpression()
parameterAsExtent()
parameterAsExtentCrs()
parameterAsExtentGeometry()
parameterAsFields()
parameterAsFile()
parameterAsFileOutput()
parameterAsInt()
parameterAsLayer()
parameterAsLayerList()
parameterAsMatrix()
parameterAsOutputLayer()
parameterAsPoint()
parameterAsPointCrs()
parameterAsRange()
parameterAsRasterLayer()
parameterAsSink()
parameterAsSource()
parameterAsString()
parameterAsVectorLayer()
postProcessAlgorithm()
prepareAlgorithm()
processAlgorithm(self, parameters: Dict[str, Any], context: QgsProcessingContext, feedback: QgsProcessingFeedback) → Dict[str, Any]
processFeature(self, feature: QgsFeature, context: QgsProcessingContext, feedback: QgsProcessingFeedback) → object

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 cancelation 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 using feedback to instead report non-fatal processing failures for features instead.

removeParameter()
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.

sourceCrs(self) → QgsCoordinateReferenceSystem

Returns the source’s coordinate reference system. This will only return a valid CRS when called from a subclasses’ processFeature() implementation.

sourceFlags(self) → QgsProcessingFeatureSource.Flag

Returns the processing feature source flags to be used in the algorithm.