-
-
Save JayGoldberg/dec0cadfeb94a441ec608e9a23842178 to your computer and use it in GitHub Desktop.
How to load GeoJSON files to BigQuery (supports filename arg)
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 python3 | |
# See: https://medium.com/@lakshmanok/how-to-load-geojson-files-into-bigquery-gis-9dc009802fb4 | |
import json | |
import sys | |
def convert_geojson(input_file_path): | |
with open(input_file_path, 'r') as ifp: | |
with open('to_load.json', 'w') as ofp: | |
features = json.load(ifp)['features'] | |
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) | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print("Usage: python script.py input_geojson_file") | |
else: | |
input_geojson_file = sys.argv[1] | |
convert_geojson(input_geojson_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment