Skip to content

Instantly share code, notes, and snippets.

@danvk
Created August 18, 2014 19:10
Show Gist options
  • Save danvk/57c3164d3c65a4f7a4f6 to your computer and use it in GitHub Desktop.
Save danvk/57c3164d3c65a4f7a4f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''Removes all non-polygon features from a GeoJSON file.
For now, these must be FeatureCollections of Features.
'''
import sys
import json
assert len(sys.argv) == 2
d = json.load(file(sys.argv[1]))
assert d['type'] == 'FeatureCollection'
indices_to_delete = []
for idx, feat in enumerate(d['features']):
assert feat['type'] == 'Feature'
if feat['geometry']['type'] not in ['Polygon', 'MultiPolygon']:
indices_to_delete.append(idx)
for idx in reversed(indices_to_delete):
del d['features'][idx]
print json.dumps(d, indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment