download_files_from_LGL_BW.py 7.5 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
LoD2 CityGML tiles are available for whole Baden-Württemberg, from LGL.

https://opengeodata.lgl-bw.de/#/(sidenav:product/12)

This script downloads the requires tiles for given regions
(as WKT strings, in *polygons* variable), and extracts the region.

Required:
* Python
* pyproj project (https://pypi.org/project/pyproj/)
* SimStadt installed on the Desktop (for RegionChooser)

Eric Duminil, 2024
"""
from pathlib import Path
from math import floor
import subprocess
import re
import urllib.request
import time
import zipfile

25
26
27
from pyproj import CRS
from pyproj import Transformer

Eric Duminil's avatar
Eric Duminil committed
28
29
from get_coordinates_by_zipcode import get_coordinates_by_zipcode

30
31
COORDINATES_REGEX = re.compile(r"(\-?\d+\.\d*) (\-?\d+\.\d*)")

32
###### User input ##########
Eric Duminil's avatar
Eric Duminil committed
33
34
# Values can be either a WKT POLYGON or MULTIPOLYGON, a Zipcode, or Zipcodes separated by a comma.
REGIONS = {
35
36
37
38
39
40
41
42
43
44
45
46
    "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...",
}
# Should RegionChooser extract the regions from multiple CityGMLs?
EXTRACT_REGIONS = True
############################


CITYGML_SERVER = "https://opengeodata.lgl-bw.de/data/lod2"
RASTER = 2  # [km]
BUNDESLAND = 'bw'

Eric Duminil's avatar
Eric Duminil committed
47
# UTM32N, used in BW. https://epsg.io/32632
Eric Duminil's avatar
Eric Duminil committed
48
49
50
TO_LOCAL_CRS = Transformer.from_crs(CRS.from_epsg(4326),
                                    CRS.from_epsg(32632),
                                    always_xy=True)
Eric Duminil's avatar
Eric Duminil committed
51

52
53
54
55
56
57
UTM = 32

SCRIPT_DIR = Path(__file__).parent
WAIT_BETWEEN_DOWNLOADS = 5  # [s] Be nice to LGL Server.

if EXTRACT_REGIONS:
Eric Duminil's avatar
Eric Duminil committed
58
59
60
61
62
63
64
65
66
    try:
        SIMSTADT_FOLDER = next(x for x in Path.home().glob('Desktop/SimStadt*_0.*/') if x.is_dir())
        print(f"RegionChooser has been found in {SIMSTADT_FOLDER}")
    except StopIteration:
        exit("No SimStadt installation found!"
             "\nPlease copy a SimStadt installation to the desktop,"
             "\nset EXTRACT_REGIONS to False,"
             "\nor set SIMSTADT_FOLDER manually: SIMSTADT_FOLDER = Path('/path/to/SimStadt')"
             )
67
68
69
    GML_GLOB = "LoD2_*/LoD2_*.gml"


Eric Duminil's avatar
Eric Duminil committed
70
def coordinates_to_grid(longitude: float, latitude: float) -> tuple[int, int]:
71
    """Returns (x, y) of the tile on CITYGML_SERVER containing a given point."""
Eric Duminil's avatar
Eric Duminil committed
72
    x, y = TO_LOCAL_CRS.transform(longitude, latitude)
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    x = floor(x / 1000) - 1  # Odd x
    y = floor(y / 1000)  # Even y
    x -= x % RASTER
    y -= y % RASTER
    return (x + 1, y)


def wkt_polygon_to_grid_coords(location_name: str, wkt: str) -> tuple[int, int, int, int]:
    """Returns (x, y) of lower-left and bottom-right tiles, containing a given region."""
    if 'POLYGON' not in wkt:
        raise ValueError(f"wkt for {location_name} should be a WKT POLYGON or MULTIPOLYGON")

    coordinates = re.findall(r'\-?\d+\.\d+', wkt)

    lons = [float(lon) for lon in coordinates[::2]]
    lats = [float(lat) for lat in coordinates[1::2]]

    min_lon, max_lon = min(lons), max(lons)
    min_lat, max_lat = min(lats), max(lats)

Eric Duminil's avatar
Eric Duminil committed
93
94
    print("%s (%.3f°N %.3f°E -> %.3f°N %.3f°E)" %
          (location_name, max_lat, min_lon, min_lat, max_lon))
95

Eric Duminil's avatar
Eric Duminil committed
96
97
    x1, y1 = coordinates_to_grid(min_lon, min_lat)
    x2, y2 = coordinates_to_grid(max_lon, max_lat)
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

    return (x1, x2, y1, y2)


def download_rectangle(output_dir: Path, x1: int, x2: int, y1: int, y2: int) -> None:
    """Downloads every zip of a given region, to output_dir, and extracts CityGML files."""
    for x in range(x1, x2 + 1, RASTER):
        for y in range(y1, y2 + 1, RASTER):
            citygml_zip = f"LoD2_{UTM}_{x}_{y}_{RASTER}_{BUNDESLAND}.zip"
            citygml_url = f"{CITYGML_SERVER}/{citygml_zip}"
            local_zip = output_dir / citygml_zip  # .replace('.zip', '.gml')
            if local_zip.exists():
                print(f"  {local_zip.name} already in {output_dir.name}/")
            else:
                print(f"  Download {citygml_zip} to {output_dir.name}/ ", end='')
113
114
115
116
117
118
119
120
                try:
                    urllib.request.urlretrieve(citygml_url, local_zip)
                except urllib.error.HTTPError as e:
                    print(f"❌ {e}")
                    continue
                finally:
                    time.sleep(WAIT_BETWEEN_DOWNLOADS)
                print("✅")
121
122

                print(f"    Extract {citygml_zip} to {output_dir.name}/ ", end='')
123
                print("✅")
124
125
126
127
128
129
130
131
132
133
134
135
                print("")
                with zipfile.ZipFile(local_zip, "r") as zip_ref:
                    zip_ref.extractall(output_dir)


def extract_region(output_dir: Path, location_name: str, wkt: str) -> None:
    """Uses RegionChooser to extract a given region from all the CityGML files found in subfolder."""
    output_file = output_dir / (location_name + '.gml')
    if output_file.exists():
        print(f"  {output_file} already exists. Not extracting.")
        return
    region_chooser_libs = Path(SIMSTADT_FOLDER).expanduser() / 'lib/*'
136
137
138
139
    gml_inputs = list(output_dir.glob(GML_GLOB))
    if len(gml_inputs) == 0:
        print("Error: No CityGML found. At least part of the region should be in Baden-Württemberg!")
        return
140
141
142
143

    params_path = output_dir / 'params.txt'
    wkt_path = output_dir / 'region.wkt'

144
145
    local_wkt = convert_wkt_to_local(wkt)

146
    print(f"  Extracting {output_file}.")
147
    with open(wkt_path, 'w') as f:
148
        f.write(local_wkt)
149
150
151
152
153
154
155

    with open(params_path, 'w') as f:
        f.write("--input\n")
        f.write(','.join(f"{gml}" for gml in gml_inputs))
        f.write("\n")
        f.write("--output\n")
        f.write(f'"{output_file}"\n')
156
        f.write('--local\n')
157
158
159
        f.write("--wkt\n")
        f.write(f'"{wkt_path}"\n')

160
161
    result = subprocess.run(['java', '-classpath', f'{region_chooser_libs}',
                             'eu.simstadt.regionchooser.RegionChooserCLI',
162
                             f'@{params_path}'
163
                             ],
164
                            text=True,
165
                            capture_output=True
166
167
168
                            )
    if (result.stderr):
        print(result.stderr)
169
170
    if result.returncode != 0:
        raise ValueError(f"RegionChooser failed with code {result.returncode}")
171
172
173
    print("  DONE!")


Eric Duminil's avatar
Eric Duminil committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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:
188
    """Downloads ZIP files, extracts CityGML files, and selects desired region."""
Eric Duminil's avatar
Eric Duminil committed
189
    for location_name, wkt_or_zipcode in regions.items():
Eric Duminil's avatar
Eric Duminil committed
190
191
        if ' ' in location_name:
            raise ValueError("Location name should not contain spaces: 'Some City' -> 'SomeCity'")
192
193
        output_dir = SCRIPT_DIR / (location_name + '.proj')
        output_dir.mkdir(parents=True, exist_ok=True)
Eric Duminil's avatar
Eric Duminil committed
194
        wkt = get_wkt(wkt_or_zipcode)
195
196
197
198
199
200
        x1, x2, y1, y2 = wkt_polygon_to_grid_coords(location_name, wkt)
        download_rectangle(output_dir, x1, x2, y1, y2)
        if EXTRACT_REGIONS:
            extract_region(output_dir, location_name, wkt)
        print()

Eric Duminil's avatar
Eric Duminil committed
201

Eric Duminil's avatar
Eric Duminil committed
202
def convert_coordinates(match):
203
    longitude, latitude = match.groups()
Eric Duminil's avatar
Eric Duminil committed
204
    x, y = TO_LOCAL_CRS.transform(longitude, latitude)
205
206
    return f"{x} {y}"

Eric Duminil's avatar
Eric Duminil committed
207

208
def convert_wkt_to_local(wkt):
Eric Duminil's avatar
Eric Duminil committed
209
    return COORDINATES_REGEX.sub(convert_coordinates, wkt)
210

Eric Duminil's avatar
Eric Duminil committed
211

212
if __name__ == '__main__':
Eric Duminil's avatar
Eric Duminil committed
213
    main(REGIONS)