Skip to main content
added 2 characters in body; edited tags
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

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?

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?

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?

Source Link
CitizenFish
  • 740
  • 3
  • 19

QGIS Processing Script - Add field to output

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?