Skip to content

Instantly share code, notes, and snippets.

@lyleaf
Created October 2, 2019 06:10
Show Gist options
  • Save lyleaf/271e3524d664f03cf7c8dff99eef18d8 to your computer and use it in GitHub Desktop.
Save lyleaf/271e3524d664f03cf7c8dff99eef18d8 to your computer and use it in GitHub Desktop.
Convert geojson to get schema to upload to bigquery
import json
with open('HGURiau_EoF2019ATR2014Bappenas2011.json', 'r') as ifp:
with open('to_load.json', 'w') as ofp:
features = json.load(ifp)['features']
print(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