|
| 1 | +import logging |
| 2 | +from typing import Callable, Dict, List |
| 3 | + |
| 4 | +import pyproj |
| 5 | +from geojson_pydantic import GeometryCollection, Polygon |
| 6 | +from geojson_pydantic.geometries import Geometry, parse_geometry_obj |
| 7 | +from shapely import box |
| 8 | +from shapely.geometry import shape |
| 9 | +from shapely.ops import transform |
| 10 | + |
| 11 | +from app.schemas.tiles import GridTypeEnum |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | +GRID_REGISTRY: Dict[GridTypeEnum, Callable[[Polygon], GeometryCollection]] = {} |
| 16 | + |
| 17 | + |
| 18 | +def split_polygon_by_grid(polygon: Polygon, grid: GridTypeEnum) -> GeometryCollection: |
| 19 | + """ |
| 20 | + Split a GeoJSON Polygon into smaller polygons according to the specified grid type. |
| 21 | +
|
| 22 | + :param polygon: The GeoJSON Polygon to split. |
| 23 | + :param grid: The grid type to use for splitting. |
| 24 | + :return: A list of GeoJSON Polygons. |
| 25 | + :raises ValueError: If the grid type is unknown. |
| 26 | + """ |
| 27 | + if grid.lower() not in GRID_REGISTRY: |
| 28 | + logger.error(f"An unknown grid was requested: {grid}") |
| 29 | + raise ValueError(f"Unknown grid: {grid}") |
| 30 | + |
| 31 | + split_func = GRID_REGISTRY[grid] |
| 32 | + return split_func(polygon) |
| 33 | + |
| 34 | + |
| 35 | +def split_by_20x20_km_grid(polygon: Polygon) -> GeometryCollection: |
| 36 | + """ |
| 37 | + Split polygon into 20x20 km tiles. |
| 38 | +
|
| 39 | + :param polygon: The GeoJSON Polygon to split. |
| 40 | + :return: A list of GeoJSON Polygons. |
| 41 | + """ |
| 42 | + logger.debug("Splitting polygon in a 20x20km grid") |
| 43 | + |
| 44 | + return GeometryCollection( |
| 45 | + type="GeometryCollection", geometries=_split_by_km_grid(polygon, 20.0) |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def _split_by_km_grid(aoi: Polygon, cell_size_km: float) -> List[Geometry]: |
| 50 | + """ |
| 51 | + Splits a polygon into a list of smaller polygons based on a square grid of given size in km. |
| 52 | +
|
| 53 | + :param aoi: Polygon in GeoJSON format. |
| 54 | + :param cell_size_km: Size of the grid cell in kilometers (default 20km). |
| 55 | + :return: List of polygons as GeoJSON dicts. |
| 56 | + """ |
| 57 | + # Load the polygon |
| 58 | + polygon = shape(aoi) |
| 59 | + |
| 60 | + # Project to a local projection (meters) for accurate distance calculations |
| 61 | + proj_wgs84 = pyproj.CRS("EPSG:4326") |
| 62 | + proj_meters = pyproj.CRS("EPSG:3857") |
| 63 | + project_to_meters = pyproj.Transformer.from_crs( |
| 64 | + proj_wgs84, proj_meters, always_xy=True |
| 65 | + ).transform |
| 66 | + project_to_wgs84 = pyproj.Transformer.from_crs( |
| 67 | + proj_meters, proj_wgs84, always_xy=True |
| 68 | + ).transform |
| 69 | + |
| 70 | + polygon_m = transform(project_to_meters, polygon) |
| 71 | + min_x, min_y, max_x, max_y = polygon_m.bounds |
| 72 | + cell_size_m = cell_size_km * 1000 # convert km to meters |
| 73 | + |
| 74 | + result_polygons: List[Geometry] = [] |
| 75 | + |
| 76 | + x = min_x |
| 77 | + while x < max_x: |
| 78 | + y = min_y |
| 79 | + while y < max_y: |
| 80 | + cell = box(x, y, x + cell_size_m, y + cell_size_m) |
| 81 | + intersection = polygon_m.intersection(cell) |
| 82 | + if not intersection.is_empty: |
| 83 | + # Transform back to WGS84 |
| 84 | + intersection_wgs84 = transform(project_to_wgs84, intersection) |
| 85 | + result_polygons.append( |
| 86 | + parse_geometry_obj(intersection_wgs84.__geo_interface__) |
| 87 | + ) |
| 88 | + y += cell_size_m |
| 89 | + x += cell_size_m |
| 90 | + return result_polygons |
| 91 | + |
| 92 | + |
| 93 | +GRID_REGISTRY[GridTypeEnum.KM_20] = split_by_20x20_km_grid |
0 commit comments