0

I have a shapefile that contains three lines. I want to add a specific numeric id to the field "line_id" and a string id to the field "border_id". Basically, for the first line, I want to add the value '1' to "line_id" and 'North' to field "border_id". For the second line I want to add the value '2' to "line_id" and 'South' to field "border_id".

Here is my code, I think I don't get how the changeAttributeValues command works on pyQGIS.

lyr.startEditing()
provider = lyr.dataProvider()
provider.addAttributes([QgsField("line_id",QVariant.Double),(QgsField("border_id",QVariant.String))])
lyr.updateFields()
lyr.commitChanges()

enter image description here

1 Answer 1

1

I propose to use the following code in case you have two features in the lyrr.

values = {1: 'North', 2: 'South'}
counter = 0
for feat in lyr.getFeatures():
    counter += 1
    feat.setAttribute('line_id', counter)
    feat.setAttribute('border_id', values[counter])
    lyr.updateFeature(feat)
lyr.commitChanges()

It will work for python 3.6 and qgis 3.2

0

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.