pycensus.census.worldwide.worldpop.loader¶
Loader for WorldPop gridded population/age-sex-structure rasters.
Downloads WorldPop rasters (https://www.worldpop.org/, REST API:
https://www.worldpop.org/sdi/introapi) for a given area of interest and
year. Unlike pyCensus' vector/tabular sources (e.g. :mod:pycensus.usa.acs),
WorldPop ships gridded raster data, so every download_*/fetch_*
function here returns a filesystem path to a downloaded/derived GeoTIFF
rather than a GeoDataFrame -- callers that need a :class:geopandas.GeoDataFrame
(e.g. for :meth:geohierarchy.GeoHierarchy.add_level) are expected to
vectorize/sample the raster themselves and expose the result through the
worldpop accessor registered in :mod:pycensus.census.worldwide.worldpop.schema.
Technical aspects
WorldPop publishes population data across several REST-queryable dataset families (https://hub.worldpop.org/rest/data). Two are wired up here, both real, verified live 2026-08-20:
pop(subsetG2_CN_POP_R25A_{resolution}): a single total- population GeoTIFF per country/year, covering 2015-2030. This is :func:download_worldpop_population's default path.-
age_structures(subsetG2_CN_Age_R25A_{resolution}): the full age-and-sex-disaggregated breakdown per country/year, also covering 2015-2030 -- one GeoTIFF per (sex, 5-year age bin) plus two pre-aggregated total-by-sex files (T_M/T_F). Age bins are00(age 0),01(ages 1-4), then 5-year bins05,10,15, ...,90(90+). :func:download_worldpop_layerbuilds :data:GLOBAL_SCHEMA_FEATURESfrom this family: -
malePopulation/femalePopulation: theT_M/T_Ffiles directly -- no band math, exact (not an approximation). over65Population: sum of bins65..90-- exact, since WorldPop's bin boundaries happen to include 65.under18Population: sum of bins00,01,05,10(ages 0-14) plus 0.6x of bin15(ages 15-19), the linear- within-bin share that falls under 18 (ages 15-17 = 3 of the bin's 5 years). WorldPop's per-year age-sex product has no dataset with an exact 18-year cutoff for years other than 2015 (a one-off, separately-publishedunder_18/U18subset exists, but only for 2015 -- verified live, itspopyearis always"2015"regardless of the requested year). This is a documented linear- interpolation approximation, not an exact figure.adultPopulation: total population (sum of allt_XXbins) minusunder18Population-- inherits the same bin-15 approximation.
Note population (from the pop family) and
malePopulation+femalePopulation (from age_structures) come from
different WorldPop models and are not guaranteed to sum to
identical totals -- both are real, independently verified WorldPop
products, just not cross-consistent by construction.
Three more real WorldPop REST families are wired up as of 2026-08-25
(verified live via https://hub.worldpop.org/rest/data and its
per-dataset/per-country endpoints):
births(subsetbic= "Individual countries"): gridded estimated-live-births-per-pixel rasters, single.7z-compressed GeoTIFF per country. Unlikepop/age_structures, this is a 2015-only dataset (popyearis reported asnullby the API; every file is a fixed*_births_pp_v{1,2}_2015.tif) and does not cover every country -- confirmed live: Israel (ISR), Chile (CHL) and Mexico (MEX) have real data, but Spain (ESP), the USA and Germany (DEU) return an emptydata: [](WorldPop only ever published this product for a subset of, largely lower/middle-income, countries). :func:download_worldpop_layerraisesValueErrorfor an uncovered country rather than silently returning nothing. Live-verified real totals: Israel's 2015 raster sums to 166,214 estimated births (Israel's actual 2015 population was ~8.3M at a ~20/1000 crude birth rate -> ~166-174k expected, matching within ~5%); Chile's sums to 235,608 (2015 population ~17.9M at a ~13.7/1000 rate -> ~245k expected, matching within ~4%).pregnancies(subsetindividual_countries): the sibling gridded-pregnancies-per-pixel product (live births plus estimated stillbirths/miscarriages/abortions per the Guttmacher Institute), same 2015-only/.7z/partial-coverage shape asbirths. Live-verified: Israel's 2015 raster sums to 245,337 pregnancies -- a pregnancies:births ratio of ~1.48, in the expected ~1.4-1.6 range for a country with Israel's real abortion/miscarriage rates.dug(subsetdug_g2_v1= "Degree of Urbanisation 2015-2030 using WorldPop Global2 R2025A"): not a count raster -- verified live by downloading Israel's real 2015_GRID_L1GeoTIFF and inspecting its actual pixel values: it is auint8categorical settlement-grid classification with exactly the values{0 (nodata/water), 1 (rural grid cell), 2 (urban cluster grid cell), 3 (urban centre grid cell)}-- Israel's real 2015 L1 grid has 20,199 rural / 1,527 urban-cluster / 683 urban-centre land pixels, matching the published GHSL/DEGURBA Level-1 settlement classification standard (https://ghsl.jrc.ec.europa.eu/degurbaDefinitions.php), not a guess. A raw class code isn't itself a summable quantity like the rest of :data:GLOBAL_SCHEMA_FEATURES, so rather than exposing it as-is, :func:download_worldpop_layer's"urbanPopulation"feature multiplies this classification (reprojected/resampled nearest-neighbour onto the population raster's own grid -- DUG ships at 1km/Mollweide ESRI:54009, the population raster at the requestedresolution/WGS84) againstdownload_worldpop_population's real per-pixel population raster, keeping only pixels DUG classifies as urban cluster or urban centre (class 2 or 3). This is real, derived-but-not-fabricated population attributable to WorldPop's own real classification and real population products, both independently verified above.
Every downloaded/derived raster is cached on disk by
(iso3, year, resolution, file/feature) -- re-running with
the same arguments and overwrite=False reuses the cached file
instead of re-downloading/re-deriving.
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_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. |
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 |