Skip to content

Instantly share code, notes, and snippets.

@ezheidtmann
Created February 28, 2019 03:47
Show Gist options
  • Save ezheidtmann/4356c732375f35a2e266f93df518c1c1 to your computer and use it in GitHub Desktop.
Save ezheidtmann/4356c732375f35a2e266f93df518c1c1 to your computer and use it in GitHub Desktop.
Remove third and greater coordinates from geojson
#!/usr/bin/env python
import json, sys
def _stripZ(coordinates):
if isinstance(coordinates, list) and not any([isinstance(el, list) for el in coordinates]) and len(coordinates) > 2:
return coordinates[:2]
else:
return [_stripZ(el) for el in coordinates]
def removeZCoordinatesFromGeoJSON(gj):
if 'coordinates' in gj:
gj['coordinates'] = _stripZ(gj['coordinates'])
elif 'geometry' in gj:
removeZCoordinatesFromGeoJSON(gj['geometry'])
elif 'geometries' in gj:
for geom in gj['geometries']:
removeZCoordinatesFromGeoJSON(geom)
elif 'features' in gj:
for feature in gj['features']:
removeZCoordinatesFromGeoJSON(feature)
if __name__ == '__main__':
gj = json.load(sys.stdin)
removeZCoordinatesFromGeoJSON(gj)
json.dump(gj, sys.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment