I am trying to assign attributes from features in one layer that intersect features in another layer. When I run my code, I do not get an errors but it does not update the attribute field as I am expecting. I wrote my code based on the example in this post https://stackoverflow.com/questions/23506358/pyqgis-for-spatial-join. It appears as though the program does not see that any features intersect, but when i compare them on the map canvas, they do. I'm not sure what's wrong. Here is my code:
GPSDegLyr.dataProvider().addAttributes([QgsField('UniqueID',QVariant.Int)])
GPSDegLyr.updateFields()
idxGPS = GPSDegLyr.fieldNameIndex('UniqueID')
idxBuff = dsslvExplodeLyrWGS84.fieldNameIndex('UniqueID')
GPSDegLyr.startEditing()
for b in dsslvExplodeLyrWGS84.getFeatures():
for g in GPSDegLyr.getFeatures():
if g.geometry().intersects(b.geometry()) == True:
val = b.attributes()[idxBuff]
print val
g.setAttribute(idxGPS,val)
GPSDegLyr.updateFeature(g)
GPSDegLyr.commitChanges()
QgsMapLayerRegistry.instance().addMapLayer(GPSDegLyr)
UPDATE: I solved the intersecting problem - now the programs sees the intersections and prints the values. But still doesn't write the values out to the attrribute table.
1 Answer 1
It was the indentation of the commitChanges line - it should have been dedented.
Explore related questions
See similar questions with these tags.