Created
April 30, 2021 05:13
-
-
Save alpha-beta-soup/cf91feb77f242226adef96d3cc1658e3 to your computer and use it in GitHub Desktop.
Utility functions for converting from a row/column index of a raster window, to longitude/latitude or an H3 index. Useful for H3-indexing raster data read with rasterio.
This file contains 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
#!/usr/bin/env python3 | |
import pyproj | |
import h3.api.basic_int as h3 | |
# A window_transformation is the affine transformation of a window, see https://rasterio.readthedocs.io/en/latest/topics/windowed-rw.html?highlight=window_transform#window-transforms | |
# Transformer | |
transformer = lambda s_srs, t_srs: pyproj.Transformer.from_proj(pyproj.Proj(s_srs), pyproj.Proj(t_srs), always_xy=True) | |
# Row/column conversion to lon, lat; given a window transformation and pyproj.Transformer | |
rc2lonlat = lambda r, c, window_transformation, transformer: transformer.transform(*(window_transformation * (c, r))) | |
# Row/column conversion to h3 index; given a target H3 resolution, window transformation and pyproj.Transformer | |
rc2h3 = lambda r, c, res, window_transformation, transformer: h3.geo_to_h3(*rc2lonlat(r, c, window_transformation, transformer)[::-1], res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment