Skip to content

pycensus.census.worldwide.worldpop.h3

WorldPop-raster-specific application of geohierarchy's area-weighted raster->H3 resampler.

This module is the "apply generic method to WorldPop specifically" half of the area-weighted raster resampling work (the generic, WorldPop-agnostic primitive lives in :mod:geohierarchy.raster_resample, this module just adds WorldPop's own known quirks on top -- nodata handling, degree-vs-metric CRS, and clipping-to-AOI):

  • WorldPop rasters are shipped in EPSG:4326 (degrees), so pixel area (and therefore area-weighting) is not metric there -- see :func:worldpop_raster_to_h3's docstring for how this is handled (the H3 grid this resamples onto is itself built/consumed in EPSG:4326, same as :func:geohierarchy.utils.h3_cells, and the underlying :func:geohierarchy.raster_resample.raster_to_polygons primitive is reprojection-agnostic: it just needs a shared CRS between the raster and the target polygons for its area-ratio math to be self-consistent, which holds even in degrees since both sides -- pixel bbox area and H3 cell polygon area -- are measured in the exact same units and get divided against each other, canceling out the degrees-vs-m^2 distortion for the split fraction even though absolute densities in degrees are not physically meaningful on their own).
  • WorldPop's real nodata sentinel (commonly -99999, but read from the file itself rather than assumed) marks unsettled/off-country land and must be excluded, not treated as a real 0.

This replaces the previous naive centroid-based approach for WorldPop specifically (nearest-H3-cell-to-pixel-center, with no area splitting -- see pycensus.raster_to_h3's resample_grid_to_h3, and UrbanAccessAnalyzer.h3_ops.from_raster_centroid, both of which remain in place as the general-purpose/other-source-shaped resamplers but are no longer the right tool for WorldPop given this exact, mass-conserving alternative now exists).

worldpop_raster_to_h3(tif_path, resolution, value_col='population', h3_col='h3_cell', tile_resolution=5, max_workers=None)

Area-weighted, mass-conserving resample of a WorldPop GeoTIFF onto an H3 grid.

Delegates to :func:geohierarchy.raster_resample.raster_to_h3_tiled (not the older single-shot :func:~geohierarchy.raster_resample.raster_to_h3) so the source GeoTIFF is never read into memory in one piece -- each H3-resolution-5 tile's worker opens tif_path itself and reads only its own small windowed slice. This is what avoids the OOM kills seen live on Boston/Beersheba (2026-08-30, killed past a 20GB cgroup cap during this exact join) without needing a larger memory budget: peak memory is bounded by one tile, not by the whole city's raster + H3 grid + spatial index all at once.

Parameters:

Name Type Description Default
tif_path str

Path to a (optionally AOI-clipped, e.g. via :func:pycensus.census.worldwide.worldpop.loader._clip_to_aoi) single-band WorldPop GeoTIFF.

required
resolution int

Target H3 resolution.

required
value_col str

Name to give the output value column (e.g. "population", "malePopulation", whatever :func:~pycensus.census.worldwide.worldpop.loader.download_worldpop_layer feature this raster represents).

'population'
h3_col str

Name to give the output H3 cell-id column.

'h3_cell'
tile_resolution int

Coarser H3 resolution partitioning the work into independent, memory-bounded chunks (default 5 -- see raster_to_h3_tiled's docstring for why this resolution specifically gives an exact, non-overlapping partition).

5
max_workers Optional[int]

Worker process count (default: one process per CPU core). Pass 1 for a small AOI where process-pool overhead would dominate.

None

Returns:

Type Description
DataFrame

Polars DataFrame with h3_col and value_col -- one row per

DataFrame

H3 cell touched by at least one valid (non-nodata) pixel. Every

DataFrame

such cell is present (no silent drops), and

DataFrame

sum(value_col) exactly equals the sum of the raster's valid

DataFrame

pixel values within the H3 grid's covered extent (see

DataFrame

func:geohierarchy.raster_resample.raster_to_polygons for the

DataFrame

conservation guarantee this is built on).

worldpop_raster_to_h3_fast(tif_path, resolution, value_col='population', h3_col='h3_cell', is_count=True)

Fast (point-based, not exact-overlay) resample of a WorldPop GeoTIFF onto an H3 grid.

2026-09-04, explicit user spec: replaces the expensive per-pixel polygon-overlay worldpop_raster_to_h3 above (real geometric intersection, ~65-70s on even a small metro's population raster at H3 res 11) with a much faster point/pixel-based method -- geohierarchy.raster_resample.raster_to_h3_auto (see its docstring for the full algorithm and correctness argument: density conversion + nearest-cell mean, OR H3-centroid raster sampling when H3 cells are smaller than the raster's own pixels, either way followed by a global mass-conservation rescale). Benchmarked live on Concepcion's real (AOI-masked) population raster: ~270x faster at H3 res 9 (0.07s vs 64s) and still much faster at the harder res-11 case (~4s vs 65-70s, after fixing an initial rasterio.sample() per-point bottleneck), with totals matching the exact method to within ~0.07% (floating-point + approximation noise, not a systematic bias -- the global rescale step keeps the total exact by construction).

Kept as a SEPARATE function from worldpop_raster_to_h3 (not a replacement) so that function's own dedicated regression tests (test_worldpop_h3.py's exact-conservation and pixel-level density-bound assertions, which specifically characterize the exact overlay method's behavior) stay meaningful and continue to pass unchanged. Real usage (city_science_network's h3_population.py) calls this fast function instead.

Parameters:

Name Type Description Default
tif_path str

Path to a (optionally AOI-clipped/masked) single-band WorldPop GeoTIFF.

required
resolution int

Target H3 resolution.

required
value_col str

Output value column name.

'population'
h3_col str

Output H3 cell-id column name.

'h3_cell'
is_count bool

Whether the raster holds an absolute count (True, the WorldPop population default) or an already-relative/ density quantity (False).

True

Returns:

Type Description
DataFrame

Polars DataFrame with h3_col and value_col.