I am using QGIS 3.12 and I want to update the content of a field by storing the feature's X and Y coordinates. The following code works given that a point layer is in the layer panel. The function works when the layer.updateFeature(feat.id())
statement is blocked, and so I could not understand the use of this method?
layer = iface.activeLayer()
layer.startEditing()
all_features = layer.getFeatures()
for feat in all_features:
geom = feat.geometry()
x = geom.asPoint().x()
y = geom.asPoint().y()
new_x = {feat.fieldNameIndex('x'): x}
layer.dataProvider().changeAttributeValues({feat.id(): new_x })
new_y = {feat.fieldNameIndex('y'): y}
layer.dataProvider().changeAttributeValues({feat.id(): new_y })
#layer.updateFeature(feat.id()) #?
# Commit changes
layer.commitChanges()