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

Note

This is an abstract class, with methods which must be implemented by a subclass.

The following methods must be implemented: QgsProcessingAlgorithm.createInstance(), QgsProcessingAlgorithm.displayName(), QgsProcessingAlgorithm.name(), outputName(), processFeature()

Class Hierarchy

Inheritance diagram of qgis.core.QgsProcessingFeatureBasedAlgorithm

Base classes

QgsProcessingAlgorithm

Abstract base class for processing algorithms.

Subclasses

QgsBatchGeocodeAlgorithm

A base class for batch geocoder algorithms, which takes a QgsGeocoderInterface object and exposes it as a Processing algorithm for batch geocoding operations.

Abstract Methods

outputName

Returns the translated, user visible name for any layers created by this algorithm.

processFeature

Processes an individual input feature from the source.

Methods

prepareSource

Read the source from parameters and context and set it

sourceCrs

Returns the source's coordinate reference system.

Virtual Methods

In PyQGIS, only methods marked as virtual can be safely overridden in a Python subclass of QgsProcessingFeatureBasedAlgorithm. See the FAQ for more details.

initParameters

Initializes any extra parameters added by the algorithm subclass.

inputLayerTypes

Returns the valid input layer types for the source layer for this algorithm.

inputParameterDescription

Returns the translated description of the parameter corresponding to the input layer.

inputParameterName

Returns the name of the parameter corresponding to the input layer.

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.

outputWkbType

Maps the input WKB geometry type (inputWkbType) to the corresponding output WKB type generated by the algorithm.

request

Returns the feature request used for fetching features to process from the source layer.

sinkFlags

Returns the feature sink flags to be used for the output.

sourceFlags

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

supportInPlaceEdit

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.

class qgis.core.QgsProcessingFeatureBasedAlgorithm[source]

Bases: QgsProcessingAlgorithm

virtual initParameters(self, configuration: Dict[str, Any] = {})[source]

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, Any] = {})

virtual 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]

virtual inputParameterDescription(self) str[source]

Returns the translated description of the parameter corresponding to the input layer.

By default this is a translated “Input layer” string.

Added in version 3.12.

Return type:

str

virtual inputParameterName(self) str[source]

Returns the name of the parameter corresponding to the input layer.

By default this is the standard “INPUT” parameter name.

Added in version 3.12.

Return type:

str

virtual outputCrs(self, inputCrs: QgsCoordinateReferenceSystem) QgsCoordinateReferenceSystem[source]

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:

QgsCoordinateReferenceSystem

virtual outputFields(self, inputFields: QgsFields) QgsFields[source]

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

Parameters:

inputFields (QgsFields)

Return type:

QgsFields

virtual outputLayerType(self) Qgis.ProcessingSourceType[source]

Returns the layer type for layers generated by this algorithm, if this is possible to determine in advance.

Return type:

Qgis.ProcessingSourceType

abstract outputName(self) str[source]

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

virtual outputWkbType(self, inputWkbType: Qgis.WkbType) Qgis.WkbType[source]

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:

Qgis.WkbType

prepareSource(self, parameters: Dict[str, Any], context: QgsProcessingContext)[source]

Read the source from parameters and context and set it

Added in version 3.4.

Parameters:
abstract processFeature(self, feature: QgsFeature, context: QgsProcessingContext, feedback: QgsProcessingFeedback | None) List[QgsFeature][source]

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

Parameters:
Return type:

List[QgsFeature]

virtual request(self) QgsFeatureRequest[source]

Returns the feature request used for fetching features to process from the source layer. The default implementation requests all attributes and geometry.

Return type:

QgsFeatureRequest

virtual sinkFlags(self) QgsFeatureSink.SinkFlags[source]

Returns the feature sink flags to be used for the output.

Added in version 3.4.1.

Return type:

QgsFeatureSink.SinkFlags

sourceCrs(self) QgsCoordinateReferenceSystem[source]

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

Return type:

QgsCoordinateReferenceSystem

virtual sourceFlags(self) Qgis.ProcessingFeatureSourceFlags[source]

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

Return type:

Qgis.ProcessingFeatureSourceFlags

virtual supportInPlaceEdit(self, layer: QgsMapLayer | None) bool[source]

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

Added in version 3.4.

Parameters:

layer (Optional[QgsMapLayer])