Skip to content

pycensus.dasymetric

Census polygons -> H3 cells, weighted by built floor area (the normal maps' population assignment).

from pycensus.dasymetric import polygons_to_cells
counts, report = polygons_to_cells(polygons, cells, count_cols, weights={"res": w_res, "non": w_non}, groups=col_group)

Rules (documented in transitlos.grid_rules): * a cell belongs to the one polygon holding its centre (never to two); * each polygon's counts are split over its cells by built floor area: the cells' floor areas are rescaled so they add up to 1 inside the polygon (only the split uses them; the cells' real floor areas stay as they are). Columns of group "res" (residents, households, age groups, ...) use residential floor area, group "non" (jobs) non-residential floor area; each falls back to the total floor area, then to an equal split; * a polygon holding no cell centre contributes nothing here: its counts are reported (report["unplaced"]) for the caller's area-wide rescaling. Every column of one group uses the same weights, so the shares between them (e.g. under-18 / residents) are the polygon's in every cell.

polygons_to_cells(polygons, cells, count_cols, weights=None, groups=None)

Counts per cell ([len(cells), n_cols] DataFrame, rows in cells order) and a report {column: {"total", "placed", "unplaced"}}.

cells: uint64 H3 ids (any resolution). weights: {"res": floor area per cell, "non": ...} aligned with cells (None: equal split). groups: column -> "res" | "non" | "all" (total floor area: res + non) | any other key of weights (default "res").

Parameters:

Name Type Description Default
polygons GeoDataFrame

Census polygons with count columns.

required
cells ndarray

uint64 H3 ids.

required
count_cols Iterable[str]

Count columns to split.

required
weights Optional[Dict[str, ndarray]]

{group: weight per cell}.

None
groups Optional[Dict[str, str]]

{column: weight group}.

None

rescale_to_totals(counts, keep, totals, groups, anchor, log=print, warn_share=0.05, label='', factors=None)

Drop the rows not in keep and put back what is missing (dropped rows + totals not placed at all) with ONE factor per group: the factor of the group's anchor column (anchor["res"] = e.g. "population", anchor["non"] = "jobs") is applied to every column of that group, so the shares inside a cell do not change. Logged per anchor, WARNING above warn_share. factors (a dict) receives, per group, the factor, the anchor's total and kept sum, and the columns it was applied to (original value = rescaled value / factor).

Parameters:

Name Type Description Default
counts DataFrame

Counts per cell.

required
keep ndarray

Boolean mask of the cells kept.

required
totals Dict[str, float]

{column: true total}.

required
groups Dict[str, str]

{column: group}.

required
anchor Dict[str, str]

{group: anchor column}.

required
log

Logging function.

print
warn_share float

Share rescaled above which a warning is logged.

0.05
label str

Text added to the log lines.

''
factors Optional[dict]

Optional dict that receives the factors.

None

Returns:

Type Description

The kept, rescaled counts.

polygon_values_at_cells(polygons, cells, cols)

Rate-style columns (means, medians, shares): each cell takes the value of the one polygon holding its centre (NaN outside every polygon). Rows in cells order.

Parameters:

Name Type Description Default
polygons GeoDataFrame

Polygons with rate columns.

required
cells ndarray

uint64 H3 ids.

required
cols Iterable[str]

Columns to copy.

required

cells_to_polygons_mean(polygons, cells, values, weights=None)

Per polygon (in polygons order): the weighted mean of values over the cells whose centre lies inside it.

weights: a sequence of weight arrays tried in turn (e.g. residents, then jobs): a polygon whose cells have no weight under the first uses the next; with none left, the plain mean. A polygon holding no cell centre (smaller than a cell) takes the value of the cell under its representative point when that cell is in cells; NaN otherwise. Used for the census geometries' level of service: the population-weighted mean of the finest cells (the map's rule).

Parameters:

Name Type Description Default
polygons GeoDataFrame

Polygons.

required
cells ndarray

uint64 H3 ids.

required
values ndarray

Value per cell.

required
weights Optional[Iterable[ndarray]]

Weight arrays tried in turn.

None