1

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.

asked Mar 10, 2015 at 17:14

1 Answer 1

1

It was the indentation of the commitChanges line - it should have been dedented.

answered Mar 10, 2015 at 20:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.