diff --git a/imports/geometry/citygml.py b/imports/geometry/citygml.py index f70b6b15..e0a2fb9f 100644 --- a/imports/geometry/citygml.py +++ b/imports/geometry/citygml.py @@ -9,7 +9,6 @@ import xmltodict from city_model_structure.city import City from city_model_structure.building import Building 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 @@ -46,6 +45,7 @@ class CityGml: 'http://www.opengis.net/citygml/2.0': None }, force_list=('cityObjectMember', 'curveMember', 'boundedBy', 'surfaceMember')) self._city_objects = None + self._geometry = GeometryHelper() for bound in self._gml['CityModel']['boundedBy']: envelope = bound['Envelope'] if '#text' in envelope['lowerCorner']: @@ -65,7 +65,25 @@ class CityGml: """ return self._gml - def _create_building(self): + def _create_building(self, city_object): + name = city_object['@id'] + function = None + year_of_construction = None + if 'yearOfConstruction' in city_object: + year_of_construction = city_object['yearOfConstruction'] + if 'function' in city_object: + function = city_object['function'] + if any(key in city_object for key in self._lod1_tags): + lod = 1 + surfaces = CityGmlLod1(city_object).surfaces + elif any(key in city_object for key in self._lod2_tags): + lod = 2 + surfaces = CityGmlLod2(city_object).surfaces + else: + raise NotImplementedError("Not supported level of detail") + return Building(name, lod, surfaces, year_of_construction, function, self._lower_corner, []) + + def _create_parts_consisting_building(self): raise NotImplementedError @property @@ -74,19 +92,13 @@ class CityGml: City model structure enriched with the geometry information :return: City """ + if self._city is None: self._city = City(self._lower_corner, self._upper_corner, self._srs_name) for o in self._gml['CityModel']['cityObjectMember']: - 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']: + city_object = o['Building'] + if 'consistsOfBuildingPart' in city_object: raise NotImplementedError("Building cluster") ''' if 'lod1Solid' in o['Building']: @@ -104,31 +116,7 @@ class CityGml: lod = 2 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 + 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']: - terrains = self._terrains(o, lod_terrain_str) - - function = None - year_of_construction = None - 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)) - + self._city.add_city_object(self._create_building(city_object)) return self._city def _terrains(self, city_object, lod_terrain_str): @@ -141,7 +129,7 @@ class CityGml: terrains = [] for curve in curves: curve_points = np.fromstring(curve, dtype=float, sep=' ') - curve_points = GeometryHelper.to_points_matrix(curve_points) + curve_points = self._geometry.to_points_matrix(curve_points) terrains.append(curve_points) return terrains @@ -186,7 +174,7 @@ class CityGml: for point in perimeter_points: duplicated_point = False for p in pv: - if GeometryHelper.almost_equal(0.0, p, point): + if GeometryHelper().almost_equal(0.0, p, point): duplicated_point = True if not duplicated_point: pv = np.append(pv, [point], axis=0) diff --git a/imports/geometry/citygml_base.py b/imports/geometry/citygml_base.py index 343d104c..db473e6c 100644 --- a/imports/geometry/citygml_base.py +++ b/imports/geometry/citygml_base.py @@ -1,14 +1,26 @@ +""" +CityGmlBase module abstract class to template the different level of details +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2021 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca +""" + from abc import ABC import numpy as np from imports.geometry.helpers.geometry_helper import GeometryHelper class CityGmlBase(ABC): + """ + CityGmlBase class inherited by the specific level of detail classes. + """ def __init__(self): self._surfaces = [] @property def surfaces(self): + """ + parsed surfaces + """ return self._surfaces @staticmethod @@ -19,11 +31,6 @@ class CityGmlBase(ABC): @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 @@ -39,7 +46,3 @@ class CityGmlBase(ABC): @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 a9cd94a3..7c829442 100644 --- a/imports/geometry/citygml_lod1.py +++ b/imports/geometry/citygml_lod1.py @@ -1,9 +1,18 @@ +""" +CityGmlLod1 module parses citygml files with level of detail 1 and import the geometry into the city model structure +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2021 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca +""" + 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(CityGmlBase): + """ + CityGmlLod1 class to parse level of detail 1 city gml files + """ @classmethod def _multi_curve(cls, o): diff --git a/imports/geometry/citygml_lod2.py b/imports/geometry/citygml_lod2.py index 910656b2..bcf26d0b 100644 --- a/imports/geometry/citygml_lod2.py +++ b/imports/geometry/citygml_lod2.py @@ -1,12 +1,19 @@ +""" +CityGmlLod1 module parses citygml files with level of detail 1 and import the geometry into the city model structure +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2021 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca +""" 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): - - + """ + CityGmlLod1 class to parse level of detail 1 city gml files + """ def __init__(self, o): super().__init__() self._o = o @@ -14,38 +21,32 @@ class CityGmlLod2(CityGmlBase): @classmethod def _identify(cls, o): - if 'lod2Solid' in o['Building']: + if 'lod2Solid' in o: return cls._solid(o) - elif 'lod2MultiSurface' in o['Building']: - print('multi_surface') + elif 'lod2MultiSurface' in o: return cls._multi_surface(o) - elif 'lod2MultiCurve' in o['Building']: - print('multi_curve') + elif 'lod2MultiCurve' in o: return cls._multi_curve(o) - elif 'consistsOfBuildingPart' in o['Building']: - raise NotImplementedError - @staticmethod def _surface_encoding(surfaces): if 'lod2MultiSurface' in surfaces: return 'lod2MultiSurface', 'MultiSurface' - return 'unknown' + raise NotImplementedError('unknown surface type') @classmethod def _solid(cls, o): surfaces = [] - for b in o["Building"]["boundedBy"]: + for b in o["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)) + surface = Surface(p, p, surface_type=GeometryHelper.gml_surface_to_libs(surface_type)) surfaces.append(surface) return surfaces - @classmethod def _multi_curve(cls, o): raise NotImplementedError @@ -53,67 +54,3 @@ class CityGmlLod2(CityGmlBase): @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 + cls._lod2_composite_surface(s) - else: - surfaces = surfaces + cls._lod2_multi_surface(s, surface_type) - return surfaces - - @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(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(cls._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList'])) - for s in o['Building']['consistsOfBuildingPart']['BuildingPart']['boundedBy']['WallSurface']['lod2MultiSurface']['MultiSurface']['surfaceMember']] - else: - 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] - - - @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 = [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 = [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