Spaces:
Sleeping
Sleeping
File size: 1,630 Bytes
7af92cf 91d8a03 7af92cf 91d8a03 7af92cf efd0240 7af92cf efd0240 7af92cf efd0240 7af92cf a67b6ba 7af92cf 107b207 91d8a03 7af92cf efd0240 7af92cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | from typing import Literal
import geopandas as gpd
from shared.utils.constants import SCAG_FT_CRS
def clip_to_buffer_rings(
features_gdf: gpd.GeoDataFrame,
sources_gdf: gpd.GeoDataFrame,
buffer_distances: list[int | float],
buffer_crs: str = SCAG_FT_CRS,
donut_how: Literal["difference", "union"] = "difference",
) -> list[gpd.GeoDataFrame]:
distances = sorted(buffer_distances)
ring_definitions = [(dist, distances[i - 1] if i > 0 else None) for i, dist in enumerate(distances)]
sources_reproj = sources_gdf.to_crs(buffer_crs)
source_geom = sources_reproj.geometry
attr_buffers = {
dist: gpd.GeoDataFrame(
sources_reproj.drop(columns="geometry"),
geometry=source_geom.buffer(dist),
crs=buffer_crs,
)
for dist in distances
}
geom_buffers = {dist: gpd.GeoDataFrame(geometry=source_geom.buffer(dist), crs=buffer_crs) for dist in distances}
features_reproj = features_gdf.to_crs(buffer_crs)
clipped = {
dist: gpd.overlay(
gpd.sjoin(features_reproj, buf, how="inner", predicate="intersects").drop(
columns=["index_right"], errors="ignore"
),
geom_buffers[dist],
how="intersection",
)
for dist, buf in attr_buffers.items()
}
rings = []
for outer, inner in ring_definitions:
outer_clip = clipped[outer]
if inner is None or outer_clip.empty:
rings.append(outer_clip)
else:
rings.append(gpd.overlay(outer_clip, geom_buffers[inner], how=donut_how))
return rings
|