Skip to content

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. 100.0 for a 100m x 100m grid, 1000.0 for 1km).

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 (the default), only resolutions whose average area is >= the source cell's own area are considered -- i.e. this never claims more spatial precision than the raw data actually has (several source cells could still share one H3 cell, but never the reverse). If True, every resolution in range is eligible and the single closest average-area match wins, even if that H3 cell is smaller than one source cell (meaning many H3 cells end up sharing the same source value) -- useful for a coarse source (e.g. 1km grids) where the caller wants finer output resolution and accepts that the extra H3 cells don't carry extra real information.

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 value_columns and a geometry. Any CRS (reprojected internally to the same metric CRS used for the nearest-join, matching the source grid's or estimated UTM zone).

required
resolution Optional[int]

H3 resolution to resample onto. If omitted, derived from cell_size_m via :func:best_h3_resolution_for_cell_size.

None
cell_size_m Optional[float]

Native cell edge length in meters, required if resolution is omitted.

None
value_columns tuple[str, ...]

Numeric columns to aggregate per H3 cell.

('population',)
agg str

Aggregation to apply per H3 cell -- "sum" for additive counts like population (the default, and the only one exercised by the real Zensus-grid use case), "mean" for intensive quantities.

'sum'
allow_finer bool

Passed through to :func:best_h3_resolution_for_cell_size when resolution is derived from cell_size_m.

False

Returns:

Type Description
GeoDataFrame

A GeoDataFrame with one row per non-empty H3 cell:

GeoDataFrame

h3_cell (str), each of value_columns (aggregated), resolution

GeoDataFrame

(int, constant), and geometry (the H3 cell's own polygon, EPSG:4326).

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.