I make two polylines features(in my example both consists of two points) with the add feature tool. Then I (with code) add some attribute values on the first feature, then changes the geometry of the second feature before I add values to the second values.

When I use layer.featureAtId() to get the second feature and then reads it, I get the values from the first feature.

I don't get this problem if I don't change the geometry or if I commit the two new polylines before doing the mentioned operations.

In the attribute table it is correct. If I use layer.nextFeature(feat) to iterate through all the features I get the right value.

# creates two features with the add feature tool
layer = qgis.utils.iface.activeLayer()

#select the first feature with the select tool 
featId1 = layer.selectedFeaturesIds()[0]

# add values  to the first feature
for i in range(5):
    layer.changeAttributeValue(featId1, i, (i*10))

#select the second feature with the select tool 
featId2 = layer.selectedFeaturesIds()[0]

# changes the geometry of the second feature
newGeom = QgsGeometry.fromPolyline([QgsPoint(1,1), QgsPoint(2,2)])
layer.changeGeometry(featId2, newGeom)
qgis.utils.iface.mapCanvas().refresh ()

# add values  to the second feature
for i in range(5):
    layer.changeAttributeValue(featId2, i, (i*10 + 1000))

# get the second feature by using featureAtId...    
feat = QgsFeature()
layer.featureAtId(featId2, feat, True, True)

# print values from the second feature
for i in range(5):
    print feat[i].toString()

# for some reason the attributeValues to the first feature get printed out. 
# in the attribute table it looks correct. 
# feat.geometry().asPolyline() seems to return the right geometry
# if I save edits after creating the two new features it works as it should   

I used QGIS version 1.8.0-Lisboa and QGIS version 1.9.0-Master

1 Answer 1


Both featId1 and featId2 are referencing the same indexed object, selectedFeaturesIds()[0]. You probably want selectedFeaturesIds()[1] to get a reference to the second selected feature.

  • I see that my code was a bit unclear. Before using featId1 = layer.selectedFeaturesIds()[0] and featId2 = layer.selectedFeaturesIds()[0] i used the "select features be rectangle" tool to only mark one feature at the time. So 'layer.selectedFeaturesIds()[0]` gives the right id both times.
    – user8790
    Jul 24, 2012 at 14:06
  • I've tested your steps and several others looking to arrive at the same result, and you are correct: whenever the geometry is updated for the non-written features, they inherit the original features attributes, regardless if they had custom attribute values already. This is evident via the Identify tool, but attribute table does show correctly (as you noted). You should create an issue ticket at hub.qgis.org/projects/quantum-gis/issues concerning this bug. However, there are probably workarounds, like saving the features to a memory layer, then writing to the data source.
    – dakcarto
    Jul 24, 2012 at 18:56
  • I have create an issue ticket now. Thank you for the tip.
    – user8790
    Jul 25, 2012 at 17:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.