Skip to content

Instantly share code, notes, and snippets.

@allenday
Last active March 24, 2021 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save allenday/7f2441b92771a9124a4d512fa024aa61 to your computer and use it in GitHub Desktop.
Save allenday/7f2441b92771a9124a4d512fa024aa61 to your computer and use it in GitHub Desktop.
#!/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