Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created April 28, 2019 18:00
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ThomasG77/a4779b61bb45d1424d933dacc5cdbddc to your computer and use it in GitHub Desktop.
Take a GeoJSON as an input, take all H3 hexagons within and output GeoJSON hexagons with their id
from h3 import h3
geoJson = {'type': 'Polygon',
'coordinates': [[[37.813318999983238, -122.4089866999972145],
[ 37.7866302000007224, -122.3805436999997056 ],
[37.7198061999978478, -122.3544736999993603],
[ 37.7076131999975672, -122.5123436999983966 ],
[37.7835871999971715, -122.5247187000021967],
[37.8151571999998453, -122.4798767000009008]]] }
hexagons = h3.polyfill(geoJson, 8)
print(h3.h3_to_geo_boundary(list(hexagons)[0], geo_json=True))
geojson_out = {
"type": "FeatureCollection",
"features": []
}
for i in hexagons:
geojson_out["features"].append({
"type": "Feature",
"properties": {
"identifier": i
},
"geometry": {
"type": "Polygon",
"coordinates": [
h3.h3_to_geo_boundary(i, geo_json=True)
]
}
})
with open("/tmp/output.geojson", "w") as outfile:
json.dump(geojson_out, outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment