Estoy intentando crear y actualizar un campo y, por ahora, sólo cambiar su valor a 2.
Aquí está mi código:
layer = qgis.utils.iface.activeLayer()
index = layer.fieldNameIndex("P")
# if index == -1: # field doesn't exist
caps = layer.dataProvider().capabilities()
if caps & QgsVectorDataProvider.AddAttributes:
res = layer.dataProvider().addAttributes( [ QgsField("P", \
QVariant.Double) ] )
layer.updateFields()
for feature in layer.getFeatures():
# None of these work
index = layer.fieldNameIndex("P")
feature["P"] = 2
feature.setAttribute(index,(2.0))
feature.attributes()[index] = 2
#################
layer.commitChanges()
Pero todo lo que obtengo es una nueva columna llena de valores NULL. Sin embargo, no puedo averiguar dónde estoy cometiendo un error.
EDITAR:
Parece que el valor real se ha actualizado (último valor) :
feature.attributes()
[28965.0, 209294.0, 43.0, 207459.0, NULL, u'HANOVER', u'AV', 3901.0, 4049.0, 3900.0, 4048.0, u'Hanover Av', u'LOCAL STR', NULL, NULL, u'LOCAL STREET', NULL, 0.0, 50.0, 50.0, 5436.0, 43.0, 25.0, 209294.0, NULL, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, NULL, 0.0, 0.0, 0.0, 0.0, 1, 2]
Pero en el shapefile, sigue mostrando NULL.