I have two polygons in different layers. I am trying to find the intersect between the two polygons. Below a figure showing my test case. I initially wanted to find the intersect between the green polygon and a bunch of other polygons and wrote a code for that. For all but one polygon this functioned as expected, shown below is the one polygon for which my function fails.
from qgis.core import * from PyQt4.QtCore import * layer1=None layer2=None for lyr in QgsMapLayerRegistry.instance().mapLayers().values(): if lyr.name() =='Poly': layer1 = lyr if lyr.name() =='Buffer': layer2 = lyr features1 = layer1.getFeatures() features2 = layer2.getFeatures() for feature1 in features1: for feature2 in features2: geominter=feature1.geometry().intersection(feature2.geometry()) print(geominter.area()) print(geominter.asPolygon()) layer = QgsVectorLayer('Polygon', 'Intersect' , 'memory') prov = layer.dataProvider() layer.startEditing() feat = QgsFeature() feat.setGeometry(geominter) prov.addFeatures([feat]) layer.commitChanges() layer.updateExtents() QgsMapLayerRegistry.instance().addMapLayers([layer]) layer.triggerRepaint()
This code worked for all other polygons I needed to find the intersect for except this one. The output layer contains one element (as expected) but nothing is plotted. The print outputs of this code are:
It accurately computes the area of the polygon I expect to obtain but the .asPolygon() output is empty, which I expect is why nothing is plotted. What could be the reason for this, and how can I obtain the intersect between these two polygons?
I am using QGIS version 2.18.11 on Windows 8.