hub/tests/test_city_merge.py

109 lines
4.8 KiB
Python
Raw Permalink Normal View History

2022-10-20 11:34:22 -04:00
"""
TestCityMerge test and validate the merge of several cities into one
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca
"""
2023-08-07 12:32:33 -04:00
2023-06-06 17:44:07 -04:00
import copy
import distutils.spawn
import subprocess
2022-10-20 11:34:22 -04:00
from pathlib import Path
from unittest import TestCase
2023-06-06 17:44:07 -04:00
from hub.city_model_structure.city import City
from hub.imports.geometry_factory import GeometryFactory
2023-06-06 17:44:07 -04:00
from hub.imports.results_factory import ResultFactory
from hub.exports.exports_factory import ExportsFactory
import hub.helpers.constants as cte
2022-10-20 11:34:22 -04:00
class TestCityMerge(TestCase):
"""
Functional TestCityMerge
"""
def setUp(self) -> None:
"""
Test setup
:return: None
"""
self._example_path = (Path(__file__).parent / 'tests_data').resolve()
self._output_path = (Path(__file__).parent / 'tests_outputs').resolve()
self._executable = 'sra'
2022-10-20 11:34:22 -04:00
def test_merge(self):
2023-07-25 15:36:49 -04:00
file_path = Path(self._example_path / 'test.geojson').resolve()
2023-06-06 17:44:07 -04:00
full_city = GeometryFactory('geojson', file_path, height_field='citygml_me').city
self.assertEqual(17, len(full_city.buildings), 'Wrong number of buildings')
odd_city = City(full_city.lower_corner, full_city.upper_corner, full_city.srs_name)
2023-07-10 14:55:36 -04:00
even_city = City(full_city.lower_corner, full_city.upper_corner, full_city.srs_name)
2023-06-06 17:44:07 -04:00
for building in full_city.buildings:
if int(building.name) % 2 == 0:
2023-07-10 14:55:36 -04:00
even_city.add_city_object(copy.deepcopy(building))
2023-06-06 17:44:07 -04:00
else:
odd_city.add_city_object(copy.deepcopy(building))
self.assertEqual(8, len(odd_city.buildings), 'Wrong number of odd buildings')
2023-07-10 14:55:36 -04:00
self.assertEqual(9, len(even_city.buildings), 'Wrong number of par buildings')
merged_city = odd_city.merge(even_city)
2023-06-06 17:44:07 -04:00
self.assertEqual(17, len(merged_city.buildings), 'Wrong number of buildings in merged city')
2023-07-10 14:55:36 -04:00
merged_city = even_city.merge(odd_city)
2023-06-06 17:44:07 -04:00
self.assertEqual(17, len(merged_city.buildings), 'Wrong number of buildings in merged city')
2023-07-10 14:55:36 -04:00
merged_city = full_city.merge(odd_city).merge(even_city)
2023-06-06 17:44:07 -04:00
self.assertEqual(17, len(merged_city.buildings), 'Wrong number of buildings in merged city')
def test_merge_with_radiation(self):
2023-06-06 17:44:07 -04:00
sra = distutils.spawn.find_executable('sra')
2023-07-25 15:36:49 -04:00
file_path = Path(self._example_path / 'test.geojson').resolve()
2023-06-06 17:44:07 -04:00
full_city = GeometryFactory('geojson', file_path, height_field='citygml_me').city
2023-07-10 14:55:36 -04:00
even_city = City(full_city.lower_corner, full_city.upper_corner, full_city.srs_name)
2023-06-06 17:44:07 -04:00
for building in full_city.buildings:
if int(building.name) % 2 == 0:
2023-07-10 14:55:36 -04:00
even_city.add_city_object(copy.deepcopy(building))
2023-07-25 15:36:49 -04:00
ExportsFactory('sra', full_city, self._output_path).export()
sra_file = str((self._output_path / f'{full_city.name}_sra.xml').resolve())
2023-06-06 17:44:07 -04:00
subprocess.run([sra, sra_file], stdout=subprocess.DEVNULL)
2023-07-25 15:36:49 -04:00
ResultFactory('sra', full_city, self._output_path).enrich()
2023-06-06 17:44:07 -04:00
self.assertEqual(17, len(full_city.buildings), 'Wrong number of buildings')
2023-07-10 14:55:36 -04:00
merged_city = full_city.merge(even_city)
full_city_building_total_radiation = 0
for building in merged_city.buildings:
for surface in building.surfaces:
if surface.global_irradiance:
2023-08-07 12:32:33 -04:00
full_city_building_total_radiation += surface.global_irradiance[cte.YEAR][0]
2023-07-10 14:55:36 -04:00
2023-06-07 12:55:03 -04:00
merged_city_building_total_radiation = 0
for building in merged_city.buildings:
for surface in building.surfaces:
if surface.global_irradiance:
2023-08-07 12:32:33 -04:00
merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR][0]
2023-07-10 14:55:36 -04:00
self.assertEqual(full_city_building_total_radiation, merged_city_building_total_radiation)
merged_city = even_city.merge(full_city)
2023-06-07 12:55:03 -04:00
merged_city_building_total_radiation = 0
for building in merged_city.buildings:
for surface in building.surfaces:
if surface.global_irradiance:
2023-08-07 12:32:33 -04:00
merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR][0]
2023-07-10 14:55:36 -04:00
self.assertEqual(full_city_building_total_radiation, merged_city_building_total_radiation)
2023-06-07 12:55:03 -04:00
2023-07-10 14:55:36 -04:00
for building in even_city.buildings:
2023-06-07 12:55:03 -04:00
for surface in building.surfaces:
2023-08-07 12:32:33 -04:00
surface.global_irradiance[cte.YEAR] = [3]
2023-06-07 12:55:03 -04:00
2023-07-10 14:55:36 -04:00
merged_city = full_city.merge(even_city)
2023-07-10 15:03:04 -04:00
first_merged_city_building_total_radiation = 0
2023-06-06 17:44:07 -04:00
for building in merged_city.buildings:
for surface in building.surfaces:
2023-06-07 12:55:03 -04:00
if surface.global_irradiance:
2023-08-07 12:32:33 -04:00
first_merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR][0]
2023-07-10 14:55:36 -04:00
merged_city = even_city.merge(full_city)
2023-07-10 15:03:04 -04:00
second_merged_city_building_total_radiation = 0
2023-06-06 17:44:07 -04:00
for building in merged_city.buildings:
for surface in building.surfaces:
2023-06-07 12:55:03 -04:00
if surface.global_irradiance:
2023-08-07 12:32:33 -04:00
second_merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR][0]
2023-08-08 13:05:55 -04:00
self.assertAlmostEqual(first_merged_city_building_total_radiation, second_merged_city_building_total_radiation, 8)
2023-06-06 17:44:07 -04:00