Created
March 6, 2020 14:56
-
-
Save la-mar/9a916ca6f90d386ed4329e96c0b03c74 to your computer and use it in GitHub Desktop.
Export hex grids from Uber's H3 using Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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