diff --git a/imports/geometry/citygml.py b/imports/geometry/citygml.py index b051988f..f70b6b15 100644 --- a/imports/geometry/citygml.py +++ b/imports/geometry/citygml.py @@ -8,9 +8,10 @@ import xmltodict from city_model_structure.city import City from city_model_structure.building import Building -from city_model_structure.attributes.surface import Surface -from imports.geometry.helpers.geometry_helper import GeometryHelper +from helpers.geometry_helper import GeometryHelper from city_model_structure.attributes.polygon import Polygon +from imports.geometry.citygml_lod2 import CityGmlLod2 +from imports.geometry.citygml_lod1 import CityGmlLod1 class CityGml: @@ -19,6 +20,8 @@ class CityGml: """ def __init__(self, path): self._city = None + self._lod1_tags = ['lod1Solid', 'lod1MultiSurface'] + self._lod2_tags = ['lod2Solid', 'lod2MultiSurface', 'lod2MultiCurve'] with open(path) as gml: # Clean the namespaces is an important task to prevent wrong ns:field due poor citygml implementations @@ -62,6 +65,9 @@ class CityGml: """ return self._gml + def _create_building(self): + raise NotImplementedError + @property def city(self) -> City: """ @@ -69,35 +75,45 @@ class CityGml: :return: City """ if self._city is None: - # todo: refactor this method to clearly choose the gml type self._city = City(self._lower_corner, self._upper_corner, self._srs_name) - i = 0 - building_part = None + for o in self._gml['CityModel']['cityObjectMember']: - i += 1 lod = 0 surfaces = [] + if any(key in o['Building'] for key in self._lod1_tags): + lod = 1 + surfaces = CityGmlLod1(o).surfaces + elif any(key in o['Building'] for key in self._lod2_tags): + lod = 2 + surfaces = CityGmlLod2(o).surfaces + elif 'consistsOfBuildingPart' in o['Building']: + raise NotImplementedError("Building cluster") + ''' if 'lod1Solid' in o['Building']: lod += 1 - surfaces = CityGml._lod1_solid(o) + surfaces = CityGmlLod1.lod1_solid(o) elif 'lod1MultiSurface' in o['Building']: lod += 1 - surfaces = CityGml._lod1_multi_surface(o) + surfaces = CityGmlLod1.lod1_multi_surface(o) + + elif 'lod2Solid' in o['Building'] : + lod += 1 + surfaces = CityGmlLod2.lod2_solid(o) elif 'lod2MultiSurface' in o['Building']: # todo: check if this is a real case or a miss-formed citygml lod = 2 - surfaces = surfaces + CityGml._lod2_solid_multi_surface(o) + surfaces = surfaces + CityGmlLod2.lod2_solid_multi_surface(o) else: for bound in o['Building']['boundedBy']: surface_type = next(iter(bound)) if 'lod2MultiSurface' in bound[surface_type]: lod = 2 - surfaces = surfaces + CityGml._lod2(bound) + surfaces = surfaces + CityGmlLod2.lod2(bound) if 'lod3Solid' in o['Building']: lod += 4 if 'lod4Solid' in o['Building']: lod += 8 - + ''' lod_terrain_str = 'lod' + str(lod) + 'TerrainIntersection' terrains = [] if lod_terrain_str in o['Building']: @@ -105,22 +121,14 @@ class CityGml: function = None year_of_construction = None - if 'consistsOfBuildingPart' in o['Building']: - if 'BuildingPart' in o['Building']['consistsOfBuildingPart']: - name = o['Building']['consistsOfBuildingPart']['BuildingPart']['name'] - if 'yearOfConstruction' in o['Building']['consistsOfBuildingPart']['BuildingPart']: - year_of_construction = o['Building']['consistsOfBuildingPart']['BuildingPart']['yearOfConstruction'] - if 'function' in o['Building']['consistsOfBuildingPart']['BuildingPart']: - function = o['Building']['consistsOfBuildingPart']['BuildingPart']['function'] - - else: - name = o['Building']['@id'] - if 'yearOfConstruction' in o['Building']: - year_of_construction = o['Building']['yearOfConstruction'] - if 'function' in o['Building']: - function = o['Building']['function'] + name = o['Building']['@id'] + if 'yearOfConstruction' in o['Building']: + year_of_construction = o['Building']['yearOfConstruction'] + if 'function' in o['Building']: + function = o['Building']['function'] self._city.add_city_object(Building(name, lod, surfaces, year_of_construction, function, self._lower_corner, terrains)) + return self._city def _terrains(self, city_object, lod_terrain_str): @@ -137,84 +145,6 @@ class CityGml: terrains.append(curve_points) return terrains - @staticmethod - def _lod1_solid(o): - try: - solid_points = [CityGml._solid_points(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']['#text'])) - for s in o['Building']['lod1Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']] - except TypeError: - solid_points = [CityGml._solid_points(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) - for s in o['Building']['lod1Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']] - - return [Surface(Polygon(sp),Polygon(sp)) for sp in solid_points] - - @staticmethod - def _lod1_multi_surface(o): - solid_points = [CityGml._solid_points(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) - for s in o['Building']['lod1MultiSurface']['MultiSurface']['surfaceMember']] - return [Surface(Polygon(sp),Polygon(sp)) for sp in solid_points] - - @staticmethod - def _lod2_solid_multi_surface(o): - if 'boundedBy' in o['Building']['consistsOfBuildingPart']['BuildingPart']: - if 'RoofSurface' in o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']: - if o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']['RoofSurface']['lod2MultiSurface'] != 'None': - polygons = [Polygon(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) - for s in o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']['RoofSurface']['lod2MultiSurface']['MultiSurface']['surfaceMember']] - - elif 'WallSurface' in o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']: - if o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']['WallSurface']['lod2MultiSurface'] != 'None': - polygons = [Polygon(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) - for s in o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']['WallSurface']['lod2MultiSurface']['MultiSurface']['surfaceMember']] - else: - polygons = [Polygon(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) - for s in o['Building']['lod2MultiSurface']['MultiSurface']['surfaceMember']] - return [Surface(p, p) for p in polygons] - - @staticmethod - def _lod2_composite_surface(s): - solid_points = [CityGml._solid_points((CityGml._remove_last_point(sm['Polygon']['exterior']['LinearRing']['posList']))) - for sm in s['CompositeSurface']['surfaceMember']] - return [Surface(Polygon(sp), Polygon(sp)) for sp in solid_points] - - @staticmethod - def _lod2_multi_surface(s, surface_type): - # todo: this need to be changed into surface bounded? - try: - solid_points = [CityGml._solid_points(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'] - ['#text']))] - except TypeError: - solid_points = [CityGml._solid_points(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing'] - ['posList']))] - return [Surface(Polygon(sp), Polygon(sp), surface_type=GeometryHelper.gml_surface_to_libs(surface_type)) for sp in solid_points] - - @staticmethod - def _lod2(bound): - surfaces = [] - for surface_type in iter(bound): - for s in bound[surface_type]['lod2MultiSurface']['MultiSurface']['surfaceMember']: - if 'CompositeSurface' in s: - surfaces = surfaces + CityGml._lod2_composite_surface(s) - else: - surfaces = surfaces + CityGml._lod2_multi_surface(s, surface_type) - return surfaces - - @staticmethod - def _remove_last_point(points): - array = points.split(' ') - res = " " - return res.join(array[0:len(array) - 3]) - - @staticmethod - def _solid_points(coordinates) -> np.ndarray: - """ - Solid surface point matrix [[x, y, z],[x, y, z],...] - :parameter coordinates: string from file - :return: np.ndarray - """ - solid_points = np.fromstring(coordinates, dtype=float, sep=' ') - solid_points = GeometryHelper.to_points_matrix(solid_points) - return solid_points @staticmethod def _holes_points(holes_coordinates) -> [np.ndarray]: diff --git a/imports/geometry/citygml_tools.py b/imports/geometry/citygml_base.py similarity index 54% rename from imports/geometry/citygml_tools.py rename to imports/geometry/citygml_base.py index 5ff4407b..343d104c 100644 --- a/imports/geometry/citygml_tools.py +++ b/imports/geometry/citygml_base.py @@ -1,8 +1,15 @@ +from abc import ABC import numpy as np from imports.geometry.helpers.geometry_helper import GeometryHelper -class CityGmlTools: +class CityGmlBase(ABC): + def __init__(self): + self._surfaces = [] + + @property + def surfaces(self): + return self._surfaces @staticmethod def _remove_last_point(points): @@ -19,4 +26,20 @@ class CityGmlTools: """ solid_points = np.fromstring(coordinates, dtype=float, sep=' ') solid_points = GeometryHelper.to_points_matrix(solid_points) - return solid_points \ No newline at end of file + return solid_points + + @classmethod + def _solid(cls, o): + raise NotImplementedError + + @classmethod + def _multi_surface(cls, o): + raise NotImplementedError + + @classmethod + def _multi_curve(cls, o): + raise NotImplementedError + + @classmethod + def _building_parts(cls, o): + raise NotImplementedError diff --git a/imports/geometry/citygml_lod1.py b/imports/geometry/citygml_lod1.py index 3040fc52..a9cd94a3 100644 --- a/imports/geometry/citygml_lod1.py +++ b/imports/geometry/citygml_lod1.py @@ -1,25 +1,41 @@ -from imports.geometry.citygml_tools import CityGmlTools +from imports.geometry.citygml_base import CityGmlBase from city_model_structure.attributes.surface import Surface from city_model_structure.attributes.polygon import Polygon -class CityGmlLod1(CityGmlTools): +class CityGmlLod1(CityGmlBase): + + @classmethod + def _multi_curve(cls, o): + pass + + @classmethod + def _multi_surface(cls, o): + pass + + @classmethod + def _solid(cls, o): + pass + + def __init__(self, o): + super().__init__() + self._o = o @staticmethod def lod1_solid(o): try: solid_points = [ - CityGmlTools._solid_points(CityGmlTools._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']['#text'])) + CityGmlBase._solid_points(CityGmlBase._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']['#text'])) for s in o['Building']['lod1Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']] except TypeError: solid_points = [ - CityGmlTools._solid_points(CityGmlTools._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) + CityGmlBase._solid_points(CityGmlBase._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) for s in o['Building']['lod1Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']] return [Surface(Polygon(sp), Polygon(sp)) for sp in solid_points] @staticmethod def lod1_multi_surface(o): - solid_points = [CityGmlTools._solid_points(CityGmlTools._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) + solid_points = [CityGmlBase._solid_points(CityGmlBase._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) for s in o['Building']['lod1MultiSurface']['MultiSurface']['surfaceMember']] return [Surface(Polygon(sp), Polygon(sp)) for sp in solid_points] \ No newline at end of file diff --git a/imports/geometry/citygml_lod2.py b/imports/geometry/citygml_lod2.py index 479b6ace..910656b2 100644 --- a/imports/geometry/citygml_lod2.py +++ b/imports/geometry/citygml_lod2.py @@ -1,65 +1,119 @@ -from imports.geometry.citygml_tools import CityGmlTools + +from imports.geometry.citygml_base import CityGmlBase from city_model_structure.attributes.surface import Surface from city_model_structure.attributes.polygon import Polygon +from imports.geometry.helpers.geometry_helper import GeometryHelper + +class CityGmlLod2(CityGmlBase): -class CityGmlLod2(CityGmlTools): + def __init__(self, o): + super().__init__() + self._o = o + self._surfaces = self._identify(self._o) + + @classmethod + def _identify(cls, o): + if 'lod2Solid' in o['Building']: + return cls._solid(o) + elif 'lod2MultiSurface' in o['Building']: + print('multi_surface') + return cls._multi_surface(o) + elif 'lod2MultiCurve' in o['Building']: + print('multi_curve') + return cls._multi_curve(o) + elif 'consistsOfBuildingPart' in o['Building']: + raise NotImplementedError + @staticmethod - def _lod2_composite_surface(s): - solid_points = [ - CityGmlTools._solid_points((CityGmlTools._remove_last_point(sm['Polygon']['exterior']['LinearRing']['posList']))) - for sm in s['CompositeSurface']['surfaceMember']] - return [Surface(Polygon(sp), Polygon(sp)) for sp in solid_points] + def _surface_encoding(surfaces): + if 'lod2MultiSurface' in surfaces: + return 'lod2MultiSurface', 'MultiSurface' + return 'unknown' - @staticmethod - def _lod2_multi_surface(s, surface_type): - # todo: this need to be changed into surface bounded? - try: - solid_points = [CityGmlTools._solid_points(CityGmlTools._remove_last_point( - s['Polygon']['exterior']['LinearRing']['posList']['#text']))] - except TypeError: - solid_points = [CityGmlTools._solid_points(CityGmlTools._remove_last_point(s['Polygon']['exterior']['LinearRing'] - ['posList']))] - return [Surface(Polygon(sp), Polygon(sp), surface_type=surface_type) for sp in solid_points] + @classmethod + def _solid(cls, o): + surfaces = [] + for b in o["Building"]["boundedBy"]: + surface_type = next(iter(b)) + surface_encoding, surface_subtype = cls._surface_encoding(b[surface_type]) + for member in b[surface_type][surface_encoding][surface_subtype]['surfaceMember']: + sp = cls._solid_points(cls._remove_last_point(member['Polygon']['exterior']['LinearRing']['posList'])) + p = Polygon(sp) + surface = Surface(p,p, surface_type=GeometryHelper.gml_surface_to_libs(surface_type)) + surfaces.append(surface) + return surfaces - @staticmethod - def lod2(bound): + + @classmethod + def _multi_curve(cls, o): + raise NotImplementedError + + @classmethod + def _multi_surface(cls, o): + raise NotImplementedError + + + @classmethod + def _lod2(cls, bound): surfaces = [] for surface_type in iter(bound): for s in bound[surface_type]['lod2MultiSurface']['MultiSurface']['surfaceMember']: if 'CompositeSurface' in s: - surfaces = surfaces + CityGmlLod2._lod2_composite_surface(s) + surfaces = surfaces + cls._lod2_composite_surface(s) else: - surfaces = surfaces + CityGmlLod2._lod2_multi_surface(s, surface_type) + surfaces = surfaces + cls._lod2_multi_surface(s, surface_type) return surfaces - @staticmethod - def lod2_solid_multi_surface(o): + @classmethod + def _lod2_solid_multi_surface(cls, o): polygons = None if 'boundedBy' in o['Building']['consistsOfBuildingPart']['BuildingPart']: if 'RoofSurface' in o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']: if o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']['RoofSurface']['lod2MultiSurface'] != 'None': - polygons = [Polygon(CityGmlTools._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) + polygons = [Polygon(cls._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) for s in o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']['RoofSurface'] ['lod2MultiSurface']['MultiSurface']['surfaceMember']] elif 'WallSurface' in o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']: if o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']['WallSurface']['lod2MultiSurface'] != 'None': - polygons = [Polygon(CityGmlTools._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) + polygons = [Polygon(cls._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) for s in o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']['WallSurface']['lod2MultiSurface']['MultiSurface']['surfaceMember']] else: - polygons = [Polygon(CityGmlTools._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) + polygons = [Polygon(cls._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) for s in o['Building']['lod2MultiSurface']['MultiSurface']['surfaceMember']] return [Surface(p,p) for p in polygons] - @staticmethod - def lod2_solid(o): + @classmethod + def _lod2_solid(cls, o): + for walls in o['Building']['boundedBy']['wallSurface']: + print(f'solid') + + @classmethod + def _lod2_solid_composite_surface(cls, o): try: - solid_points = [CityGmlTools._solid_points(CityGmlTools._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']['#text'])) + solid_points = [cls._solid_points(cls._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']['#text'])) for s in o['Building']['lod2Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']] except TypeError: - solid_points = [CityGmlTools._solid_points(CityGmlTools._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) + solid_points = [cls._solid_points(cls._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) for s in o['Building']['lod2Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']] return [Surface(Polygon(sp),Polygon(sp)) for sp in solid_points] + + @classmethod + def _lod2_composite_surface(cls, s): + solid_points = [ + cls._solid_points((cls._remove_last_point(sm['Polygon']['exterior']['LinearRing']['posList']))) + for sm in s['CompositeSurface']['surfaceMember']] + return [Surface(Polygon(sp), Polygon(sp)) for sp in solid_points] + + @classmethod + def _lod2_multi_surface(cls, s, surface_type): + # todo: this need to be changed into surface bounded? + try: + solid_points = [cls._solid_points(cls._remove_last_point( + s['Polygon']['exterior']['LinearRing']['posList']['#text']))] + except TypeError: + solid_points = [cls._solid_points(cls._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']))] + return [Surface(Polygon(sp), Polygon(sp), surface_type=surface_type) for sp in solid_points] \ No newline at end of file diff --git a/imports/geometry/helpers/geometry_helper.py b/imports/geometry/helpers/geometry_helper.py index 5db04968..42b0e6c0 100644 --- a/imports/geometry/helpers/geometry_helper.py +++ b/imports/geometry/helpers/geometry_helper.py @@ -323,4 +323,4 @@ class GeometryHelper: elif surface == 'GroundSurface': return 'Ground' else: - return 'Roof' + return 'Roof' \ No newline at end of file diff --git a/non_functional_tests/test_geometry_factory.py b/non_functional_tests/test_geometry_factory.py index b0a8edcf..765a7863 100644 --- a/non_functional_tests/test_geometry_factory.py +++ b/non_functional_tests/test_geometry_factory.py @@ -52,7 +52,9 @@ class TestGeometryFactory(TestCase): file = '20190815_mitte_out_MC_FloursurfaceADD.gml' city = self._get_citygml(file) for building in city.buildings: - self.assertIsNotNone(building.volume, 'building volume is none') + print(f'building {building.name} has {len(building.surfaces)} surfaces {building.volume}') + self.assertFalse(building.volume is 'inf', 'building volume is inf') + print(f'Found {len(city.buildings)} buildings' ) def test_citygml_buildings(self): """ @@ -61,6 +63,7 @@ class TestGeometryFactory(TestCase): """ file = 'one_building_in_kelowna.gml' city = self._get_citygml(file) + self.assertTrue(len(city.buildings) == 1) for building in city.buildings: self.assertIsNotNone(building.name, 'building name is none') self.assertIsNotNone(building.lod, 'building lod is none') diff --git a/non_functional_tests/test_sensors_factory.py b/non_functional_tests/test_sensors_factory.py index 378db55c..7504ace1 100644 --- a/non_functional_tests/test_sensors_factory.py +++ b/non_functional_tests/test_sensors_factory.py @@ -54,5 +54,6 @@ class TestSensorsFactory(TestCase): update = pd.DataFrame([['2020-01-19 23:55:00', '12345.0']], columns=["Date time", "Energy consumption"]) update = update.astype({"Date time": 'datetime64', "Energy consumption": 'float64'}) sensor.add_period(update) + row = sensor.measures.loc[sensor.measures["Date time"] == '2020-01-19 23:55:00']['Energy consumption'].iloc[0] self.assertTrue(f'{row}' == '12345.0')