from pathlib import Path from types import SimpleNamespace import pytest from shapely.geometry import LineString, Point, Polygon, box from leonida.heights import PALM_CLEARANCE from leonida.park import ParkError, ParkLayout, World, apply_flamingo_park, apply_park, check_corners, load_park DATA = Path(__file__).resolve().parents[1] / "data" QUAD = box(0, 0, 100, 200) SERVICE = box(60, 0, 66, 30) def layout(): return ParkLayout(lawn=QUAD, surfaces=[(box(10, 10, 40, 40), "field")], paths=[box(0, 148, 100, 152)], service=[SERVICE], buildings=[(box(70, 60, 80, 70), 5.0)], keepouts=[box(60, 160, 90, 190)]) def world(): return World( land=[box(-100, -100, 200, 300)], surfaces=[(box(-30, -30, 130, 230), "urban")], roads=[(box(-50, 95, 150, 105), False), (box(-20, -20, 0, 220), False)], centerlines=[LineString([(-50, 100), (150, 100)]), LineString([(-5, 150), (10, 150)])], buildings=[(box(20, 60, 30, 70), {"height": 9.0, "source": "district"}, False), # ganz im Park (box(-6, 80, 4, 90), {"height": 9.0, "source": "district"}, False), # 40 % im Park (box(-4, 120, 6, 130), {"height": 9.0, "source": "district"}, False)], # 60 % im Park palms=[(50.0, 50.0), (-10.0, 50.0)], ) def test_old_roads_and_centerlines_end_at_the_park_edge(): result = apply_park(world(), layout(), seed=1) assert all(poly.intersection(QUAD).area < 1e-6 for poly, _ in result.roads if not poly.equals(SERVICE)) assert any(poly.contains(Point(-30, 100)) for poly, _ in result.roads) assert any(poly.contains(Point(120, 100)) for poly, _ in result.roads) assert sorted(line.bounds for line in result.centerlines) == [(-50.0, 100.0, 0.0, 100.0), (100.0, 100.0, 150.0, 100.0)] def test_old_surfaces_buildings_and_palms_in_the_park_go(): result = apply_park(world(), layout(), seed=1) assert all(poly.intersection(QUAD).area < 1e-6 for poly, kind in result.surfaces if kind == "urban") assert sorted((b[0].bounds, b[1]["source"]) for b in result.buildings) == [ ((-6.0, 80.0, 4.0, 90.0), "district"), ((70.0, 60.0, 80.0, 70.0), "osm")] assert (-10.0, 50.0) in result.palms and (50.0, 50.0) not in result.palms def test_layout_goes_in_with_the_lawn_before_the_fields(): result = apply_park(world(), layout(), seed=1) kinds = [kind for _, kind in result.surfaces] assert kinds.index("park") < kinds.index("field") assert any(kind == "park" and poly.equals(QUAD) for poly, kind in result.surfaces) assert result.paths == layout().paths assert any(poly.equals(SERVICE) and not bridge for poly, bridge in result.roads) def test_new_palms_stand_on_lawn_away_from_roads_paths_buildings_and_keepouts(): result = apply_park(world(), layout(), seed=1) new = [Point(p) for p in result.palms if p != (-10.0, 50.0)] assert len(new) > 5 for palm in new: assert QUAD.contains(palm) and not box(10, 10, 40, 40).contains(palm) assert palm.distance(box(0, 148, 100, 152)) > PALM_CLEARANCE assert palm.distance(box(60, 160, 90, 190)) > PALM_CLEARANCE assert all(palm.distance(poly) > PALM_CLEARANCE for poly, _ in result.roads) assert all(palm.distance(b[0]) > PALM_CLEARANCE for b in result.buildings) def test_same_seed_gives_the_same_palms(): assert apply_park(world(), layout(), seed=7).palms == apply_park(world(), layout(), seed=7).palms def test_corners_must_lie_at_a_road(): roads = [(box(-20, -20, 0, 220), False), (box(100, -20, 120, 220), False)] check_corners(SimpleNamespace(game={"sw": (0.0, 0.0), "se": (100.0, 0.0), "ne": (100.0, 200.0), "nw": (0.0, 200.0)}), roads) far = SimpleNamespace(game={"sw": (0.0, 0.0), "se": (100.0, 0.0), "ne": (100.0, 200.0), "nw": (30.0, 200.0)}) with pytest.raises(ParkError, match="Spielecke nw"): check_corners(far, roads) def test_flamingo_park_goes_into_a_world_with_roads_around_the_park(): config = load_park(DATA / "flamingo-park.toml") park = Polygon([config.game[key] for key in ("sw", "se", "ne", "nw")]) ring = park.buffer(15, join_style="mitre").difference(park) start = World(land=[park.buffer(400)], surfaces=[], roads=[(ring, False)], centerlines=[], buildings=[], palms=[]) result, messages = apply_flamingo_park(start, DATA / "flamingo-park.toml", seed=20260912) assert {"park", "field", "court", "pool", "parking", "sand"} <= {kind for _, kind in result.surfaces} assert len([b for b in result.buildings if b[1]["source"] == "osm"]) == 11 assert result.paths and result.palms assert LineString([config.street.start, config.street.end]) in result.centerlines assert not any(message.startswith("Warnung") for message in messages) assert any(message.startswith("Flamingo Park:") for message in messages)