2

My objective is to add a new calculated field to the output layer from a processing script.

This layer is created as follows:-


source = self.parameterAsSource(
            parameters,
            self.INPUT,
            context
        )

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

I am then adding a feature to the sink layer from source in an iteration loop


 for current, feature in enumerate(source.getFeatures()):
      value = 1
      #I want to add value to feature as an attribute, how??
      sink.addFeature(feature, QgsFeatureSink.FastInsert)

How do I add the value attribute to the sink layer?

Should it be done when declaring sink?

2 Answers 2

7

Ok I managed it, bit hacky and I need to copy in other fields from feature

Declare the sink with new fields

new_fields = source.fields()
new_fields.append(QgsField('overlap', QVariant.Double))
(sink, dest_id) = self.parameterAsSink(
            parameters,
            self.OUTPUT,
            context,
            new_fields,
            source.wkbType(),
            source.sourceCrs()
)

Create a new feature and output

 new_feat = QgsFeature()
 new_feat.setGeometry(feature.geometry())
 new_feat.setFields(new_fields)
 new_feat['overlap'] = overlap
 sink.addFeature(new_feat, QgsFeatureSink.FastInsert)
0

I have been looking for this answer for WAY too long.

My task was keeping some old features as well, ended up needing to copy them over.

Here is my solution

        #adding a field for a sampled DEM 
        new_fields = source.fields()
        new_fields.append(QgsField('DEM', QVariant.Double))

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

        #we do not add the original features, we are adding a copy of the old features
             # construct a new feature as unfortunately features form a source object can't be manipulated 
            new_feat = QgsFeature()

            #new fields is old fields plus one new, see up there
            new_feat.setFields(new_fields)

            # iterate over the old features attributes to fill up its copy
            for current2,att in enumerate(feature.attributes()):
                 new_feat.setAttribute(new_fields.field(current2).name(),att)

            
            #add the sampled dem here
            new_feat.setAttribute('z',sampled_values[current])
            
            #feedback.pushInfo(new_feat.attributes)
            sink.addFeature(new_feat, QgsFeatureSink.FastInsert)
            
            # Update the progress bar
            feedback.setProgress(int(current * total))

Your Answer

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

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