Last active
December 31, 2019 14:58
-
-
Save remi-dupre/6c4a1d699e48c00e134657dc1164a2c9 to your computer and use it in GitHub Desktop.
This file contains 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 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