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: |
required |
date
|
Union[date, datetime, int]
|
A date/datetime, or a bare year as |
required |
feature
|
str
|
One of :data: |
required |
folder
|
Optional[str]
|
Directory to save/derive files into. Defaults to a system temp directory. |
None
|
overwrite
|
bool
|
If |
False
|
resolution
|
str
|
|
'100m'
|
Returns:
| Type | Description |
|---|---|
str
|
Path to a single-band GeoTIFF for the requested feature. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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: |
required |
folder
|
Optional[str]
|
Directory to save the downloaded file to. If |
None
|
overwrite
|
bool
|
If |
False
|
resolution
|
str
|
Resolution of the WorldPop dataset, |
'100m'
|
dataset
|
str
|
WorldPop dataset family: |
'pop'
|
subset
|
str
|
WorldPop dataset subset code (e.g. |
'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: |
required |
resolution
|
int
|
Target H3 resolution. |
required |
value_col
|
str
|
Name to give the output value column (e.g.
|
'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
|
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 |
DataFrame
|
H3 cell touched by at least one valid (non-nodata) pixel. Every |
DataFrame
|
such cell is present (no silent drops), and |
DataFrame
|
|
DataFrame
|
pixel values within the H3 grid's covered extent (see |
DataFrame
|
func: |
DataFrame
|
conservation guarantee this is built on). |