3

I'm able to successfully add attributes with an expression to my vector layer using:

#Add fields to get evaulated based on an expression
vectorLayer.addExpressionField("'R'",
QgsField("rtype",QVariant.String, 'string', 11))

vectorLayer.addExpressionField("y(point_on_surface($geometry))",
QgsField("lat", QVariant.Double, 'double', 20, 7))

vectorLayer.addExpressionField("x(point_on_surface($geometry))",
QgsField("long", QVariant.Double, 'double', 20, 7))

vectorLayer.addExpressionField("\'Comment explanation; type: {}\'".format(type), QgsField("comment",QVariant.String))

vectorLayer.updateFields()

Then based on a select by location I need to update the comment value, so I get the indexes using:

request = QgsFeatureRequest().setFilterFid(1)
features = vectorLayer.getFeatures(request)

#Get indexes for attributes we are keeping 
for feature in features:

     rTypeIdx = feature.fields().indexFromName("rtype") 

     latIdx = feature.fields().indexFromName("lat")

     longIdx = feature.fields().indexFromName("long")

     commentIdx = feature.fields().indexFromName("comment")

To update the comment accordingly I use:

selectedFeatures = vectorLayer.selectedFeatures()

for feature in selectedFeatures:

     fid = feature.id()

     #Get comment index and value for updating attribute values 
     commentIdx = feature.fields().indexFromName("comment")

     commentValue = 'Comment value'

     attributes = {commentIdx: commentValue}

     feedback.pushInfo("Updating feature {} with attributes{}".format(fid,attributes))
     vectorLayer.dataProvider().changeAttributeValues({fid: attributes})

After I run my script everything works, except I get the warning that my attribute index for the comment is 10 out of bounds [0;7]. I've verified this index using several different methods including feature.fields().indexFromName("comment"). They all return the index value of 10 which is the last attribute. If I hardcode the value of 7 for my index it crashes QGIS. What am I doing wrong? How can I get the correct index value to update this field without getting an index error? I'm using QGIS version 3.x.

EDIT: I'm now trying to update my feature using

vectorLayer.startEditing()
featureUpdated = vectorLayer.changeAttributeValue(fid, commentIdx, commentValue)
vectorLayer.commitChanges()

This is also failing by returning False.

  • since you are changing only one value you can try to use: vectorLayer.changeAttributeValue(fid, commentIdx, commentValue) – eurojam May 21 at 6:36
  • The changeAttributeValue method for vectorLayer just seems to silently fail. – user1587687 May 21 at 18:24
  • It seems like since I used the vectorLayer to add my fields using a QgsExpression that I need to continue to update the layer this way instead of using the dataProvider. By mixing these two my indexes don't appear to be matching up. – user1587687 May 21 at 19:54

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.