Skip to content

Instantly share code, notes, and snippets.

@leoluk
Created August 11, 2021 16:56
Show Gist options
  • Save leoluk/cbd4bc155d70d2791c6f0051fe40e4f7 to your computer and use it in GitHub Desktop.
Save leoluk/cbd4bc155d70d2791c6f0051fe40e4f7 to your computer and use it in GitHub Desktop.
Converts geojson with polygon features to a JSON blob readable by http://shadowcalculator.eu
#!/usr/bin/env python3
"""
Converts geojson with polygon features to a JSON blob readable by http://shadowcalculator.eu
Usage: ./shadowcalc.py trees.geojson
"""
import sys
import json
import time
data = json.load(open(sys.argv[1]))
shapes = []
for feature in data['features']:
if feature['geometry']['type'] != 'Polygon':
continue
coords = []
heights = []
for coord in feature['geometry']['coordinates'][0]:
coords.append({'lat': coord[1], 'lng': coord[0]})
heights.append(8)
shapes.append({'coords': coords, 'heights': heights})
print(json.dumps({
'shapes': shapes,
'timestamp': int(time.time() * 1000),
'mapCenterLat': coords[0]['lat'],
'mapCenterLng': coords[0]['lng'],
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment