diff --git a/tests/test_exports.py b/tests/test_exports.py new file mode 100644 index 0000000..e719790 --- /dev/null +++ b/tests/test_exports.py @@ -0,0 +1,150 @@ +""" +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 +import os +from pathlib import Path +from unittest import TestCase + +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 +from hub.imports.construction_factory import ConstructionFactory +from hub.imports.geometry_factory import GeometryFactory +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 + 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: + 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): + """ + 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) + 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) + 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') + os.unlink(geojson_file) # todo: this test need to cover a multipolygon example too + + def test_energy_ade_export(self): + """ + export to energy ADE + """ + self._export_building_energy('energy_ade') + + def test_sra_export(self): + """ + export to SRA + """ + self._export('sra') + + def test_idf_export(self): + """ + export to IDF + """ + file = 'test.geojson' + file_path = (self._example_path / file).resolve() + city = GeometryFactory('geojson', + 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 + + self.assertIsNotNone(city, 'city is none') + EnergyBuildingsExportsFactory('idf', city, self._output_path).export() + ConstructionFactory('nrcan', city).enrich() + EnergyBuildingsExportsFactory('idf', city, self._output_path).export() + UsageFactory('nrcan', city).enrich() + WeatherFactory('epw', city).enrich() + print(self._output_path) + try: + EnergyBuildingsExportsFactory('idf', city, self._output_path).export() + except Exception: + self.fail("Idf ExportsFactory raised ExceptionType unexpectedly!") diff --git a/tests/tests_data/one_building_in_kelowna.gml b/tests/tests_data/one_building_in_kelowna.gml new file mode 100644 index 0000000..6036f4e --- /dev/null +++ b/tests/tests_data/one_building_in_kelowna.gml @@ -0,0 +1,409 @@ + + + + + 326011.03601000085 5526048.416990001 -1.6000000000058208 + 329466.6600299999 5529018.72205 9.80000000000291 + + + + + + 291 + + + m2 + + residential + 1996 + 5.3 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 329231.5010599997 5528270.404139999 4.311470000000554 329229.15295 5528271.14002 3.8000000000029104 329229.30395000055 5528269.304020001 3.8000000000029104 329231.5010599997 5528270.404139999 4.311470000000554 + + + + + + + + + + + + + + + + + 329248.8121399991 5528267.658840001 4.925719999999274 329254.11205999926 5528262.99903 3.8000000000029104 329253.52796000056 5528272.956 3.8000000000029104 329248.8121399991 5528267.658840001 4.925719999999274 + + + + + + + + + + + + + + + + + 329246.16602000035 5528272.533020001 0 329246.16602000035 5528272.533020001 3.8000000000029104 329253.52796000056 5528272.956 3.8000000000029104 329253.52796000056 5528272.956 0 329246.16602000035 5528272.533020001 0 + + + + + + + + + + + + + + + + + 329241.7199700009 5528276.307010001 0 329241.7199700009 5528276.307010001 3.8000000000029104 329246.16602000035 5528272.533020001 3.8000000000029104 329246.16602000035 5528272.533020001 0 329241.7199700009 5528276.307010001 0 + + + + + + + + + + + + + + + + + 329237.9890100006 5528272.159 0 329237.9890100006 5528272.159 3.8000000000029104 329241.7199700009 5528276.307010001 3.8000000000029104 329241.7199700009 5528276.307010001 0 329237.9890100006 5528272.159 0 + + + + + + + + + + + + + + + + + 329233.3360600006 5528276.213989999 0 329233.3360600006 5528276.213989999 3.8000000000029104 329237.9890100006 5528272.159 3.8000000000029104 329237.9890100006 5528272.159 0 329233.3360600006 5528276.213989999 0 + + + + + + + + + + + + + + + + + 329229.15295 5528271.14002 0 329229.15295 5528271.14002 3.8000000000029104 329233.3360600006 5528276.213989999 3.8000000000029104 329233.3360600006 5528276.213989999 0 329229.15295 5528271.14002 0 + + + + + + + + + + + + + + + + + 329229.30395000055 5528269.304020001 0 329229.30395000055 5528269.304020001 3.8000000000029104 329229.15295 5528271.14002 3.8000000000029104 329229.15295 5528271.14002 0 329229.30395000055 5528269.304020001 0 + + + + + + + + + + + + + + + + + 329242.40003000014 5528257.71503 0 329242.40003000014 5528257.71503 3.8000000000029104 329229.30395000055 5528269.304020001 3.8000000000029104 329229.30395000055 5528269.304020001 0 329242.40003000014 5528257.71503 0 + + + + + + + + + + + + + + + + + 329247.3289800007 5528262.52503 0 329247.3289800007 5528262.52503 3.8000000000029104 329242.40003000014 5528257.71503 3.8000000000029104 329242.40003000014 5528257.71503 0 329247.3289800007 5528262.52503 0 + + + + + + + + + + + + + + + + + 329254.11205999926 5528262.99903 0 329254.11205999926 5528262.99903 3.8000000000029104 329247.3289800007 5528262.52503 3.8000000000029104 329247.3289800007 5528262.52503 0 329254.11205999926 5528262.99903 0 + + + + + + + + + + + + + + + + + 329253.52796000056 5528272.956 0 329253.52796000056 5528272.956 3.8000000000029104 329254.11205999926 5528262.99903 3.8000000000029104 329254.11205999926 5528262.99903 0 329253.52796000056 5528272.956 0 + + + + + + + + + + + + + + + + + 329253.52796000056 5528272.956 0 329254.11205999926 5528262.99903 0 329247.3289800007 5528262.52503 0 329242.40003000014 5528257.71503 0 329229.30395000055 5528269.304020001 0 329229.15295 5528271.14002 0 329233.3360600006 5528276.213989999 0 329237.9890100006 5528272.159 0 329241.7199700009 5528276.307010001 0 329246.16602000035 5528272.533020001 0 329253.52796000056 5528272.956 0 + + + + + + + + + + + + + + + + + 329246.16602000035 5528272.533020001 3.8000000000029104 329244.33748999983 5528267.074109999 4.999100000000908 329245.1323099993 5528267.42457 4.930840000000899 329248.8121399991 5528267.658840001 4.925719999999274 329253.52796000056 5528272.956 3.8000000000029104 329246.16602000035 5528272.533020001 3.8000000000029104 + + + + + + + + + + + + + + + + + 329241.7199700009 5528276.307010001 3.8000000000029104 329242.3462899998 5528267.00502 5.30000000000291 329244.33748999983 5528267.074109999 4.999100000000908 329246.16602000035 5528272.533020001 3.8000000000029104 329241.7199700009 5528276.307010001 3.8000000000029104 + + + + + + + + + + + + + + + + + 329237.9890100006 5528272.159 3.8000000000029104 329238.32637000084 5528266.609999999 4.6887600000045495 329242.1777599994 5528266.829500001 5.298219999996945 329242.3462899998 5528267.00502 5.30000000000291 329241.7199700009 5528276.307010001 3.8000000000029104 329237.9890100006 5528272.159 3.8000000000029104 + + + + + + + + + + + + + + + + + 329233.3360600006 5528276.213989999 3.8000000000029104 329233.80010999925 5528270.5848900005 4.683640000002924 329238.32637000084 5528266.609999999 4.6887600000045495 329237.9890100006 5528272.159 3.8000000000029104 329233.3360600006 5528276.213989999 3.8000000000029104 + + + + + + + + + + + + + + + + + 329229.15295 5528271.14002 3.8000000000029104 329231.5010599997 5528270.404139999 4.311470000000554 329233.80010999925 5528270.5848900005 4.683640000002924 329233.3360600006 5528276.213989999 3.8000000000029104 329229.15295 5528271.14002 3.8000000000029104 + + + + + + + + + + + + + + + + + 329242.40003000014 5528257.71503 3.8000000000029104 329242.1777599994 5528266.829500001 5.298219999996945 329238.32637000084 5528266.609999999 4.6887600000045495 329233.80010999925 5528270.5848900005 4.683640000002924 329231.5010599997 5528270.404139999 4.311470000000554 329229.30395000055 5528269.304020001 3.8000000000029104 329242.40003000014 5528257.71503 3.8000000000029104 + + + + + + + + + + + + + + + + + 329247.3289800007 5528262.52503 3.8000000000029104 329245.1323099993 5528267.42457 4.930840000000899 329244.33748999983 5528267.074109999 4.999100000000908 329242.3462899998 5528267.00502 5.30000000000291 329242.1777599994 5528266.829500001 5.298219999996945 329242.40003000014 5528257.71503 3.8000000000029104 329247.3289800007 5528262.52503 3.8000000000029104 + + + + + + + + + + + + + + + + + 329254.11205999926 5528262.99903 3.8000000000029104 329248.8121399991 5528267.658840001 4.925719999999274 329245.1323099993 5528267.42457 4.930840000000899 329247.3289800007 5528262.52503 3.8000000000029104 329254.11205999926 5528262.99903 3.8000000000029104 + + + + + + + + + + +