summer_course_2024/tests/test_city_merge.py

110 lines
4.9 KiB
Python
Raw 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-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-07 12:55:03 -04:00
import pandas as pd
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-06-06 17:44:07 -04:00
file_path = Path('./tests_data/test.geojson').resolve()
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')
file_path = Path('./tests_data/test.geojson').resolve()
output_path = Path('./tests_outputs/')
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-06-06 17:44:07 -04:00
ExportsFactory('sra', full_city, output_path).export()
sra_file = str((output_path / f'{full_city.name}_sra.xml').resolve())
subprocess.run([sra, sra_file], stdout=subprocess.DEVNULL)
ResultFactory('sra', full_city, output_path).enrich()
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:
full_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 0]
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:
merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 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:
merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 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:
surface.global_irradiance[cte.YEAR] = pd.DataFrame([3], columns=['sra_mockup_value'])
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-07-10 15:03:04 -04:00
first_merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 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-07-10 15:03:04 -04:00
second_merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 0]
self.assertEqual(first_merged_city_building_total_radiation, second_merged_city_building_total_radiation)
2023-06-06 17:44:07 -04:00