diff --git a/download_files_from_LGL_BW.py b/download_files_from_LGL_BW.py index 824ad52378c6685603e49e47bbafe9accd5be580..6b46b1d17d3e3e03771449fa28014864c37442f1 100644 --- a/download_files_from_LGL_BW.py +++ b/download_files_from_LGL_BW.py @@ -25,10 +25,13 @@ import zipfile from pyproj import CRS from pyproj import Transformer +from get_coordinates_by_zipcode import get_coordinates_by_zipcode + COORDINATES_REGEX = re.compile(r"(\-?\d+\.\d*) (\-?\d+\.\d*)") ###### User input ########## -POLYGONS = { +# Values can be either a WKT POLYGON or MULTIPOLYGON, a Zipcode, or Zipcodes separated by a comma. +REGIONS = { "StuttgartCenter": "POLYGON((9.175287 48.780916, 9.185501 48.777522, 9.181467 48.773704, 9.174429 48.768472, 9.168807 48.773902, 9.175287 48.780916))", # "AnotherRegion": "Another WKT Polygon...", } @@ -168,13 +171,27 @@ def extract_region(output_dir: Path, location_name: str, wkt: str) -> None: print(" DONE!") -def main(polygons: dict[str, str]) -> None: +def get_wkt(wkt_or_zipcode: str) -> str: + """Returns WKT string for a given region, either specified as a POLYGON, or Zipcode(s). + "POLYGON((...))" + "MULTIPOLYGON(((...)))" + "70567" + "70567,70569" + """ + if 'POLYGON' in wkt_or_zipcode: + return wkt_or_zipcode + else: + return get_coordinates_by_zipcode(wkt_or_zipcode.split(',')) + # raise ValueError(f"Unknown region format: {wkt_or_zipcode}") + +def main(regions: dict[str, str]) -> None: """Downloads ZIP files, extracts CityGML files, and selects desired region.""" - for location_name, wkt in polygons.items(): + for location_name, wkt_or_zipcode in regions.items(): if ' ' in location_name: raise ValueError("Location name should not contain spaces: 'Some City' -> 'SomeCity'") output_dir = SCRIPT_DIR / (location_name + '.proj') output_dir.mkdir(parents=True, exist_ok=True) + wkt = get_wkt(wkt_or_zipcode) x1, x2, y1, y2 = wkt_polygon_to_grid_coords(location_name, wkt) download_rectangle(output_dir, x1, x2, y1, y2) if EXTRACT_REGIONS: @@ -193,4 +210,4 @@ def convert_wkt_to_local(wkt): if __name__ == '__main__': - main(POLYGONS) + main(REGIONS)