population
Population data retrieval and processing module for geographic analysis.
This module provides classes and functions for retrieving, processing, and analyzing population data from various sources such as Facebook’s Data for Good and WorldPop. It includes an abstract base class that defines the common interface, and concrete implementations for specific data sources.
The module supports retrieving population data within specified administrative boundaries, aggregating the data at different resolutions, and preparing it for accessibility analysis with facilities.
Examples
Retrieve and process population data from WorldPop:
>>> from pisa.administrative_area import AdministrativeArea
>>> from pisa.population import WorldpopPopulation
>>>
>>> # Get administrative area boundaries
>>> admin_area = AdministrativeArea("Timor-Leste", admin_level=1)
>>> boundaries = admin_area.get_admin_area_boundaries("Baucau")
>>> country_code = admin_area.get_iso3_country_code()
>>>
>>> # Create a population object and retrieve data
>>> population = WorldpopPopulation(
>>> admin_area_boundaries=boundaries,
>>> iso3_country_code=country_code
>>> )
>>>
>>> # Get processed population data as a GeoDataFrame
>>> population_gdf = population.get_population_gdf()
>>> print(f"Total population: {population_gdf['population'].sum()}")
See also
administrative_areaModule for retrieving administrative area boundaries
facilitiesModule for working with facility location data
Classes
Abstract base class for Population data retrieval and processing. |
|
Population data from Facebook's High Resolution Population Density Maps. |
|
Population data from WorldPop global population data. |
Module Contents
- class population.Population
Bases:
abc.ABCAbstract base class for Population data retrieval and processing.
This class provides the core functionality for retrieving and processing population data for a specified administrative area. Subclasses must implement the method get_population_data() to support different data sources.
- Parameters:
admin_area_boundaries (Polygon or MultiPolygon) – The geographical boundaries of the administrative area for which to retrieve population data
iso3_country_code (str) – The ISO3 country code for the administrative area (e.g.,
TLSfor Timor-Leste)population_resolution (int, optional) – The decimal precision to which latitude and longitude coordinates are rounded for aggregation purposes (default:
5)
- admin_area_boundaries: shapely.Polygon | shapely.MultiPolygon
- iso3_country_code: str
- population_resolution: int = 5
- get_population_gdf() geopandas.GeoDataFrame
Get aggregated population data for the administrative area as a GeoDataFrame.
This method integrates the population data retrieval workflow by:
Retrieving raw population data using the specific implementation of the get_population_data() method
Aggregating the population data based on the specified resolution
- Returns:
Aggregated population data with columns:
longitude: Rounded longitude coordinatelatitude: Rounded latitude coordinatepopulation: Total population at the coordinategeometry: Point geometry representing the coordinateID: Unique identifier for each point
- Return type:
geopandas.GeoDataFrame
- class population.FacebookPopulation
Bases:
PopulationPopulation data from Facebook’s High Resolution Population Density Maps.
This class retrieves and processes population data from Facebook’s Data for Good program, which provides high-resolution population density maps. The data is accessed via the Humanitarian Data Exchange (HDX) platform.
The class follows the same initialization pattern as its parent class Population.
See also
PopulationParent abstract class
WorldPopulationAlternative population data source implementation
- static download_population_facebook(iso3_country_code: str) pandas.DataFrame
Download Facebook population data for a specific country.
This method retrieves population data from Facebook’s High Resolution Population Density Maps via the Humanitarian Data Exchange (HDX) platform for a country specified by its ISO3 code.
- Parameters:
iso3_country_code (str) – The ISO3 country code for which to download population data (e.g.,
TLSfor Timor-Leste)- Returns:
Raw population data from Facebook for the specified country
- Return type:
pandas.DataFrame
- Raises:
Exception – If there are issues with the HDX configuration or data download
Notes
The method accesses the Facebook population dataset from the year 2020.
- static process_population_facebook(downloaded_data: pandas.DataFrame, iso3_country_code: str, admin_area_boundaries: shapely.Polygon | shapely.MultiPolygon) pandas.DataFrame
Process Facebook population data for a specific administrative area.
This method transforms raw Facebook population data into a filtered dataset that only includes points within the specified administrative area boundaries.
- Parameters:
downloaded_data (pandas.DataFrame) – Raw population data downloaded from Facebook/HDX
iso3_country_code (str) – ISO3 country code used to identify the population column in the data
admin_area_boundaries (Polygon or MultiPolygon) – The geographical boundaries used to clip the population data
- Returns:
Processed population data containing only points within the administrative area, with columns:
longitude: Longitude coordinatelatitude: Latitude coordinatepopulation: Population count (renamed from country-specific column)
- Return type:
pandas.DataFrame
- class population.WorldpopPopulation
Bases:
PopulationPopulation data from WorldPop global population data.
This class retrieves and processes population data from the WorldPop project, which provides high-resolution population density estimates globally. The data is accessed via the WorldPop REST API.
The class follows the same initialization pattern as its parent class Population.
See also
PopulationParent abstract class
FacebookPopulationAlternative population data source implementation
- static download_population_worldpop(iso3_country_code: str) str
Download population data from WorldPop for a specific country.
This method retrieves population data from the WorldPop REST API for a country specified by its ISO3 code, using the most recently available dataset.
- Parameters:
iso3_country_code (str) – The ISO3 country code for which to download population data (e.g.,
TLSfor Timor-Leste)- Returns:
File path to the downloaded raster data file
- Return type:
str
- Raises:
requests.exceptions.RequestException – If there are issues with the WorldPop API request
- static process_population_worldpop(file_path: str, admin_area_boundaries: shapely.Polygon | shapely.MultiPolygon) pandas.DataFrame
Process WorldPop raster data for a specific administrative area.
This method converts the downloaded WorldPop raster data into a DataFrame of point-based population values, filtered to include only points within the specified administrative area boundaries.
- Parameters:
file_path (str) – Path to the downloaded WorldPop raster file
admin_area_boundaries (Polygon or MultiPolygon) – The geographical boundaries used to filter the population data
- Returns:
Processed population data containing only points within the administrative area, with columns:
longitude: Longitude coordinatelatitude: Latitude coordinatepopulation: Population count
- Return type:
pandas.DataFrame
- static get_admarea_mask(vector_polygon: shapely.Polygon | shapely.MultiPolygon, raster_layer: rasterio.DatasetReader) numpy.ndarray
Create a boolean mask identifying raster pixels within a vector polygon.
This method creates a mask that can be used to filter raster data to include only points that fall within a specified vector polygon.
- Parameters:
vector_polygon (Polygon or MultiPolygon) – The vector geometry used to create the mask
raster_layer (rasterio.DatasetReader) – The open raster dataset to be masked
- Returns:
A boolean mask with the same dimensions as the raster, where:
True: pixel is within the polygonFalse: pixel is outside the polygon
- Return type:
np.ndarray
Notes
The method uses rasterio’s mask function with the all_touched=True parameter, which includes all pixels that are touched by the polygon, not just those whose centers are within it. It then creates a boolean mask by checking which pixels have values greater than zero.