QgsProcessingFeatureBasedAlgorithm class

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

Base classes

class QgsProcessingAlgorithm
Abstract base class for processing algorithms.

Constructors, destructors, conversion operators

QgsProcessingFeatureBasedAlgorithm() defaulted
Constructor for QgsProcessingFeatureBasedAlgorithm.

Public functions

auto flags() const -> QgsProcessingAlgorithm::Flags override
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
auto processFeature(const QgsFeature& feature, QgsProcessingContext& context, QgsProcessingFeedback* feedback) -> QgsFeatureList pure virtual
Processes an individual input feature from the source.

Protected functions

void initAlgorithm(const QVariantMap& configuration = QVariantMap()) override
Initializes the algorithm using the specified configuration.
void initParameters(const QVariantMap& configuration = QVariantMap()) virtual
Initializes any extra parameters added by the algorithm subclass.
auto inputLayerTypes() const -> QList<int> virtual
Returns the valid input layer types for the source layer for this algorithm.
auto outputCrs(const QgsCoordinateReferenceSystem& inputCrs) const -> QgsCoordinateReferenceSystem virtual
Maps the input source coordinate reference system (inputCrs) to a corresponding output CRS generated by the algorithm.
auto outputFields(const QgsFields& inputFields) const -> QgsFields virtual
Maps the input source fields (inputFields) to corresponding output fields generated by the algorithm.
auto outputLayerType() const -> QgsProcessing::SourceType virtual
Returns the layer type for layers generated by this algorithm, if this is possible to determine in advance.
auto outputName() const -> QString pure virtual
Returns the translated, user visible name for any layers created by this algorithm.
auto outputWkbType(QgsWkbTypes::Type inputWkbType) const -> QgsWkbTypes::Type virtual
Maps the input WKB geometry type (inputWkbType) to the corresponding output WKB type generated by the algorithm.
void prepareSource(const QVariantMap& parameters, QgsProcessingContext& context)
Read the source from parameters and context and set it.
auto processAlgorithm(const QVariantMap& parameters, QgsProcessingContext& context, QgsProcessingFeedback* feedback) -> QVariantMap virtual
Runs the algorithm using the specified parameters.
auto request() const -> QgsFeatureRequest virtual
Returns the feature request used for fetching features to process from the source layer.
auto sinkFlags() const -> QgsFeatureSink::SinkFlags virtual
Returns the feature sink flags to be used for the output.
auto sourceCrs() const -> QgsCoordinateReferenceSystem
Returns the source's coordinate reference system.
auto sourceFlags() const -> QgsProcessingFeatureSource::Flag virtual
Returns the processing feature source flags to be used in the algorithm.
auto supportInPlaceEdit(const QgsMapLayer* layer) const -> bool override
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.

Function documentation

QgsProcessingAlgorithm::Flags QgsProcessingFeatureBasedAlgorithm::flags() const override

Returns the flags indicating how and when the algorithm operates and should be exposed to users.

Default flags are FlagSupportsBatch and FlagCanCancel.

QgsFeatureList QgsProcessingFeatureBasedAlgorithm::processFeature(const QgsFeature& feature, QgsProcessingContext& context, QgsProcessingFeedback* feedback) pure virtual

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.

void QgsProcessingFeatureBasedAlgorithm::initAlgorithm(const QVariantMap& configuration = QVariantMap()) override protected

Initializes the algorithm using the specified configuration.

This should be called directly after creating algorithms and before retrieving any parameterDefinitions() or outputDefinitions().

Subclasses should use their implementations to add all required input parameter and output definitions (which can be dynamically adjusted according to configuration).

Dynamic configuration can be used by algorithms which alter their behavior when used inside processing models. For instance, a "feature router" type algorithm which sends input features to one of any number of outputs sinks based on some preconfigured filter parameters can use the init method to create these outputs based on the specified configuration.

void QgsProcessingFeatureBasedAlgorithm::initParameters(const QVariantMap& configuration = QVariantMap()) virtual protected

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.

QList<int> QgsProcessingFeatureBasedAlgorithm::inputLayerTypes() const virtual protected

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.

QgsCoordinateReferenceSystem QgsProcessingFeatureBasedAlgorithm::outputCrs(const QgsCoordinateReferenceSystem& inputCrs) const virtual protected

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

QgsFields QgsProcessingFeatureBasedAlgorithm::outputFields(const QgsFields& inputFields) const virtual protected

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

QString QgsProcessingFeatureBasedAlgorithm::outputName() const pure virtual protected

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.

QgsWkbTypes::Type QgsProcessingFeatureBasedAlgorithm::outputWkbType(QgsWkbTypes::Type inputWkbType) const virtual protected

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

void QgsProcessingFeatureBasedAlgorithm::prepareSource(const QVariantMap& parameters, QgsProcessingContext& context) protected

Read the source from parameters and context and set it.

QVariantMap QgsProcessingFeatureBasedAlgorithm::processAlgorithm(const QVariantMap& parameters, QgsProcessingContext& context, QgsProcessingFeedback* feedback) virtual protected

Runs the algorithm using the specified parameters.

Returns A map of algorithm outputs. These may be output layer references, or calculated values such as statistical calculations. Unless the algorithm subclass overrides the postProcessAlgorithm() step this returned map will be used as the output for the algorithm.

Algorithms should implement their custom processing logic here.

The context argument gives a temporary context with thread affinity matching the thread in which the algorithm is being run. This is a cut-back copy of the context passed to the prepareAlgorithm() and postProcessAlgorithm() steps, but it is generally safe for most algorithms to utilize this context for loading layers and creating sinks. Any loaded layers or sinks created within this temporary context will be transferred back to the main execution context upon successful completion of the processAlgorithm() step.

Algorithm progress should be reported using the supplied feedback object. Additionally, well-behaved algorithms should periodically check feedback to determine whether the algorithm should be canceled and exited early.

This method will not be called if the prepareAlgorithm() step failed (returned false).

c++ implementations of processAlgorithm can throw the QgsProcessingException exception to indicate that a fatal error occurred within the execution. Python based subclasses should raise GeoAlgorithmExecutionException for the same purpose.

QgsFeatureRequest QgsProcessingFeatureBasedAlgorithm::request() const virtual protected

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

The default implementation requests all attributes and geometry.

QgsFeatureSink::SinkFlags QgsProcessingFeatureBasedAlgorithm::sinkFlags() const virtual protected

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

QgsCoordinateReferenceSystem QgsProcessingFeatureBasedAlgorithm::sourceCrs() const protected

Returns the source's coordinate reference system.

This will only return a valid CRS when called from a subclasses' processFeature() implementation.

bool QgsProcessingFeatureBasedAlgorithm::supportInPlaceEdit(const QgsMapLayer* layer) const override protected

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 true if the algorithm supports in-place editing