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: |
required |
id_col
|
str
|
Name of the unique id column in |
required |
columns
|
Union[str, List[str]]
|
Column name or list of column names in |
required |
agg
|
Union[AggregationStrategy, Dict[str, AggregationStrategy], None]
|
Aggregation strategy applied to every column in |
None
|
geometry_col
|
str
|
Name of the geometry column in |
'geometry'
|
crs
|
Coordinate reference system the hierarchy (and the returned
GeoDataFrame) should use. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
A copy of |
GeoDataFrame
|
geometry, and the aggregated |
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 |
required |
resolution
|
int
|
H3 resolution of |
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: |
None
|
fallback_radius_multiplier
|
Optional[float]
|
Guarantees every cell in |
5.0
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Polars DataFrame with |
DataFrame
|
cell within |
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 |