Created
December 9, 2010 21:02
-
-
Save anonymous/735330 to your computer and use it in GitHub Desktop.
grazie Paolo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys | |
from osgeo import ogr,gdal | |
def multipoly2poly(in_lyr, out_lyr): | |
for in_feat in in_lyr: | |
geom = in_feat.GetGeometryRef() | |
if geom.GetGeometryName() == 'MULTIPOLYGON': | |
for geom_part in geom: | |
addPolygon(geom_part.ExportToWkb(), out_lyr) | |
else: | |
addPolygon(geom.ExportToWkb(), out_lyr) | |
def addPolygon(simplePolygon, out_lyr): | |
featureDefn = out_lyr.GetLayerDefn() | |
polygon = ogr.CreateGeometryFromWkb(simplePolygon) | |
out_feat = ogr.Feature(featureDefn) | |
out_feat.SetGeometry(polygon) | |
out_lyr.CreateFeature(out_feat) | |
print 'Polygon added.' | |
gdal.UseExceptions() | |
driver = ogr.GetDriverByName('ESRI Shapefile') | |
in_ds = driver.Open('poly.shp', 0) | |
in_lyr = in_ds.GetLayer() | |
outputshp = 'polys.shp' | |
if os.path.exists(outputshp): | |
driver.DeleteDataSource(outputshp) | |
out_ds = driver.CreateDataSource(outputshp) | |
out_lyr = out_ds.CreateLayer('polys', geom_type=ogr.wkbPolygon) | |
multipoly2poly(in_lyr, out_lyr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment