It is not clear for me what do you want to do with above code but, when you have overlapped features in the same layer you can use itertools python module for avoiding this kind of lines:
if outerFeature.id() != innerFeature.id()
for repeated features. So, your code can be written as follow for producing a memory layer of all intersection of primary features.
layer = iface.activeLayer()
features = [ feature for feature in layer.getFeatures() ]
n = layer.featureCount()
list = range(n)
intersections = [features[i].geometry().intersection(features[j].geometry()).asWkt()
for i,j in itertools.combinations(list, 2)
if features[i].geometry().intersects(features[j].geometry()) ]
epsg = layer.crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"
mem_layer = QgsVectorLayer(uri,
prov = mem_layer.dataProvider()
feats = [ QgsFeature() for i in range(len(intersections)) ]
for i, feat in enumerate(feats):
By using shapefile with overlapped features of following image:
after running above code, I got following result:
However, these interceptions can also intercepts each other or with another primary features. In this case, for a complete clipping, you can use 'saga:polygonselfintersection' method from Processing in a very concise script as follow:
polys = QgsVectorLayer("/home/zeito/pyqgis_data/overlapped_features.shp","self_intersections","ogr")
output = "/home/zeito/pyqgis_data/intersection.shp"
After running above code, result obtained was:
In attributes table was created a field (named ID) where it was placed all possible intersections for primary features.