Created
February 28, 2019 03:47
-
-
Save ezheidtmann/4356c732375f35a2e266f93df518c1c1 to your computer and use it in GitHub Desktop.
Remove third and greater coordinates from geojson
This file contains hidden or 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
#!/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