Commit 8b7cf643 authored by Eric Duminil's avatar Eric Duminil
Browse files

Most features are tested

parent 101dc71b
No related merge requests found
Showing with 55 additions and 11 deletions
+55 -11
......@@ -140,12 +140,12 @@ def download_whole_region(output_dir: Path, wkt_region: str, x1: int, x2: int, y
logger.info("✅ Extraction successful")
def extract_region(output_dir: Path, location_name: str, wkt_str: str, simstadt_folder: Path) -> None:
def extract_region(output_dir: Path, location_name: str, wkt_str: str, simstadt_folder: Path) -> Path | 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():
logger.info(" %s already exists. Not extracting.", output_file)
return
return output_file
region_chooser_libs = simstadt_folder / 'lib/*'
gml_inputs = list(output_dir.glob(GML_GLOB))
......@@ -186,6 +186,7 @@ def extract_region(output_dir: Path, location_name: str, wkt_str: str, simstadt_
if result.returncode != 0:
raise ValueError(f"RegionChooser failed with code {result.returncode}")
logger.info(" DONE!")
return output_file
def get_wkt(wkt_or_zipcode: str) -> str:
......@@ -208,7 +209,7 @@ def convert_coordinates(match):
return f"{x} {y}"
def convert_wkt_to_local(wkt_str):
def convert_wkt_to_local(wkt_str: str) -> str:
"""Convert WKT from WGS84 to UTM32N"""
return COORDINATES_REGEX.sub(convert_coordinates, wkt_str)
......@@ -245,14 +246,16 @@ Examples:
def main(location_name: str, wkt_or_zipcode: str, download_only: bool = False,
simstadt_folder: Path | None = None, output_folder: Path | None = None):
simstadt_folder: Path | str | None = None, output_folder: Path | str | None = None) -> Path | None:
"""Main function to process arguments and run the download/extraction"""
# Validate location name
if ' ' in location_name:
raise ValueError("Location name should not contain spaces: 'Some City' -> 'SomeCity'")
if not output_folder:
if output_folder:
output_folder = Path(output_folder)
else:
# Create output directory
output_folder = SCRIPT_DIR / (location_name + '.proj')
......@@ -267,19 +270,24 @@ def main(location_name: str, wkt_or_zipcode: str, download_only: bool = False,
# Download region
download_whole_region(output_folder, wkt_str, x1, x2, y1, y2)
gml_path = None
# Extract region if not download-only
if not download_only:
simstadt_folder = simstadt_folder or find_simstadt_folder()
if not simstadt_folder:
if simstadt_folder:
simstadt_folder = Path(simstadt_folder)
else:
logger.error(
"No SimStadt installation found! Please provide --simstadt-folder or use --download-only.")
return
extract_region(output_folder, location_name, wkt_str, simstadt_folder)
gml_path = extract_region(output_folder, location_name, wkt_str, simstadt_folder)
else:
logger.info("Download-only mode: Skipping region extraction.")
logger.info("Processing of %s complete!", location_name)
return gml_path
if __name__ == '__main__':
......
......@@ -6,6 +6,9 @@ from pathlib import Path
import download_LoD2_from_LGL_BW as bw_data
# Just a few buildings in Schwarzwald
KALTENBRONN_WKT = "POLYGON ((8.4266 48.704415, 8.43926 48.704415, 8.43926 48.709031, 8.4266 48.709031, 8.4266 48.704415))"
class TestUtils(unittest.TestCase):
def test_simstadt_is_available(self):
self.assertIsNotNone(bw_data.find_simstadt_folder(),
......@@ -32,12 +35,45 @@ class TestGetOpenData(unittest.TestCase):
def test_get_schlossplatz(self):
# Stuttgart - Schloßplatz
stuttgart_wkt = "POLYGON ((9.179206 48.779454, 9.179292 48.777644, 9.178498 48.776753, 9.18118 48.775933, 9.181952 48.77705, 9.182768 48.77862, 9.179206 48.779454))"
bw_data.main("Schlossplatz", stuttgart_wkt)
gml_path = bw_data.main("Schlossplatz", stuttgart_wkt,
output_folder=self.tempFolder / 'Schlossplatz.proj')
self.assertTrue(gml_path.exists())
zip_path = gml_path.parent / "LoD2_32_513_5402_2_bw.zip"
self.assertTrue(zip_path.exists())
with open(gml_path) as gml:
content = gml.read()
self.assertIn("LoD2_32_513_5402_1_BW", content)
self.assertIn("EPSG:25832", content)
self.assertIn("DEBW_5221000539k", content)
self.assertIn("<bldg:function>31001_3010</bldg:function>", content)
def test_get_schwarzwald(self):
# Reichental
reichental_wkt = "POLYGON ((8.424282 48.745889, 8.381882 48.737286, 8.382397 48.721773, 8.439903 48.726869, 8.424282 48.745889))"
bw_data.main("Reichental", reichental_wkt)
gml_path = bw_data.main("Kaltenbronn", KALTENBRONN_WKT,
output_folder=self.tempFolder / 'Kaltenbronn.proj')
self.assertTrue(gml_path.exists())
with open(gml_path) as gml:
content = gml.read()
self.assertIn("EPSG:25832", content)
self.assertIn("DEBW_B010000BSWW", content)
def test_only_download(self):
gml_path = bw_data.main("JustDownload", KALTENBRONN_WKT, download_only=True,
output_folder=self.tempFolder / 'JustDownload')
self.assertIsNone(gml_path)
gml_path = self.tempFolder / 'JustDownload' / "JustDownload.gml"
self.assertFalse(gml_path.exists())
zip_path = self.tempFolder / 'JustDownload' / "LoD2_32_457_5394_2_bw.zip"
self.assertTrue(zip_path.exists())
self.assertEqual(len(list(self.tempFolder.glob('JustDownload/**/*.gml'))),
4, "4 tiles should have been downloaded")
def test_wrong_simstadt(self):
self.assertRaisesRegex(ValueError, "RegionChooser failed", bw_data.main,
"WrongSimStadt", KALTENBRONN_WKT, simstadt_folder="/Surely/Not/Here/",
output_folder=self.tempFolder / 'WrongSimStadt'
)
@classmethod
def tearDownClass(cls):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment