Class: QgsProcessingFeatureBasedAlgorithm¶
-
class
qgis.core.
QgsProcessingFeatureBasedAlgorithm
¶ Bases:
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).Methods
- 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.
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.Read the source from
parameters
andcontext
and set it- param parameters
Processes an individual input
feature
from the source.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.
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.-
addOutput
()¶
-
addParameter
()¶
-
createInstance
()¶
-
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]
-
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).
- 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: 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).- Parameters
inputWkbType (QgsWkbTypes.Type) –
- Return type
-
parameterAsBool
()¶
-
parameterAsCompatibleSourceLayerPath
()¶
-
parameterAsCrs
()¶
-
parameterAsDouble
()¶
-
parameterAsEnum
()¶
-
parameterAsEnums
()¶
-
parameterAsExpression
()¶
-
parameterAsExtent
()¶
-
parameterAsExtentCrs
()¶
-
parameterAsExtentGeometry
()¶
-
parameterAsFields
()¶
-
parameterAsFile
()¶
-
parameterAsFileOutput
()¶
-
parameterAsInt
()¶
-
parameterAsInts
()¶
-
parameterAsLayer
()¶
-
parameterAsLayerList
()¶
-
parameterAsMatrix
()¶
-
parameterAsOutputLayer
()¶
-
parameterAsPoint
()¶
-
parameterAsPointCrs
()¶
-
parameterAsRange
()¶
-
parameterAsRasterLayer
()¶
-
parameterAsSink
()¶
-
parameterAsSource
()¶
-
parameterAsString
()¶
-
parameterAsVectorLayer
()¶
-
postProcessAlgorithm
()¶
-
prepareAlgorithm
()¶
-
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) → 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 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
object
-
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.
- 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
-
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) –