Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python3
# See: https://medium.com/@lakshmanok/how-to-load-geojson-files-into-bigquery-gis-9dc009802fb4
# Processes output from: https://code.earthengine.google.com/49115c966c4c83a3ca7ac52bd5aba4f6
import json
from geolib import geohash
# check the accuracy
# PostGIS check from https://postgis.net/docs/ST_GeoHash.html
assert (geohash.encode(48, -126, 20) == 'c0w3hf1s70w3hf1s70w3')
# Google BigQuery check from https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_geohash
assert (geohash.encode(47.62, -122.35, 10) == 'c22yzugqw7')
with open('worldpopfull100k.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']
props['geometry'] = json.dumps({
'type': 'Polygon',
'coordinates': obj['geometry']['coordinates'][0]
})
(lon, lat) = props['point']
props['geohash'] = geohash.encode(lat, lon, 10)
del props['point']
print(props)
print('Schema: ', schema)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment