Improve data pipeline

This commit is contained in:
Andras Schmelczer 2026-06-01 20:10:03 +01:00
parent e8345cbdc1
commit f99bd4e5c9
36 changed files with 966 additions and 129 deletions

View file

@ -893,3 +893,54 @@ class TestSubtractGreenspace:
result = subtract_greenspace(postcode, tree, geoms)
# 80% < 90% cap, so subtraction should happen
assert result.area == pytest.approx(2000, rel=0.01)
class TestToWgs84GeojsonValidity:
"""to_wgs84_geojson must emit GeoJSON that round-trips to a valid geometry."""
def test_geojson_round_trips_to_valid_geometry(self):
from shapely.geometry import shape
geojson = to_wgs84_geojson(box(530000, 180000, 530100, 180100))
assert geojson is not None
rt = shape(geojson)
assert not rt.is_empty
assert rt.is_valid
def test_written_district_features_are_all_valid(self, tmp_path):
from shapely.geometry import shape
postcodes = {
"AA1 1AA": box(530000, 180000, 530100, 180100),
"AA1 1AB": MultiPolygon(
[
box(530200, 180000, 530250, 180050),
box(530200, 180060, 530250, 180110),
]
),
}
assert write_district_geojson(postcodes, tmp_path) == 1
collection = json.loads((tmp_path / "units" / "AA1.geojson").read_text())
for feature in collection["features"]:
geom = shape(feature["geometry"])
assert geom.is_valid
assert not geom.is_empty
class TestGreenspaceHolePreserved:
"""Interior holes carved by greenspace subtraction must survive merge_fragments
(the post-subtraction _fill_holes that previously negated them was removed)."""
def test_interior_lake_hole_survives_merge_fragments(self):
from shapely.strtree import STRtree
postcode = box(0, 0, 100, 100) # 10000 sqm
lake = box(30, 30, 70, 70) # 1600 sqm fully-interior hole (16% removal)
result = merge_fragments(
[("TEST1", postcode)],
greenspace_tree=STRtree([lake]),
greenspace_geoms=[lake],
)
merged = result["TEST1"]
assert len(list(merged.interiors)) == 1
assert merged.area == pytest.approx(10000 - 1600, rel=0.05)