Skip to content

UrbanAccessAnalyzer.population

Offer/demand level-of-service rasters for accessibility analysis.

Kept raster-based (not migrated to Polars): population and service-coverage data here is naturally gridded, and rasterio is the correct tool for reading, reprojecting, and smoothing grids. This module is the demand side of an offer/demand accessibility comparison -- :func:level_of_service combines a rasterized "offer" layer (e.g. computed from :mod:UrbanAccessAnalyzer.isochrones access scores) with a smoothed population-density "demand" layer to produce a per-pixel difference = demand - offer raster.

WorldPop raster download and country/subdivision resolution (previously download_worldpop_population and get_country_region in this module) have moved to :mod:pycensus.census.worldwide.worldpop -- install the census extra (pip install urbanaccessanalyzer[census]) to use them. They are re-exported (lazily) from this module for backwards compatibility.

ls_str_to_int(arr, ref_list)

Encode level-of-service labels as their index in ref_list (labels not in the list get len(ref_list)).

Parameters:

Name Type Description Default
arr

Array of labels.

required
ref_list

The labels from best to worst.

required

Returns:

Type Description

Integer array of the same shape.

ls_int_to_str(arr, ref_list)

Decode indices back to level-of-service labels (len(ref_list) becomes "").

Parameters:

Name Type Description Default
arr

Integer array from :func:ls_str_to_int.

required
ref_list

The labels from best to worst.

required

Returns:

Type Description

Array of labels.

level_of_service_difference(offer, demand, level_of_services)

How many levels the demand is above the offer (positive: the place needs more service than it has).

Parameters:

Name Type Description Default
offer

Level-of-service labels offered.

required
demand

Level-of-service labels the population density calls for.

required
level_of_services

The labels from best to worst.

required

Returns:

Type Description

Integer array: demand index minus offer index.

filter_population_by_streets(streets_gdf, population, street_buffer, aoi=None, transform=None, crs=None, min_population=0, scale=True, population_column='population')

Keep only the population that lives within street_buffer metres of a street, rescaled to the original total.

Parameters:

Name Type Description Default
streets_gdf

Street lines.

required
population

Population raster path, array, or GeoDataFrame.

required
street_buffer

Distance from the streets, in metres.

required
aoi

Optional area to crop to.

None
transform

Affine transform (needed when population is an array).

None
crs

CRS (needed when population is an array).

None
min_population float

Pixels below this population are dropped.

0
scale bool

Rescale the kept population to the original total.

True
population_column

Population column when population is a GeoDataFrame.

'population'

Returns:

Type Description

The filtered population, in the same form as the input.

density(population_data, aoi=None, buffer=0, kernel_shape='disk', resolution=None, population_column=None, min_value=0, transform=None, crs=None, return_raster=True)

Population density (people per km2), optionally averaged within buffer metres of each pixel.

Parameters:

Name Type Description Default
population_data str | GeoDataFrame | ndarray

Population raster path, array, or GeoDataFrame.

required
aoi

Optional area to crop to.

None
buffer float

Averaging radius in metres (0: per pixel).

0
kernel_shape str

Shape of the averaging window ("disk" or "square").

'disk'
resolution float

Pixel size in metres when rasterising a GeoDataFrame.

None
population_column str

Population column when population_data is a GeoDataFrame.

None
min_value float

Pixels below this population get no density.

0
transform

Affine transform (needed for an array).

None
crs

CRS (needed for an array).

None
return_raster bool

Return the raster instead of polygons.

True

Returns:

Type Description

The density raster, or a GeoDataFrame with a pop_density column.

density_matrix_to_processing_order(density_matrix, level_of_services)

Order the (density, distance, level) rules of a density matrix so that better levels are written last and win.

Parameters:

Name Type Description Default
density_matrix

Table with a density column and one column per distance, holding the level required.

required
level_of_services

The labels from best to worst.

required

Returns:

Type Description

DataFrame with density, distance, ls in processing order.

level_of_service_raster(save_path, population, offer, density_matrix, level_of_services, min_population=0, polygons=None, aoi=None, transform=None, crs=None, resolution=100, street_buffer=50, level_of_service_column='level_of_service')

Write the offered, demanded and difference level-of-service rasters (GeoTIFF) to save_path.

Parameters:

Name Type Description Default
save_path

Output folder.

required
population

Population raster path, array, or GeoDataFrame.

required
offer

Service geometries (lines or polygons) with a level column.

required
density_matrix DataFrame

Density thresholds per distance and level (see :func:density_matrix_to_processing_order).

required
level_of_services list

The labels from best to worst.

required
min_population float

Pixels below this population are ignored.

0
polygons

Unused here (kept for symmetry with :func:level_of_service).

None
aoi

Optional area to crop to.

None
transform

Affine transform (needed for an array).

None
crs

CRS (needed for an array).

None
resolution

Output pixel size in metres.

100
street_buffer float

Buffer (m) around line geometries of the offer.

50
level_of_service_column str

Level column of offer.

'level_of_service'

Returns:

Type Description

None (files offer.tif, demand.tif and difference.tif are written).

level_of_service(population, offer, density_matrix, level_of_services, min_population=0, polygons=None, aoi=None, transform=None, crs=None, resolution=100, street_buffer=50, level_of_service_column='level_of_service')

Offered and demanded level of service for every populated pixel (or polygon), and their difference.

Parameters:

Name Type Description Default
population

Population raster path, array, or GeoDataFrame.

required
offer

Service geometries (lines or polygons) with a level column.

required
density_matrix DataFrame

Density thresholds per distance and level.

required
level_of_services list

The labels from best to worst.

required
min_population float

Places with this population or less are dropped.

0
polygons

Optional polygons to report on instead of pixels.

None
aoi

Optional area to crop to.

None
transform

Affine transform (needed for an array).

None
crs

CRS (needed for an array).

None
resolution

Pixel size in metres.

100
street_buffer float

Buffer (m) around the offer geometries.

50
level_of_service_column str

Level column of offer.

'level_of_service'

Returns:

Type Description

GeoDataFrame with population, densities per buffer, level_of_service_offer, level_of_service_demand and

level_of_service_difference.