Skip to content

geohierarchy.edges

Aggregate line-geometry attribute data onto an arbitrary polygon layer.

This module is a thin convenience wrapper around :class:~geohierarchy.core.GeoHierarchy for the common one-shot task of turning "edges" data -- any GeoDataFrame of LineString/MultiLineString geometries with attribute columns, such as street network edges, rail lines, or utility lines -- into an aggregated table on top of a polygon geometry layer (H3 cells from :func:~geohierarchy.utils.h3_cells, administrative boundaries, or any other polygon GeoDataFrame). Internally it builds a two-level hierarchy (the target polygons as the only level) and injects the edges via :meth:~geohierarchy.core.GeoHierarchy.add_vector_data with geoweight_by="length", since overlap between a line and a polygon is naturally weighted by the length of line inside each polygon, not by area.

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

Aggregate columns from a line-geometry GeoDataFrame onto a polygon level.

Builds a single-level :class:~geohierarchy.core.GeoHierarchy from level_gdf, injects edges_gdf's attribute columns into it with :meth:~geohierarchy.core.GeoHierarchy.add_vector_data (length-weighted, since edges_gdf holds line geometries), and returns the resulting polygon layer with just the requested columns.

Parameters:

Name Type Description Default
edges_gdf GeoDataFrame

Source GeoDataFrame of line geometries (e.g. street network edges) carrying the attribute columns to aggregate.

required
level_gdf GeoDataFrame

Target polygon GeoDataFrame to aggregate onto -- an H3 grid from :func:~geohierarchy.utils.h3_cells, 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_gdf to aggregate onto level_gdf.

required
agg Union[AggregationStrategy, Dict[str, AggregationStrategy], None]

Aggregation strategy applied to every column in columns, or a per-column mapping of {column: AggregationStrategy}. Defaults to :class:~geohierarchy.aggregation.Mean for every column if not given.

None
geometry_col str

Name of the geometry column in edges_gdf, in case it isn't already named "geometry".

'geometry'
crs

Coordinate reference system the hierarchy (and the returned GeoDataFrame) should use. Defaults to level_gdf's own CRS.

None

Returns:

Type Description
GeoDataFrame

A copy of level_gdf (reprojected to crs) with id_col,

GeoDataFrame

geometry, and the aggregated columns -- one row per polygon.

edges_to_h3_by_distance(edges_gdf, h3_cells, columns, resolution, margin_m=10.0, agg=None, fallback_radius_multiplier=5.0)

Aggregate line-geometry columns onto H3 cells by centroid distance, not geometric overlap.

An alternative to :func:edges_to_level for H3 targets specifically: instead of a GEOS polygon/line overlay (which can be expensive or even memory-unstable for very large edge counts -- e.g. shapely.union_all over 800K+ street edges failing outright with GEOSException: std::bad_alloc), "touching" is defined purely by distance -- an H3 cell counts as touched by a line if any point on that line falls within margin_m of the cell's centroid, checked via a scipy.spatial.cKDTree radius query against densified line-sample points (see :func:_densify_lines). No Shapely geometry object is built anywhere in this path.

Parameters:

Name Type Description Default
edges_gdf GeoDataFrame

Source GeoDataFrame of line geometries carrying the attribute columns to aggregate.

required
h3_cells Sequence[str]

Candidate H3 cells to test (e.g. every cell with population, to skip cells irrelevant to the caller).

required
columns Union[str, List[str]]

Column name or list of column names in edges_gdf to aggregate onto touching cells.

required
resolution int

H3 resolution of h3_cells -- used to size the search radius (margin_m beyond one cell's approximate diameter, 2 * h3.average_hexagon_edge_length(resolution)).

required
margin_m float

Extra distance beyond one cell diameter still counted as "touching".

10.0
agg Union[AggregationStrategy, Dict[str, AggregationStrategy], None]

Aggregation strategy applied to every column, or a per-column mapping. Defaults to :class:~geohierarchy.aggregation.Max (matching this function's typical use: assigning each cell the best/highest value among nearby edges).

None
fallback_radius_multiplier Optional[float]

Guarantees every cell in h3_cells gets a value. Any cell that matched no edge within radius (margin_m + 2 * edge_len_m) falls back to its single nearest edge-sample point, as long as that point is within fallback_radius_multiplier * edge_len_m (the H3 cell's approximate edge length at resolution) of the cell centroid. Cells with no edge at all within that fallback distance are still left absent from the result -- callers that need an explicit "no coverage" sentinel (e.g. filling with 0.0) should do so themselves on the missing rows. Set to None to disable the fallback and keep the original behavior (unmatched cells simply absent).

5.0

Returns:

Type Description
DataFrame

Polars DataFrame with h3_cell and columns, one row per

DataFrame

cell within margin_m of at least one edge. Cells with no

DataFrame

nearby edge are simply absent.

h3_cells_near_lines(geoms_4326, resolution, max_dist_m, region=None)

uint64 ids of every H3 cell at resolution whose centre is within max_dist_m of one of the lines geoms_4326 (EPSG:4326 shapely geometries); with region (a 4326 polygon), only cells whose centre is inside it.

Candidates: the cells of the lines' vertices densified every min(edge, max_dist_m / 10) m (at least 2 m) and their rings out to max_dist_m; then the exact test: each candidate centre's distance to the densified vertices (KD tree; the error is below half the densification step). Chunk large line sets yourself and union the results.

Parameters:

Name Type Description Default
geoms_4326

Line geometries in EPSG:4326 (shapely).

required
resolution int

H3 resolution of the cells.

required
max_dist_m float

Distance from the lines, in metres, within which a cell's centre must lie.

required
region

Optional EPSG:4326 polygon: only cells whose centre is inside it are kept.

None

Returns:

Type Description

Sorted unique uint64 H3 cell ids.