1

In QGIS with PyQGIS I want to update values of a field in features of a vector layer. I see that the feature field is updated when I do a print. But it doesn't commit the value, and I don't see them updated in the attributeTable. Why is that? Anyone has a clue?

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
from qgis.PyQt.QtCore import QVariant

sanitation = pd.read_csv("data.csv")

layer = iface.activeLayer()

adm2PCodeFNI = layer.dataProvider().fieldNameIndex("ADM2_PCODE")
budgetPercentFNI = layer.dataProvider().fieldNameIndex("BDGT_PERC")
print(adm2PCodeFNI)
print(budgetPercentFNI)

layer.startEditing()

for feature in layer.getFeatures():
    for i in range(0,len(sanitation)):
        adm2_pcode = sanitation.iloc[i, sanitation.columns.get_loc("adm2_pcode")]
        if feature.attributes()[adm2PCodeFNI] == adm2_pcode :
            bP = sanitation.iloc[i, sanitation.columns.get_loc("budget_perc")]
            feature[budgetPercentFNI] = bP
            print(feature.attributes())
            layer.updateFeature(feature)
            break

layer.commitChanges()
2
  • If I use layer.changeAttributeValue() and I cast the Float value to a String, I get the values in the AttributeTable, although the type in the table is Double. Strange behaviour, but this is a workaround for me to get the data in the AttributeTable. Jun 22, 2020 at 13:18
  • 1
    See my comment at gis.stackexchange.com/questions/365221/… Could help
    – ThomasG77
    Jun 22, 2020 at 13:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.