Skip to content

Instantly share code, notes, and snippets.

@remi-dupre
Last active December 31, 2019 14:58
Show Gist options
  • Save remi-dupre/6c4a1d699e48c00e134657dc1164a2c9 to your computer and use it in GitHub Desktop.
Save remi-dupre/6c4a1d699e48c00e134657dc1164a2c9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import gzip
import json
import os
from shapely.geometry import mapping, shape
# File for admin data outputed by Cosmogony
COSMOGONY_DATA = 'admins.json.gz'
# Directory where to put output json
OUTPUT_DIR = 'output'
# Target admins to include in the output
TARGETS = {
'paris': {
'filter': {'admin_level': 4, 'name': 'Île-de-France'},
},
'lyon': {
'filter': {'admin_level': 6, 'name': 'Métropole de Lyon'},
}
}
if not os.path.exists(OUTPUT_DIR):
os.mkdir(OUTPUT_DIR)
def match(admin_data):
for name, meta in TARGETS.items():
if all(admin_data[key] == val for key, val in meta['filter'].items()):
return name
return None
with gzip.open(COSMOGONY_DATA) as f:
for data in map(json.loads, f):
matching_target = match(data)
if matching_target:
print("Found", matching_target)
geometry = shape(data['geometry'])
output_path = os.path.join(OUTPUT_DIR, matching_target + ".json")
with open(output_path, 'w') as f:
f.write(
json.dumps(
mapping(geometry.simplify(0.01)),
indent=4
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment