diff --git a/download_files_from_LGL_BW.py b/download_files_from_LGL_BW.py index 881ad0a0944f5f5d173cdb256fd060da249dc776..66faabca47b7c6124396ec75b0d8305532838120 100644 --- a/download_files_from_LGL_BW.py +++ b/download_files_from_LGL_BW.py @@ -25,7 +25,9 @@ import zipfile from pyproj import CRS from pyproj import Transformer -import shapely +from shapely import wkt +from shapely.ops import transform +from shapely.geometry import Point # TODO: Write tests # TODO: Use logging @@ -49,6 +51,7 @@ EXTRACT_REGIONS = True CITYGML_SERVER = "https://opengeodata.lgl-bw.de/data/lod2" RASTER = 2 # [km] +KILOMETER = 1000 # [m] BUNDESLAND = 'bw' # UTM32N, used in BW. https://epsg.io/32632 @@ -77,8 +80,8 @@ if EXTRACT_REGIONS: def coordinates_to_grid(longitude: float, latitude: float) -> tuple[int, int]: """Returns (x, y) of the tile on CITYGML_SERVER containing a given point.""" x, y = TO_LOCAL_CRS.transform(longitude, latitude) - x = floor(x / 1000) - 1 # Odd x - y = floor(y / 1000) # Even y + x = floor(x / KILOMETER) - 1 # Odd x + y = floor(y / KILOMETER) # Even y x -= x % RASTER y -= y % RASTER return (x + 1, y) @@ -106,11 +109,16 @@ def wkt_polygon_to_grid_coords(location_name: str, wkt: str) -> tuple[int, int, return (x1, x2, y1, y2) -def download_whole_region(output_dir: Path, wkt: str, x1: int, x2: int, y1: int, y2: int) -> None: +def download_whole_region(output_dir: Path, wkt_region: str, x1: int, x2: int, y1: int, y2: int) -> None: """Downloads every zip of a given region, to output_dir, and extracts CityGML files.""" - # FIXME: Too many uninteresting tiles for complex shapes. Check if the tile is interesecting with the polygon! + + wgs84_region = wkt.loads(wkt_region) + local_region = transform(TO_LOCAL_CRS.transform, wgs84_region) for x in range(x1, x2 + 1, RASTER): for y in range(y1, y2 + 1, RASTER): + tile_center = Point((x + 1) * KILOMETER, (y + 1) * KILOMETER) + if local_region.distance(tile_center) > RASTER * KILOMETER: + continue citygml_zip = f"LoD2_{UTM}_{x}_{y}_{RASTER}_{BUNDESLAND}.zip" citygml_url = f"{CITYGML_SERVER}/{citygml_zip}" local_zip = output_dir / citygml_zip