As @BERA says, the attribute map is a dictionary of dictionaries, with the first key as the feature ID, and the second key as the field index.
e.g.
attrMap = {
featureId1: {fieldIndex1: value1, fieldIndex2: value2, fieldIndex3: value3},
featureId2: {fieldIndex1: value4, fieldIndex2: value5},
...}
Updated code using changeAttributeValues
(there may be more efficient ways of iterating):
outfile = "c:/temp/output.gpkg"
oldFieldNames = ["oldField1", "oldField2", "oldField3"]
newFieldNames = ["newField1", "newField2", "newField3"]
mapping = [
{0: "orange", 1: "apple", 2: "pineapple"},
{0: "red", 1: "yellow", 2: "green", 3: "blue"},
{0: "dog", 1: "cat", 2: "goat", 3: "cow", 4: "fish"},
]
vl = QgsVectorLayer(outfile, 'temp', 'ogr')
pr = vl.dataProvider()
attrMap = {}
newFieldIndices = []
for i in range(len(newFieldNames)):
newFieldIndices.append(pr.fieldNameIndex(newFieldNames[i]))
for feature in vl.getFeatures():
featureAttrMap = {}
for i in range(len(oldFieldNames)):
featureAttrMap[newFieldIndices[i]] = mapping[i][feature[oldFieldNames[i]]]
attrMap[feature.id()] = featureAttrMap
# print(attrMap)
pr.changeAttributeValues(attrMap)
The attrMap data for the example above is:
attrMap = {
1: {4: 'orange', 5: 'blue', 6: 'fish'},
2: {4: 'apple', 5: 'green', 6: 'cow'}
}
Best Answer
Yes. Import these:
Then:
Documented:
https://qgis.org/api/classQgsGeometry.html#ac99eef4d4d213c72559f4bd3fe5aefa6