pycensus.raster_to_h3¶
Resample a raster/grid-cell source (points or small polygons) onto H3 cells.
Technical aspects
Several real population sources in this codebase are shaped like a
raster grid rather than an administrative polygon hierarchy -- e.g.
:mod:pycensus.census.worldwide.worldpop (continuous raster) and
:mod:pycensus.census.countries.germany.zensus_grid (a real 100m x 100m
Destatis Zensus 2022 CSV grid). Both of those modules' own docstrings
say a caller should resample their output onto H3 (or any other target
geometry) itself -- this module is that shared resampler, so the same
logic isn't reimplemented ad hoc per source (as
CS_transitLOS.code.pipeline._join_zensus_grid_population originally
did for Germany, joining straight onto the pipeline's existing H3
grid rather than onto a purpose-picked resolution).
Resolution selection (:func:best_h3_resolution_for_cell_size) is
driven entirely by H3's own real average-hexagon-area table
(h3.average_hexagon_area, not a hardcoded/guessed table), picking
the resolution whose average cell area is the closest match to the
source's native cell size without going coarser than the source data
(i.e. never picks a resolution whose average area exceeds the source
cell's own area, so resampling never claims spatial precision the raw
data doesn't have). For a 100m x 100m grid (0.01 km^2/cell) this lands
on H3 res 10 (~0.0150 km^2/cell, the closest real match still >= the
source cell size -- res 11's ~0.00218 km^2 is smaller/finer than the
source data actually resolves, and res 9's ~0.105 km^2 is 10x coarser).
For a 1km x 1km grid (1 km^2/cell) this lands on H3 res 7 (~5.16 km^2)
under the "never go finer than the source" rule as originally stated,
but the real tradeoff is documented and a caller wanting the finer
res-8 option (~0.737 km^2, technically finer than one source cell, so
several H3 cells share one raster value rather than 1:1) can pass
allow_finer=True -- see that function's docstring for the exact
reasoning and when each makes sense.
Aggregation approach (point-in-nearest-H3-cell, not area-weighted split):
Each source cell is reduced to its own centroid and assigned to the H3
cell containing that point (falling back to the nearest H3 cell for
the rare centroid that lands just outside every candidate, e.g. right
at the edge of the requested AOI clip -- mirroring
CS_transitLOS.code.pipeline._join_zensus_grid_population's existing
sjoin_nearest fallback). A full area-weighted split (like
_join_polygon_stats/_interpolate_acs_to_dhc_blocks do for
coarse administrative polygons straddling many H3 cells) is NOT used
here on purpose: by construction (:func:best_h3_resolution_for_cell_size
picks the H3 resolution to be at or below the source cell's own
footprint), a single source cell almost never spans more than one
target H3 cell's interior -- it is comparable in size to, or smaller
than, the H3 cell that receives it. Area-weighting a cell against a
same-order-of-magnitude target would add real computational cost
(a polygon overlay per cell instead of a point join) for a negligible
accuracy gain over a straight nearest-centroid assignment; centroid
assignment also exactly conserves the source total under a sum
aggregation (every source cell contributes to exactly one H3 cell, so
nothing is dropped or double-counted -- see
:func:resample_grid_to_h3's own docstring and
tests/test_raster_to_h3.py for a live no-drop/no-duplicate check).
best_h3_resolution_for_cell_size(cell_size_m, min_res=0, max_res=15, allow_finer=False)
¶
Pick the H3 resolution whose average cell area best matches a raster's native cell size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cell_size_m
|
float
|
The raster/grid's native cell edge length in meters
(e.g. |
required |
min_res
|
int
|
Lowest (coarsest) H3 resolution to consider (real H3 range: 0-15). |
0
|
max_res
|
int
|
Highest (finest) H3 resolution to consider (real H3 range: 0-15). |
15
|
allow_finer
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
int
|
The best-matching H3 resolution (int). |
resample_grid_to_h3(grid, resolution=None, cell_size_m=None, value_columns=('population',), agg='sum', allow_finer=False)
¶
Resample a raster/grid-cell GeoDataFrame onto H3 cells at a matching resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
GeoDataFrame
|
GeoDataFrame of raster/grid cells (points or small polygons),
each carrying |
required |
resolution
|
Optional[int]
|
H3 resolution to resample onto. If omitted, derived from
|
None
|
cell_size_m
|
Optional[float]
|
Native cell edge length in meters, required if
|
None
|
value_columns
|
tuple[str, ...]
|
Numeric columns to aggregate per H3 cell. |
('population',)
|
agg
|
str
|
Aggregation to apply per H3 cell -- |
'sum'
|
allow_finer
|
bool
|
Passed through to :func: |
False
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
A GeoDataFrame with one row per non-empty H3 cell: |
GeoDataFrame
|
|
GeoDataFrame
|
(int, constant), and |
Correctness (see module docstring for the full reasoning):
Every input row contributes to exactly one H3 cell (its centroid's
containing cell, or nearest if the centroid falls just outside every
H3 cell -- can't happen for cells derived from h3.cells_to_h3shape-style
coverage, but is handled defensively for arbitrary input grids). This
means a "sum" aggregation exactly conserves the input's total: no
cell is dropped, and no cell is counted toward more than one H3 cell.