Skip to content

UrbanAccessAnalyzer.geometry_levels

Bridge from UAA's Polars edge results to geohierarchy's any-polygon aggregation.

UAA represents street-network edges (and their per-edge access scores) as a Polars DataFrame with a WKB geometry column, while the geohierarchy package works with GeoPandas GeoDataFrames and aggregates line data onto an arbitrary polygon layer (an H3 grid, administrative boundaries, or any other polygon GeoDataFrame) via its edges_to_level helper. This module does the small amount of glue work in between: converting UAA's Polars/WKB edge table into a GeoDataFrame (the same conversion :meth:AccessibilityAnalyzer .to_h3 already did before this refactor) and handling columns that aren't plain numeric quantities -- e.g. categorical access-score labels -- which geohierarchy's aggregation strategies (Max/Min) don't natively support since they call Polars numeric-only operations internally. This is an optional dependency of UAA: geohierarchy is only imported when a function in this module is actually called, behind the geohierarchy extra.

edges_to_level(edges_df, level_gdf, id_col, columns, agg=None, geometry_col='geometry', crs=None)

Aggregate columns from a Polars WKB edge table onto a polygon level.

Converts edges_df (a Polars DataFrame with a WKB-encoded geometry_col, as produced throughout UAA) into a GeoDataFrame, then delegates to :func:geohierarchy.edges_to_level to length-weight the requested columns onto level_gdf.

Non-numeric columns (e.g. categorical access-score labels) can't be aggregated by geohierarchy's Max/Min strategies directly, since those call Polars numeric-only expressions internally. For any column resolved to a Max/Min strategy that isn't already numeric, this function encodes it to integer codes ordered the same way Polars would sort the raw values (so the numeric max/min matches the string max/min UAA previously produced), aggregates, then decodes the result back to the original labels.

Parameters:

Name Type Description Default
edges_df DataFrame

Polars DataFrame with a WKB geometry_col column plus the attribute columns to aggregate.

required
level_gdf GeoDataFrame

Target polygon GeoDataFrame to aggregate onto -- an H3 grid, an administrative boundary layer, or any other polygon layer.

required
id_col str

Name of the unique id column in level_gdf.

required
columns Union[str, List[str]]

Column name or list of column names in edges_df to aggregate onto level_gdf.

required
agg

Aggregation strategy (a :class:~geohierarchy.aggregation.AggregationStrategy instance) applied to every column, or a per-column {column: AggregationStrategy} mapping. Defaults to :class:~geohierarchy.aggregation.Mean for every column, per :func:geohierarchy.edges_to_level.

None
geometry_col str

Name of the WKB geometry column in edges_df.

'geometry'
crs

Coordinate reference system the result should use. Defaults to level_gdf's own CRS.

None

Returns:

Type Description
GeoDataFrame

A copy of level_gdf with id_col, geometry, and the

GeoDataFrame

aggregated columns -- one row per polygon.

Raises:

Type Description
ImportError

If the optional geohierarchy dependency isn't installed.