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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 23, 2019 at 11:55

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)
answered Jan 23, 2019 at 14:31
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))
Matt
19.4k4 gold badges25 silver badges64 bronze badges
answered Apr 17, 2023 at 13:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.