Skip to content

pycensus.census.worldwide.worldpop

WorldPop gridded population source.

Importing this module registers the gdf.worldpop GeoDataFrame accessor (see :mod:pycensus.accessors). Use :func:download_worldpop_population to fetch the raw raster and :func:get_country_region to resolve a country (and optional subdivision) code from a lat/lon.

GLOBAL_SCHEMA_FEATURES = ('population', 'malePopulation', 'femalePopulation', 'under18Population', 'over65Population', 'adultPopulation', 'urbanPopulation') module-attribute

Feature names (matching global_schema.json) that :func:download_worldpop_layer supports.

births/pregnancies are deliberately NOT included here (2026-08-25): both are fixed at WorldPop's single 2015 vintage with no per-year files (see download_worldpop_layer's feature == "pregnancies" branch, which already warns and falls back to 2015 regardless of the requested year) -- real, live-verified data, but 11 years stale relative to this project's 2020-2026 data-recency requirement. download_worldpop_layer still implements them (harmless, real, dead code kept for a future project with a looser recency bar) but no caller should request them for this project. urbanPopulation (Degree of Urbanisation) stays: unlike births/ pregnancies, WorldPop genuinely publishes a real per-year DUG raster for 2015-2030 (live-verified 2026-08-25 -- Israel alone has 16 real per-year rows, ids 124025-124040, one per year 2015-2030), and _fetch_single_country_dataset_row's year-matching branch already selects the real row for whatever year is requested -- so a 2025 (or any 2020-2026) urbanPopulation fetch is real, current data, not a stale fallback.

download_worldpop_layer(aoi, date, feature, folder=None, overwrite=False, resolution='100m')

Download (or derive) a single global-schema-aligned WorldPop raster for an AOI and year.

This is the recommended entry point for anything beyond plain total population -- it hides WorldPop's dataset/subset/file-naming details behind the same logical feature names used across every other pycensus country module (see :data:GLOBAL_SCHEMA_FEATURES), and handles multi-file downloads/zip extraction/band-math internally.

Parameters:

Name Type Description Default
aoi GeoDataFrame

Area of interest. Must have a CRS set; its centroid resolves the country via reverse geocoding (see :func:get_country_region).

required
date Union[date, datetime, int]

A date/datetime, or a bare year as int (2015-2030).

required
feature str

One of :data:GLOBAL_SCHEMA_FEATURES -- "population", "malePopulation", "femalePopulation", "under18Population", "over65Population", "adultPopulation", "births", "pregnancies", or "urbanPopulation". See the module docstring for exactly how each is derived, and which are exact vs. approximated. "births"/"pregnancies" are only available for a subset of countries and are always the real 2015 dataset regardless of date (WorldPop never published another year for these two products -- see the module docstring); both raise ValueError for a country WorldPop has no data for.

required
folder Optional[str]

Directory to save/derive files into. Defaults to a system temp directory.

None
overwrite bool

If True, re-download/re-derive even if a cached file already exists.

False
resolution str

"100m" or "1km".

'100m'

Returns:

Type Description
str

Path to a single-band GeoTIFF for the requested feature.

Raises:

Type Description
ValueError

If feature isn't in :data:GLOBAL_SCHEMA_FEATURES, the AOI's country/year has no matching WorldPop dataset, or (for population) the year is outside WorldPop's 2015-2030 pop coverage.

download_worldpop_population(aoi, date, folder=None, overwrite=False, resolution='100m', dataset='pop', subset='wpgpunadj', chunk_size=1048576)

Download a WorldPop population (or age-structure) raster for an AOI and year.

Resolves the AOI's country via :func:get_country_region, queries the WorldPop REST API (https://hub.worldpop.org/rest/data/) for the matching dataset/subset/year, and streams the resulting GeoTIFF to disk. If the resolved file is a .zip (e.g. the age_structures/U18 subset), it is automatically extracted and the path to the .tif inside is returned -- callers never need to unzip it themselves.

For the global-schema-aligned features (population by sex, under-18, over-65, adult), prefer :func:download_worldpop_layer instead -- it's built on the same underlying data but returns exactly one of those logical features per call without the caller needing to know WorldPop's dataset/subset naming.

Parameters:

Name Type Description Default
aoi GeoDataFrame

Area of interest. Must have a CRS set (ideally EPSG:4326); its centroid is used to resolve the country via reverse geocoding.

required
date Union[date, datetime, int]

A :class:datetime.date/:class:datetime.datetime, or a bare year as int; only the year is used to query WorldPop.

required
folder Optional[str]

Directory to save the downloaded file to. If None (default), a system temporary directory is used. If "", the current working directory is used.

None
overwrite bool

If False (default) and the destination file already exists, skip the download and reuse it.

False
resolution str

Resolution of the WorldPop dataset, "100m" or "1km".

'100m'
dataset str

WorldPop dataset family: "pop" for population counts, or "age_structures" for age-structure rasters.

'pop'
subset str

WorldPop dataset subset code (e.g. "wpgpunadj"). If not applicable for the resolved year, it is overridden automatically (see WorldPop's dataset catalog at https://hub.worldpop.org/rest/data/ for all available datasets/subsets). For dataset="age_structures", pass "under_18"/"U18" to select the under-18 subset (only available for year 2015, see :func:download_worldpop_layer's docstring for a per-year alternative).

'wpgpunadj'
chunk_size int

Chunk size in bytes used when streaming the download.

1048576

Returns:

Type Description
str

Path to the downloaded (and, if applicable, extracted) GeoTIFF file.

Raises:

Type Description
ValueError

If the AOI has no CRS, its country cannot be resolved, or no matching WorldPop dataset exists for the requested year/dataset/subset combination.

HTTPError

If a WorldPop API or file-download request fails.

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).