More

Exploding multipart features in qgis using python

Exploding multipart features in qgis using python


I want to explode a multipart layer to singlepart in python. I see through the qgis gui there is a way to do it under vector--geometry--tools--Multipart to Singleparts… which does what I need, I just need the python code for this. I'm new to qgis.


A multipart layer is a simple list of singlepart layers:

You can use:

1) Fiona and Shapely

from shapely.geometry import shape import fiona multis = fiona.open("multipolygons.shp") # first feature of the shapefile first = multis.next() print first {'geometry': {'type': 'MultiPolygon', 'coordinates': [[[(244697.45179524383, 1000369.2307574936), (244827.15493968062, 1000373.0455558595), (244933.96929392271, 1000353.9715640305), (244933.96929392271, 1000353.9715640305), (244930.15449555693, 1000147.9724522779), (244697.45179524383, 1000159.4168473752), (244697.45179524383, 1000369.2307574936)]], [[(246082.22360202507, 1000453.1563215409), (246139.44557751188, 1000460.7859182726), (246189.03795626713, 1000403.5639427857), (246189.03795626713, 1000403.5639427857), (246086.03840039085, 1000132.7132588148), (245990.66844124615, 1000205.1944277647), (246082.22360202507, 1000453.1563215409)]]]}, 'type': 'Feature', 'id': '0', 'properties': OrderedDict([(u'id', 1)])} # transform to shapely geometry multipol = shape(first['geometry'])

iterate through the geometries of multipol

for poly in multipol: print poly POLYGON ((244697.4517952438 1000369.230757494, 244827.1549396806 1000373.045555859, 244933.9692939227 1000353.971564031, 244933.9692939227 1000353.971564031, 244930.1544955569 1000147.972452278, 244697.4517952438 1000159.416847375, 244697.4517952438 1000369.230757494)) POLYGON ((246082.2236020251 1000453.156321541, 246139.4455775119 1000460.785918273, 246189.0379562671 1000403.563942786, 246189.0379562671 1000403.563942786, 246086.0384003909 1000132.713258815, 245990.6684412461 1000205.194427765, 246082.2236020251 1000453.156321541))

2) PyQGIS in the console

layer = qgis.utils.iface.activeLayer() # first feature of the shapefile first = layer.getFeatures().next() # the geometry multipol = first.geometry()

iterate through the geometries of the Multipolygon

for poly in multipol.asMultiPolygon(): print poly [[(244697.45179524383, 1000369.2307574936), (244827.15493968062, 1000373.0455558595), (244933.96929392271, 1000353.9715640305), (244933.96929392271, 1000353.9715640305), (244930.15449555693, 1000147.9724522779), (244697.45179524383, 1000159.4168473752), (244697.45179524383, 1000369.2307574936)]] [[(246082.22360202507, 1000453.1563215409), (246139.44557751188, 1000460.7859182726), (246189.03795626713, 1000403.5639427857), (246189.03795626713, 1000403.5639427857), (246086.03840039085, 1000132.7132588148), (245990.66844124615, 1000205.1944277647), (246082.22360202507, 1000453.1563215409)]]

And

print QgsGeometry.fromPolygon(poly).exportToWkt() POLYGON((246082.22360202507115901 1000453.15632154094055295,246139.44557751188403927 1000460.78591827256605029,246189.03795626712962985 1000403.5639427857240662,246189.03795626712962985 1000403.5639427857240662,246086.03840039085480385 1000132.71325881476514041,245990.66844124614726752 1000205.19442776474170387,246082.22360202507115901 1000453.15632154094055295))

And it is is easy to create a new shapefile (Fiona, PyQGIS) or a layer (PyQGIS) with these results

3) or the PyQGIS processing module as in Run ftools multiparts to singleparts from PYQGIS'


Watch the video: How to Merge Multiple Polygons:Features Into Single Polygon:Feature Using QGIS