From 81324c945e7899cc55882844b224d78783bc666d Mon Sep 17 00:00:00 2001 From: guille Date: Thu, 3 Jun 2021 10:12:06 -0400 Subject: [PATCH] Partial correction citygml read --- helpers/geometry_helper.py | 9 -- imports/geometry/citygml.py | 25 +++- .../{citygml_tools.py => citygml_base.py} | 27 +++- imports/geometry/citygml_lod1.py | 26 +++- imports/geometry/citygml_lod2.py | 116 +++++++++++++----- imports/geometry/helpers/geometry_helper.py | 11 +- non_functional_tests/test_geometry_factory.py | 5 +- non_functional_tests/test_sensors_factory.py | 1 + 8 files changed, 167 insertions(+), 53 deletions(-) rename imports/geometry/{citygml_tools.py => citygml_base.py} (54%) diff --git a/helpers/geometry_helper.py b/helpers/geometry_helper.py index 317b4427..30a8ddf2 100644 --- a/helpers/geometry_helper.py +++ b/helpers/geometry_helper.py @@ -160,15 +160,6 @@ class GeometryHelper: return [trimesh_1, trimesh_2] - @staticmethod - def gml_surface_to_libs(surface): - if surface == 'WallSurface': - return 'Wall' - elif surface == 'GroundSurface': - return 'Ground' - else: - return 'Roof' - @staticmethod def get_location(latitude, longitude): url = 'https://nominatim.openstreetmap.org/reverse?lat={latitude}&lon={longitude}&format=json' diff --git a/imports/geometry/citygml.py b/imports/geometry/citygml.py index 9c915f29..5bcbe109 100644 --- a/imports/geometry/citygml.py +++ b/imports/geometry/citygml.py @@ -20,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 @@ -64,6 +66,9 @@ class CityGml: """ return self._gml + def _create_building(self): + raise NotImplementedError + @property def city(self) -> City: """ @@ -71,20 +76,29 @@ 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 + 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 = CityGmlLod1.lod1_solid(o) elif 'lod1MultiSurface' in o['Building']: lod += 1 surfaces = CityGmlLod1.lod1_multi_surface(o) - elif 'lod2Solid' in o['Building']: + + elif 'lod2Solid' in o['Building'] : lod += 1 surfaces = CityGmlLod2.lod2_solid(o) elif 'lod2MultiSurface' in o['Building']: @@ -101,7 +115,7 @@ class CityGml: lod += 4 if 'lod4Solid' in o['Building']: lod += 8 - + ''' lod_terrain_str = 'lod' + str(lod) + 'TerrainIntersection' terrains = [] if lod_terrain_str in o['Building']: @@ -116,6 +130,7 @@ class CityGml: 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): 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..8b80c7dd 100644 --- a/imports/geometry/citygml_lod2.py +++ b/imports/geometry/citygml_lod2.py @@ -1,65 +1,121 @@ -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) + for p in o["Building"]["consistsOfBuildingPart"]: + raise NotImplementedError + 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 6d5995c2..42b0e6c0 100644 --- a/imports/geometry/helpers/geometry_helper.py +++ b/imports/geometry/helpers/geometry_helper.py @@ -314,4 +314,13 @@ class GeometryHelper: :return: Boolean """ delta = self.distance_between_points(v1, v2) - return delta <= delta_max \ No newline at end of file + return delta <= delta_max + + @staticmethod + def gml_surface_to_libs(surface): + if surface == 'WallSurface': + return 'Wall' + elif surface == 'GroundSurface': + return 'Ground' + else: + 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 a309e0d1..bab49a85 100644 --- a/non_functional_tests/test_sensors_factory.py +++ b/non_functional_tests/test_sensors_factory.py @@ -53,6 +53,7 @@ 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')