Skip to content

Instantly share code, notes, and snippets.

@la-mar
Created March 6, 2020 14:56
Show Gist options
  • Save la-mar/9a916ca6f90d386ed4329e96c0b03c74 to your computer and use it in GitHub Desktop.
Save la-mar/9a916ca6f90d386ed4329e96c0b03c74 to your computer and use it in GitHub Desktop.
Export hex grids from Uber's H3 using Python
from h3 import h3
import geojson
from geojson import Feature, Polygon, FeatureCollection
from pathlib import Path
export_path = Path(".")
bbox = {
"type": "Polygon",
"coordinates": [
[
[-106.64564605, 25.83705992],
[-93.50782176, 25.83705992],
[-93.50782176, 36.50045285],
[-106.64564605, 36.50045285],
[-106.64564605, 25.83705992],
]
],
}
for i in range(1, 17):
print(f"Generating resolution: {i}")
result = h3.polyfill(bbox, i, True)
coords = [h3.h3_to_geo_boundary(x, True) for x in result]
features = [Feature(id=idx, geometry=Polygon([x])) for idx, x in enumerate(coords)]
feature_count = len(features)
fc = FeatureCollection(features)
filename = f"h3_res{i}.geojson"
export_path.joinpath(filename).write_text(geojson.dumps(fc))
print(f"Output written to {filename}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment