Spaces:
Sleeping
Sleeping
File size: 6,066 Bytes
7af92cf 91d8a03 7af92cf 91d8a03 7af92cf 91d8a03 7af92cf 91d8a03 7af92cf 91d8a03 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | import geopandas as gpd
import pytest
from shapely.geometry import LineString, Point, Polygon, box
from shared.utils.constants import SCAG_FT_CRS
from shared.utils.geoprocessing import clip_to_buffer_rings
# EPSG:2229 coordinates for a point in central LA
LA_X, LA_Y = 6_490_000, 2_040_000
def _source(x: float = LA_X, y: float = LA_Y) -> gpd.GeoDataFrame:
return gpd.GeoDataFrame(
{"source_id": ["S1"]},
geometry=[Point(x, y)],
crs=SCAG_FT_CRS,
)
def _point_feature(x: float, y: float) -> gpd.GeoDataFrame:
return gpd.GeoDataFrame(
{"feature_id": ["F1"]},
geometry=[Point(x, y)],
crs=SCAG_FT_CRS,
)
def _line_feature(x0: float, y0: float, x1: float, y1: float) -> gpd.GeoDataFrame:
return gpd.GeoDataFrame(
{"feature_id": ["F1"]},
geometry=[LineString([(x0, y0), (x1, y1)])],
crs=SCAG_FT_CRS,
)
def _polygon_feature(cx: float, cy: float, half: float = 50) -> gpd.GeoDataFrame:
return gpd.GeoDataFrame(
{"feature_id": ["F1"]},
geometry=[box(cx - half, cy - half, cx + half, cy + half)],
crs=SCAG_FT_CRS,
)
class TestClipToBufferRingsInclusion:
"""Features inside / outside buffer rings are included / excluded correctly."""
def test_point_inside_inner_ring_is_included(self):
# Arrange
features = _point_feature(LA_X + 100, LA_Y)
# Act
inner, *_ = clip_to_buffer_rings(features, _source(), [200, 1320])
# Asser
assert len(inner) == 1
def test_point_outside_outer_ring_is_excluded(self):
# Arrange
features = _point_feature(LA_X + 3000, LA_Y)
# Act
inner, outer = clip_to_buffer_rings(features, _source(), [200, 2640])
# Asser
assert len(inner) == 0
assert len(outer) == 0
def test_point_in_donut_excluded_from_inner_ring(self):
# Arrange
features = _point_feature(LA_X + 500, LA_Y)
# Act
inner, donut = clip_to_buffer_rings(features, _source(), [200, 1320])
# Asser
assert len(inner) == 0
assert len(donut) == 1
def test_line_crossing_buffer_boundary_is_clipped(self):
# Arrange
features = _line_feature(LA_X, LA_Y, LA_X + 1000, LA_Y)
# Act
inner, *_ = clip_to_buffer_rings(features, _source(), [200, 1320])
# Assert
original_length = features.geometry.length.iloc[0]
assert len(inner) == 1
assert inner.geometry.length.iloc[0] < original_length
def test_line_fully_outside_all_rings_excluded(self):
# Arrange
features = _line_feature(LA_X + 3000, LA_Y, LA_X + 4000, LA_Y)
# Act
inner, outer = clip_to_buffer_rings(features, _source(), [200, 2640])
# Asser
assert len(inner) == 0
assert len(outer) == 0
def test_polygon_inside_inner_ring_fully_included(self):
# Arrange
features = _polygon_feature(LA_X + 50, LA_Y, half=10)
# Act
inner, *_ = clip_to_buffer_rings(features, _source(), [200, 1320])
# Asser
assert len(inner) == 1
assert inner.geometry.area.iloc[0] == pytest.approx(features.geometry.area.iloc[0], rel=0.01)
def test_polygon_straddling_inner_boundary_is_clipped(self):
# Arrange
features = _polygon_feature(LA_X, LA_Y, half=500)
# Act
inner, *_ = clip_to_buffer_rings(features, _source(), [200, 1320])
# Assert
assert inner.geometry.area.iloc[0] < features.geometry.area.iloc[0]
def test_polygon_outside_all_rings_excluded(self):
# Arrange
features = _polygon_feature(LA_X + 5000, LA_Y, half=100)
# Act
inner, outer = clip_to_buffer_rings(features, _source(), [200, 2640])
# Asser
assert len(inner) == 0
assert len(outer) == 0
class TestClipToBufferRingsDonutShape:
"""Donut rings have boundaries at the expected distances."""
def test_inner_ring_radius_matches_buffer_distance(self):
# Arrange
features = _polygon_feature(LA_X, LA_Y, half=3000)
# Act
inner, *_ = clip_to_buffer_rings(features, _source(), [200, 1320, 2640])
# Assert
expected_area = 3.14159 * 200**2
assert inner.geometry.area.iloc[0] == pytest.approx(expected_area, rel=0.01)
def test_donut_excludes_inner_buffer_area(self):
# Arrange
features = _polygon_feature(LA_X, LA_Y, half=3000)
# Act
inner, donut, *_ = clip_to_buffer_rings(features, _source(), [200, 1320, 2640])
# Assert
donut_union = donut.geometry.unary_union
inner_union = inner.geometry.unary_union
assert donut_union.intersection(inner_union).area == pytest.approx(0, abs=1)
def test_donut_outer_boundary_matches_buffer_distance(self):
# Arrange
features = _polygon_feature(LA_X, LA_Y, half=3000)
# Act
inner, donut, *_ = clip_to_buffer_rings(features, _source(), [200, 1320, 2640])
# Assert
expected_area = 3.14159 * 1320**2
combined_area = inner.geometry.area.sum() + donut.geometry.area.sum()
assert combined_area == pytest.approx(expected_area, rel=0.01)
def test_three_rings_are_non_overlapping_and_contiguous(self):
# Arrange
features = _polygon_feature(LA_X, LA_Y, half=3000)
# Act
inner, qtr, half = clip_to_buffer_rings(features, _source(), [200, 1320, 2640])
# Assert
inner_u = inner.geometry.unary_union
qtr_u = qtr.geometry.unary_union
half_u = half.geometry.unary_union
assert inner_u.intersection(qtr_u).area == pytest.approx(0, abs=1)
assert inner_u.intersection(half_u).area == pytest.approx(0, abs=1)
assert qtr_u.intersection(half_u).area == pytest.approx(0, abs=1)
# Assert
expected_area = 3.14159 * 2640**2
total_area = inner_u.area + qtr_u.area + half_u.area
assert total_area == pytest.approx(expected_area, rel=0.01)
|