Sunday, 3 July 2016

qgis - Pyqgis: "a.geometry().intersects(b.geometry())" wouldn't find any intersections but should. Why?


I am teaching myself pyqgis and try to find those polygons in one layer that intersect with polygons in another layer. It seems very trivial, and on GIS.SE are several similar questions that are suggesting me (with my limited python/pyqgis-knowledge) that this should work:


#ausschluss and gebaeude are QgsVectorLayers with Polygons 
#(shapefiles with the same crs and clearly intersecting)

gebaeude.setSelectedFeatures([]) #deselects everything
selections=[] #declares it is a list
for f in gebaeude.getFeatures():
for a in ausschluss.getFeatures():
if a.geometry().intersects(f.geometry()):
#same result with .within() and even with a and f switched (the docs aren't that clear on that)
selections.append( f.id() )
break #only one or less intersection are possible
gebaeude.setSelectedFeatures(selections)
gebaeude.invertSelection()


#gives 0, 78137 an 78137 (no intersection found)
print len(selections)
print gebaeude.selectedFeatureCount()
print gebaeude.featureCount()

What is it, I am doing wrong?


clearly intersecting...


Edit: Running QGis 2.8.3 on OS X (10.8.5) with the KyngChaos-Longterm-Built


system-stuff





No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...