Skip to content

transitlos.neighbourhood

Neighbourhood (buffer) statistics of H3 grids.

density_within(cells, values, radius_m=1000): for every H3 cell, the sum of values over the grid's cells whose centres lie within radius_m of its centre, divided by those cells' total area (per km2). The grid is rasterised (cell centres -> pixels of pixel_m) and convolved with a disk, tile by tile with a halo, so a whole US state at resolution 11 fits in memory.

density_within(cells, values, radius_m=1000.0, pixel_m=50.0, tile_px=4096)

Per cell: sum(values) / sum(cell area km2) over the grid cells whose centres are within radius_m (the cell itself included).

Parameters:

Name Type Description Default
cells

H3 cell ids.

required
values

Value per cell (residents).

required
radius_m float

Radius (m).

1000.0
pixel_m float

Raster pixel size (m) of the computation.

50.0
tile_px int

Tile size in pixels.

4096

Returns:

Type Description
ndarray

Density per cell (per km2).

density_within_multi(cells, values, radii, pixel_m=50.0, tile_px=4096)

density_within for several radii, the cell centres and areas computed once: a list of arrays in the order of radii.

Parameters:

Name Type Description Default
cells

H3 cell ids.

required
values

Value per cell.

required
radii

Radii (m).

required
pixel_m float

Raster pixel size (m).

50.0
tile_px int

Tile size in pixels.

4096

add_densities(df, cell_col='h3', pop_col='population')

df with every DENSITY_RADII column (residents per km2 within 500 m / 1 / 2 / 3 / 4 / 5 km); unchanged without the two columns.

Parameters:

Name Type Description Default
df

Table of cells.

required
cell_col str

H3 id column.

'h3'
pop_col str

Residents column.

'population'

add_density_1km(df, cell_col='h3', pop_col='population', out_col='pop_density_1km')

df with out_col = residents per km2 within 1 km of each cell (see density_within); unchanged without the two columns.

Parameters:

Name Type Description Default
df

Table of cells.

required
cell_col str

H3 id column.

'h3'
pop_col str

Residents column.

'population'
out_col str

Output column.

'pop_density_1km'

densities_to_polygons(polys, cells, cols=DENSITY_COLUMNS, cell_col='h3', pop_col='population')

polys with each of cols = residents-weighted mean of that cell column over the H3 cells whose centre lies in each polygon (the densities its residents live in); NaN where no populated cell centre falls inside. One spatial join for all columns.

Parameters:

Name Type Description Default
polys

Polygons.

required
cells

Cells with the density columns.

required
cols

Density columns.

DENSITY_COLUMNS
cell_col str

H3 id column.

'h3'
pop_col str

Residents column.

'population'

density_1km_to_polygons(polys, cells, cell_col='h3', pop_col='population', dens_col='pop_density_1km', out_col='pop_density_1km')

polys (GeoDataFrame) with out_col = residents-weighted mean of dens_col over the H3 cells whose centre lies in each polygon (the 1 km density the polygon's residents live in); NaN where no populated cell centre falls inside.

Parameters:

Name Type Description Default
polys

Polygons.

required
cells

Cells with the density column.

required
cell_col str

H3 id column.

'h3'
pop_col str

Residents column.

'population'
dens_col str

Density column of the cells.

'pop_density_1km'
out_col str

Output column.

'pop_density_1km'

nearest_cell_density(polys, cells, cell_col='h3', dens_col='pop_density_1km')

For each polygon: dens_col of the populated cell whose centre is nearest to the polygon's representative point (a list of columns: a 2-D array, one column each).

Parameters:

Name Type Description Default
polys

Polygons.

required
cells

Cells with the density column(s).

required
cell_col str

H3 id column.

'h3'
dens_col

Density column or list of columns.

'pop_density_1km'