"""Load polygon rings from a GeoJSON file.""" from __future__ import annotations import json from pathlib import Path def load_geojson_polygons( path: "str | Path", ) -> "list[list[list[tuple[float, float]]]]": """Load polygons from a GeoJSON file. Reads every feature geometry. Polygon features produce one polygon; MultiPolygon features produce N polygons. Each polygon is a list of rings where each ring is a list of (x, y) float tuples. Args: path: Path to the GeoJSON file. Returns: List of polygons. Each polygon is a list of rings (exterior + holes). Each ring is a list of (x, y) tuples. Raises: FileNotFoundError: If the file does not exist. ValueError: If the file is not valid GeoJSON or contains unsupported geometry types. """ p = Path(path) if not p.exists(): raise FileNotFoundError(f"GeoJSON file not found: {p}") with p.open(encoding="utf-8") as f: data = json.load(f) features = data.get("features", []) result: list[list[list[tuple[float, float]]]] = [] for feature in features: geom = feature.get("geometry") if geom is None: continue gtype = geom.get("type") coords = geom.get("coordinates", []) if gtype == "Polygon": # coords = [exterior_ring, *hole_rings] rings = [[(x, y) for x, y, *_ in ring] for ring in coords] result.append(rings) elif gtype == "MultiPolygon": # coords = [polygon, ...] each polygon = [ring, ...] for polygon_coords in coords: rings = [[(x, y) for x, y, *_ in ring] for ring in polygon_coords] result.append(rings) return result