Created
December 29, 2015 06:11
-
-
Save mapmeld/8742ae89c6d687171d00 to your computer and use it in GitHub Desktop.
Split a GeoJSON MultiPolygon FeatureCollection into GeoJSON Polygons
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
# 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