Wednesday 11 September 2019

qgis - Troubles Updating Values of Selected Features with PyQGIS


I'm trying to modify the values of some selected features. I have written this small script, but nothing happens when I run it. Is there something that I've missed? Here is my code.


lyr = qgis.utils.iface.activeLayer()
features = lyr.selectedFeatures()

lyr.startEditing()
lyr.updateFields()
for f in features:

fid= f.id()
lyr.changeAttributeValue(fid,12,"0")
lyr.updateFeature(f)

print "elemento alterado"

lyr.commitChanges()

Answer



Just remove the lyr.updateFeature(f) line that is inside your for loop. You don't need it and, according to the docs, it's also inefficient.


If that doesn't work, make sure the 13th field is of type String.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...