Skip to content

Instantly share code, notes, and snippets.

View lptorres's full-sized avatar

Luis Torres lptorres

  • Philippines
View GitHub Profile
@lptorres
lptorres / multi2single.py
Created December 4, 2013 01:55
A simple script that converts shapefiles with multipart polygons into singlepart polygons, and copies the fields of the source shapefile. This script is based on a script by Paolo Corti, found here: https://gist.github.com/anonymous/735330 The original script only converts multipolygons by creating a new feature for each part of a multipolygon. …
import os
from osgeo import gdal, ogr, osr
import sys
def initFields(in_lyr, out_lyr):
#Arbitrarily get the first feature
feat = in_lyr[0]
#loop over each field
@lptorres
lptorres / multi2single_inplace.py
Last active September 14, 2021 06:16
This script is similar to multi2single.py, but it converts the shapefile in place, instead of creating a new shapefile.
from osgeo import gdal, ogr
import sys
def multipoly2poly(layer):
for feature in layer:
fid = feature.GetFID()
geom = feature.GetGeometryRef()
if geom.GetGeometryName() == 'MULTIPOLYGON':