6

I am trying to implement a QGIS processing algorithm which creates singleparts from a multiparts polygon layer and additionally adds a field with a unique ID column (just the index number) in order to distinguish between the newly created singleparts (which otherwise would inherit the exact same attributes).

So far I was able to implement the multiparttosingleparts processing algorithm as well as copying the attributes over to the newly generated layer. Before reading the singlepart layer and adding the features to the sink I am trying to generate a new field called 'sub_gid' (sub ID).

layer_provider = singlepart_layer.dataProvider()
layer_provider.addAttributes([QgsField('sub_gid', QVariant.Int)])
singlepart_layer.updateFields() # Will generate the fields property of this layer by obtaining all fields from the dataProvider

Unfortunately the attribute table of the output layer doesn't show the "sub_gid" field.

What am I missing in my code?

Find attached the full QGIS script:

    from PyQt5.QtCore import QCoreApplication
    from qgis.PyQt.QtCore import QVariant
    from qgis.core import (QgsProcessing,
                           QgsFeatureSink,
                           QgsFeature, # To change and read Features
                           QgsField, # To edit fields
                           QgsVectorDataProvider, # For retrieval and writing of feature and attribute information 
                           QgsProcessingException,
                           QgsProcessingAlgorithm,
                           QgsProcessingParameterFeatureSource,
                           QgsProcessingParameterFeatureSink)
    import processing


    class SGISmGlAlgorithm(QgsProcessingAlgorithm):
    """
    qgis script for filtering the sgi2016 (Swiss Glacier Inventory 2016)
    originally used to identify small glaciers based on the following categories:

    1. Size: <= 0.04 km2
    2. Slope >25°

    The algorithm identifies new glaciers, which developed from defragmenting glacier complexes.
    -> multipart to single part

    All Processing algorithms should extend the QgsProcessingAlgorithm
    class.
    """

    # Constants used to refer to parameters and outputs. They will be
    # used when calling the algorithm from another algorithm, or when
    # calling from the QGIS console.

    INPUT = 'INPUT'
    OUTPUT = 'OUTPUT'

    def tr(self, string):
        """
        Returns a translatable string with the self.tr() function.
        """
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return SGISmGlAlgorithm()

    def name(self):
        """
        Returns the algorithm name, used for identifying the algorithm. This
        string should be fixed for the algorithm, and must not be localised.
        The name should be unique within each provider. Names should contain
        lowercase alphanumeric characters only and no spaces or other
        formatting characters.
        """
        return 'sgi_glacier_filter'

    def displayName(self):
        """
        Returns the translated algorithm name, which should be used for any
        user-visible display of the algorithm name.
        """
        return self.tr('SGI glacier Filter')

    def group(self):
        """
        Returns the name of the group this algorithm belongs to. This string
        should be localised.
        """
        return self.tr('scripts')

    def groupId(self):
        """
        Returns the unique ID of the group this algorithm belongs to. This
        string should be fixed for the algorithm, and must not be localised.
        The group id should be unique within each provider. Group id should
        contain lowercase alphanumeric characters only and no spaces or other
        formatting characters.
        """
        return 'scripts'

    def shortHelpString(self):
        """
        Returns a localised short helper string for the algorithm. This string
        should provide a basic description about what the algorithm does and the
        parameters and outputs associated with it..
        """
        return self.tr("Algorithm to filter the SGI (Swiss Glacier Inventory) by various glacier characteristics")

    def initAlgorithm(self, config=None):
        """
        Here we define the inputs and output of the algorithm, along
        with some other properties.
        """

        # We add the input vector features source. It can have any kind of
        # geometry.
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )

        # We add a feature sink in which to store our processed features (this
        # usually takes the form of a newly created vector layer when the
        # algorithm is run in QGIS).
        self.addParameter(
            QgsProcessingParameterFeatureSink(
                self.OUTPUT,
                self.tr('sgi_filtered')
            )
        )

    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """

        # Retrieve the feature source and sink. The 'dest_id' variable is used
        # to uniquely identify the feature sink, and must be included in the
        # dictionary returned by the processAlgorithm function.
        source = self.parameterAsSource(
            parameters,
            self.INPUT,
            context
        )

        # If source was not found, throw an exception to indicate that the algorithm
        # encountered a fatal error. The exception text can be any string, but in this
        # case we use the pre-built invalidSourceError method to return a standard
        # helper text for when a source cannot be evaluated
        if source is None:
            raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))

        (sink, dest_id) = self.parameterAsSink(
            parameters,
            self.OUTPUT,
            context,
            source.fields(),
            source.wkbType(),
            source.sourceCrs()
        )

        # Send some information to the user
        feedback.pushInfo('CRS is {}'.format(source.sourceCrs().authid()))

        # If sink was not created, throw an exception to indicate that the algorithm
        # encountered a fatal error. The exception text can be any string, but in this
        # case we use the pre-built invalidSinkError method to return a standard
        # helper text for when a sink cannot be evaluated
        if sink is None:
            raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))

        # Compute the number of steps to display within the progress bar and
        # get features from source
        total = 100.0 / source.featureCount() if source.featureCount() else 0
        features = source.getFeatures()

        for current, feature in enumerate(features):
            # Stop the algorithm if cancel button has been clicked
            if feedback.isCanceled():
                break

            # Add a feature in the sink
            sink.addFeature(feature, QgsFeatureSink.FastInsert)

            # Update the progress bar
            feedback.setProgress(int(current * total))
        
        # Multipart to Singlepart processing in order to split up glacier polygons, which
        # previously used to be connected and have now defragmented into smaller glaciers
        feedback.pushInfo('Multipart to Singlepart')    
        singlepart_layer = processing.run('native:multiparttosingleparts', {
                'INPUT': parameters[self.INPUT],
                'OUTPUT': 'memory:' 
                }, context=context, feedback=feedback)['OUTPUT']
                
        layer_provider = singlepart_layer.dataProvider()
        layer_provider.addAttributes([QgsField('sub_gid', QVariant.Int)])
        singlepart_layer.updateFields() # Will generate the fields property of this layer by obtaining all fields from the dataProvider
        
        # Read the singlepart layer and create output features
        for f in singlepart_layer.getFeatures():
            new_feature = QgsFeature()
            # Set geometry to singlepart geometry
            new_feature.setGeometry(f.geometry())

            # Copy attributes from SGI attributes and add to new features
            attrs = f.attributes()
            new_feature.setAttributes(attrs)
            
            sink.addFeature(new_feature, QgsFeatureSink.FastInsert)

        # To run another Processing algorithm as part of this algorithm, you can use
        # processing.run(...). Make sure you pass the current context and feedback
        # to processing.run to ensure that all temporary layer outputs are available
        # to the executed algorithm, and that the executed algorithm can send feedback
        # reports to the user (and correctly handle cancellation and progress reports!)


        # Return the results of the algorithm. In this case our only result is
        # the feature sink which contains the processed features, but some
        # algorithms may return multiple feature sinks, calculated numeric
        # statistics, etc. These should all be included in the returned
        # dictionary, with keys matching the feature corresponding parameter
        # or output names.
        return {self.OUTPUT: dest_id}

2 Answers 2

5

There are a few issues here, but I think the main problem is that you are adding the new 'sub_gid' field to the temporary output from the processing.run() call instead of adding it to the feature sink. Also, I don't actually see where you are adding any feature attributes which would fill the new column. Finally, it looks like you are adding all features from the input layer, then adding all features from the temporary output with the new field added, which again will not work so I'm not sure why you are doing that.

Try the modified version of your script below:

from PyQt5.QtCore import QCoreApplication
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsProcessing,
                       QgsProcessingUtils, 
                       QgsFeatureSink,
                       QgsFeature, # To change and read Features
                       QgsField, # To edit fields
                       QgsProcessingException,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterFeatureSink,
                       QgsProcessingMultiStepFeedback)
import processing


class SGISmGlAlgorithm(QgsProcessingAlgorithm):

    INPUT = 'INPUT'
    OUTPUT = 'OUTPUT'

    def tr(self, string):
        """
        Returns a translatable string with the self.tr() function.
        """
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return SGISmGlAlgorithm()

    def name(self):
        return 'sgi_glacier_filter'

    def displayName(self):
        return self.tr('SGI glacier Filter')

    def group(self):
        return self.tr('scripts')

    def groupId(self):
        return 'scripts'

    def shortHelpString(self):
        return self.tr("Algorithm to filter the SGI (Swiss Glacier Inventory) by various glacier characteristics")

    def initAlgorithm(self, config=None):
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )

        self.addParameter(
            QgsProcessingParameterFeatureSink(
                self.OUTPUT,
                self.tr('sgi_filtered')
            )
        )

    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(
            parameters,
            self.INPUT,
            context
        )
        if source is None:
            raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
        
        # Retrieve source layer fields
        flds = source.fields()
        
        # Append new field
        flds.append(QgsField('sub_gid', QVariant.Int))
        

        (sink, dest_id) = self.parameterAsSink(
            parameters,
            self.OUTPUT,
            context,
            flds, # QgsFields object containing additional field
            source.wkbType(),
            source.sourceCrs()
        )
        
        multi_step_feedback = QgsProcessingMultiStepFeedback(2, feedback)
        
        feedback.pushInfo('CRS is {}'.format(source.sourceCrs().authid()))

        if sink is None:
            raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
        
        feedback.pushInfo('Multipart to Singlepart')
        
        multi_step_feedback.setCurrentStep(1)
        
        singlepart_output = processing.run('native:multiparttosingleparts', {
                'INPUT': parameters[self.INPUT],
                'OUTPUT': 'TEMPORARY_OUTPUT' 
                }, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
        
        # Get access to a layer object from temporary output layer
        singlepart_layer = QgsProcessingUtils.mapLayerFromString(singlepart_output, context)
        
        multi_step_feedback.setCurrentStep(2)
        
        # Read the singlepart layer and create output features
        for current, f in enumerate(singlepart_layer.getFeatures()):
            if feedback.isCanceled():
                break
            feedback.setProgress((singlepart_layer.featureCount()/(current+1))*100)

            new_feature = QgsFeature(flds)
            # Set geometry to singlepart geometry
            new_feature.setGeometry(f.geometry())

            # Copy attributes from SGI attributes and add to new features
            attrs = f.attributes()
            
            # Append current index to attributes to add (in "sub_gid" field)
            attrs.append(current)
            
            new_feature.setAttributes(attrs)
            
            sink.addFeature(new_feature, QgsFeatureSink.FastInsert)
            
        return {self.OUTPUT: dest_id}
1
  • Yes this script worked well! Thanks a lot! I actually worked a little bit in the console and figured out the mistake with the temporary output.
    – J.Beer
    Nov 14, 2022 at 8:00
5

It doesn't answer your question directly (using layer_provider.addAttributes), but you could try using the addAttribute() method of the layer itself:

singplepart_layer.startEditing()
singlepart_layer.addAttribute(QgsField('fieldname', QVariant.Int, len=0))
singlepart_layer.commitChanges()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.