I am trying to write a PyQGIS script that copies information from one feature into an output feature and adds a field.
This is how I'm initializing my fields for my output layer
inlayer = processing.getObject(input_layer)
provider = inlayer.dataProvider()
fields = provider.fields()
fields.append( QgsField("NewField", QVariant.Double) )
writer = QgsVectorFileWriter(output_layer, 'UTF-8', fields, QGis.WKBPoint, inlayer.crs())
I'm then looping through the features and adding output features to my writer
outFeature = QgsFeature(fields)
outFeature.setGeometry(setting the geometry... nothing to see here)
outFeature.setAttributes(inFeature.attributes()) #copying attributes from the input layer
outFeature.setAttribute("NewValue", newValue) #this line is what is causing the error
writer.addFeature(outFeature)
When I run the script I am getting an steady stream of error messages in the log messages panel under the General tab
Attribute index 9 out of bounds [0;9]
If I check the attributes table for the output layer that I just created, I have all the fields that I'm expecting. I have all the attributes from the input layer, and I have a column for the NewValue
, but I do not have any data populated in the NewValue
column. The NewValue
column is the 10th column, so I'm confused why my script is failing to write to that column saying index 9 is out of bounds because I would think that the NewValue
column would be index 9. What am I missing or doing wrong?