hub/tests/test_exports.py

168 lines
7.0 KiB
Python
Raw Normal View History

2024-01-25 00:57:00 -05:00
"""
TestExports test and validate the city export formats
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
"""
import json
2023-10-24 02:01:57 -04:00
import os
from pathlib import Path
from unittest import TestCase
2023-11-03 02:21:50 -04:00
import hub.helpers.constants as cte
from hub.city_model_structure.city import City
from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory
from hub.exports.exports_factory import ExportsFactory
from hub.helpers.dictionaries import Dictionaries
2023-01-24 10:51:50 -05:00
from hub.imports.construction_factory import ConstructionFactory
2023-11-03 02:21:50 -04:00
from hub.imports.geometry_factory import GeometryFactory
from hub.imports.results_factory import ResultFactory
2023-01-24 10:51:50 -05:00
from hub.imports.usage_factory import UsageFactory
from hub.imports.weather_factory import WeatherFactory
class TestExports(TestCase):
"""
TestExports class contains the unittest for export functionality
"""
def setUp(self) -> None:
"""
Test setup
:return: None
"""
self._city = None
self._complete_city = None
self._example_path = (Path(__file__).parent / 'tests_data').resolve()
self._output_path = (Path(__file__).parent / 'tests_outputs').resolve()
def _get_citygml(self, file):
file_path = (self._example_path / file).resolve()
self._city = GeometryFactory('citygml', path=file_path).city
self.assertIsNotNone(self._city, 'city is none')
return self._city
def _get_complete_city(self, from_pickle):
if self._complete_city is None:
if from_pickle:
file_path = (self._example_path / 'ConcordiaSWGcampus.pickle').resolve()
self._complete_city = City.load(file_path)
else:
file_path = (self._example_path / 'one_building_in_kelowna.gml').resolve()
self._complete_city = self._get_citygml(file_path)
for building in self._complete_city.buildings:
building.function = Dictionaries().hft_function_to_hub_function[building.function]
building.year_of_construction = 2006
2021-11-16 07:57:47 -05:00
ConstructionFactory('nrel', self._complete_city).enrich()
UsageFactory('nrcan', self._complete_city).enrich()
cli = (self._example_path / 'weather' / 'inseldb_Summerland.cli').resolve()
self._complete_city.climate_file = Path(cli)
self._complete_city.climate_reference_city = 'Summerland'
dummy_measures = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
for building in self._complete_city.buildings:
2023-08-07 12:32:33 -04:00
building.heating_demand[cte.MONTH] = dummy_measures
building.cooling_demand[cte.MONTH] = dummy_measures
building.heating_demand[cte.YEAR] = [0.0]
building.cooling_demand[cte.YEAR] = [0.0]
return self._complete_city
def _export(self, export_type, from_pickle=False):
self._complete_city = self._get_complete_city(from_pickle)
ExportsFactory(export_type, self._complete_city, self._output_path, base_uri='../glb').export()
def _export_building_energy(self, export_type, from_pickle=False):
self._complete_city = self._get_complete_city(from_pickle)
EnergyBuildingsExportsFactory(export_type, self._complete_city, self._output_path).export()
def test_obj_export(self):
2021-08-27 12:51:30 -04:00
"""
export to obj
"""
self._export('obj', False)
def test_cesiumjs_tileset_export(self):
"""
export to cesiumjs tileset
"""
self._export('cesiumjs_tileset', False)
tileset = Path(self._output_path / f'{self._city.name}.json')
self.assertTrue(tileset.exists())
with open(tileset, 'r') as f:
json_tileset = json.load(f)
self.assertEqual(1, len(json_tileset['root']['children']), "Wrong number of children")
def test_glb_export(self):
"""
export to glb format
"""
self._export('glb', False)
2023-11-03 02:21:50 -04:00
for building in self._city.buildings:
glb_file = Path(self._output_path / f'{building.name}.glb')
self.assertTrue(glb_file.exists(), f'{building.name} Building glb wasn\'t correctly generated')
def test_geojson_export(self):
self._export('geojson', False)
2023-10-24 02:00:48 -04:00
geojson_file = Path(self._output_path / f'{self._city.name}.geojson')
self.assertTrue(geojson_file.exists(), f'{geojson_file} doesn\'t exists')
with open(geojson_file, 'r') as f:
geojson = json.load(f)
self.assertEqual(1, len(geojson['features']), 'Wrong number of buildings')
geometry = geojson['features'][0]['geometry']
self.assertEqual('Polygon', geometry['type'], 'Wrong geometry type')
self.assertEqual(1, len(geometry['coordinates']), 'Wrong polygon structure')
self.assertEqual(11, len(geometry['coordinates'][0]), 'Wrong number of vertices')
2023-10-24 02:01:57 -04:00
os.unlink(geojson_file) # todo: this test need to cover a multipolygon example too
def test_energy_ade_export(self):
2021-08-27 12:51:30 -04:00
"""
export to energy ADE
"""
self._export_building_energy('energy_ade')
def test_sra_export(self):
2021-08-27 12:51:30 -04:00
"""
export to SRA
"""
self._export('sra')
def test_idf_export(self):
"""
export to IDF
"""
2024-10-29 16:52:38 -04:00
# file = '1588_v1.geojson'
file = 'test.geojson'
2023-03-23 15:38:38 -04:00
file_path = (self._example_path / file).resolve()
city = GeometryFactory('geojson',
2023-03-23 15:38:38 -04:00
path=file_path,
height_field='citygml_me',
year_of_construction_field='ANNEE_CONS',
function_field='CODE_UTILI',
function_to_hub=Dictionaries().montreal_function_to_hub_function).city
2023-03-23 15:38:38 -04:00
self.assertIsNotNone(city, 'city is none')
2024-09-16 11:34:43 -04:00
#EnergyBuildingsExportsFactory('idf', city, self._output_path).export()
2023-04-12 10:16:14 -04:00
ConstructionFactory('nrcan', city).enrich()
2024-09-16 11:34:43 -04:00
# EnergyBuildingsExportsFactory('idf', city, self._output_path).export()
2023-03-23 15:38:38 -04:00
UsageFactory('nrcan', city).enrich()
WeatherFactory('epw', city).enrich()
try:
2024-10-14 23:24:33 -04:00
idf = EnergyBuildingsExportsFactory('cerc_idf', city, self._output_path).export()
idf.run()
csv_output_path = (self._output_path / f'{city.name}_out.csv').resolve()
ResultFactory('cerc_idf', city, csv_output_path).enrich()
self.assertTrue(csv_output_path.is_file())
2024-11-28 15:46:21 -05:00
for building in city.buildings:
self.assertIsNotNone(building.heating_demand)
self.assertIsNotNone(building.cooling_demand)
self.assertIsNotNone(building.domestic_hot_water_heat_demand)
self.assertIsNotNone(building.lighting_electrical_demand)
self.assertIsNotNone(building.appliances_electrical_demand)
total_demand = sum(building.heating_demand[cte.HOUR])
2024-11-28 15:46:21 -05:00
total_demand_month = sum(building.heating_demand[cte.MONTH])
print(building.heating_demand[cte.YEAR])
print(building.heating_demand[cte.YEAR][0], total_demand_month, total_demand)
self.assertAlmostEqual(total_demand, building.heating_demand[cte.YEAR][0], 2)
total_demand = sum(building.heating_demand[cte.MONTH])
self.assertEqual(total_demand, building.heating_demand[cte.YEAR][0], 2)
except Exception:
self.fail("Idf ExportsFactory raised ExceptionType unexpectedly!")