Skip to content

Instantly share code, notes, and snippets.

@mapmeld
Created December 29, 2015 06:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mapmeld/8742ae89c6d687171d00 to your computer and use it in GitHub Desktop.
Save mapmeld/8742ae89c6d687171d00 to your computer and use it in GitHub Desktop.
Split a GeoJSON MultiPolygon FeatureCollection into GeoJSON Polygons
# split-multi.py
# open source, MIT license
import json
js = open('multipolygon.geojson', 'r').read()
gj = json.loads(js)
output = { "type": "FeatureCollection", "features": [] }
for feature in gj['features']:
if (feature['geometry'] is not None) and (feature['geometry']['type'] == 'MultiPolygon'):
for poly in feature['geometry']['coordinates']:
xfeature = { "type": "Feature", "properties": {}, "geometry": { "type": "Polygon" } }
xfeature['geometry']['coordinates'] = poly
output['features'].append(xfeature)
open('polygons.geojson', 'w').write(json.dumps(output))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment