Created
July 17, 2019 18:19
-
-
Save lakshmanok/4418ff447f01c705f64fc4299aa6c560 to your computer and use it in GitHub Desktop.
How to load GeoJSON files to BigQuery
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 | |
# See: https://medium.com/@lakshmanok/how-to-load-geojson-files-into-bigquery-gis-9dc009802fb4 | |
import json | |
with open('NUTS_BN_01M_2016_4326_LEVL_3.geojson', 'r') as ifp: | |
with open('to_load.json', 'w') as ofp: | |
features = json.load(ifp)['features'] | |
# new-line-separated JSON | |
schema = None | |
for obj in features: | |
props = obj['properties'] # a dictionary | |
props['geometry'] = json.dumps(obj['geometry']) # make the geometry a string | |
json.dump(props, fp=ofp) | |
print('', file=ofp) # newline | |
if schema is None: | |
schema = [] | |
for key, value in props.items(): | |
if key == 'geometry': | |
schema.append('geometry:GEOGRAPHY') | |
elif isinstance(value, str): | |
schema.append(key) | |
else: | |
schema.append('{}:{}'.format(key, | |
'int64' if isinstance(value, int) else 'float64')) | |
schema = ','.join(schema) | |
print('Schema: ', schema) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment