Try this:
# Count Overlapping Polygons by Sections
# Author: Ervin Wirth, 2016
from PyQt4.QtCore import QVariant
import itertools
filename1 = "buffer2"
path1="d:/Works/ChargingStation/data/test/"
layer1 = QgsVectorLayer(path1 + filename1 +".shp", filename1, "ogr")
save_layer = QgsVectorLayer("Polygon?crs=epsg:23700", "save_layer", "memory")
save_layer.dataProvider().addAttributes([QgsField("id", QVariant.Int), QgsField("k", QVariant.Int)])
save_layer.updateFields()
# create a recursive function
# arg1: input layer; arg2: level of section; arg3: threshold for sliver polygons
def iterlay(layer1, k, thres):
# create a memory layer for intersections
mem_layer = QgsVectorLayer("Polygon?crs=epsg:23700", "temp_layer", "memory")
mem_layer.dataProvider().addAttributes([QgsField("id", QVariant.Int), QgsField("k", QVariant.Int)])
mem_layer.updateFields()
id = mem_layer.fieldNameIndex('id')
# prepare a loop on the input layer
polygons = [feature for feature in layer1.getFeatures()]
list = range(len(polygons))
print len(polygons)
# create the sections in all combinations and store them temporary
for i,j in itertools.combinations(list, 2):
# if polygon has section, than intersecting
if polygons[i].geometry().intersects(polygons[j].geometry()):
geom = polygons[i].geometry().intersection(polygons[j].geometry())
# intersections can be multipolygons
if geom.asMultiPolygon():
for singlepoly in geom.asMultiPolygon():
qgisgeom = QgsGeometry.fromPolygon (singlepoly)
if qgisgeom.area() > thres:
feature = QgsFeature()
feature.setAttributes([str(k) + '-' + str(i) + '-' + str(j) ,str(k)])
feature.setGeometry(qgisgeom)
mem_layer.dataProvider().addFeatures([feature])
# and simple polygons
if geom.asPolygon():
qgisgeom = QgsGeometry.fromPolygon (geom.asPolygon())
if qgisgeom.area() > thres:
feature = QgsFeature()
feature.setAttributes([str(k) + '-' + str(i) + '-' + str(j) ,str(k)])
feature.setGeometry(qgisgeom)
mem_layer.dataProvider().addFeatures([feature])
print mem_layer.featureCount()
# remove duplicates from sections, and eliminate sliver polygons
sections = [feature for feature in mem_layer.getFeatures()]
list = range(len(sections))
dlist=[]
for i,j in itertools.combinations(list, 2):
# compare the shapes (maybe can be modified to equals())
if sections[i].geometry().difference(sections[j].geometry()).area() < thres and sections[j].geometry().difference(sections[i].geometry()).area() < thres:
dlist.append(sections[j].id())
if sections[i].geometry().area() < thres:
dlist.append(sections[i].id())
myset = set(dlist)
for dupl in myset:
mem_layer.startEditing()
mem_layer.deleteFeature(dupl)
mem_layer.commitChanges()
print mem_layer.featureCount()
# save the remained parts
sections = [feature for feature in mem_layer.getFeatures()]
for poly in polygons:
# have to create the differences with all the sections
tempgeom = poly.geometry()
for section in sections:
newgeom = tempgeom.difference(section.geometry())
tempgeom = newgeom
# if remained something, write out
if tempgeom.asPolygon():
qgisgeom = QgsGeometry.fromPolygon (tempgeom.asPolygon())
if qgisgeom.area() > thres:
feature = QgsFeature()
feature.setAttributes([str(poly.id()) ,str(k)])
feature.setGeometry(qgisgeom)
save_layer.dataProvider().addFeatures([feature])
if tempgeom.asMultiPolygon():
for singlepoly in tempgeom.asMultiPolygon():
qgisgeom = QgsGeometry.fromPolygon (singlepoly)
if qgisgeom.area() > thres:
feature = QgsFeature()
feature.setAttributes([str(poly.id()) ,str(k)])
feature.setGeometry(qgisgeom)
save_layer.dataProvider().addFeatures([feature])
# have to stop when there isn't any section
if mem_layer.featureCount() == 0:
#if k == 2:
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
QgsMapLayerRegistry.instance().addMapLayer(save_layer)
else:
k += 1
iterlay(mem_layer, k, thres)
# Call the funtion
iterlay(layer1,1,100000)