2020-06-09 14:07:47 -04:00
|
|
|
"""
|
2021-06-04 09:22:06 -04:00
|
|
|
CityGml module parses citygml_classes files and import the geometry into the city model structure
|
2020-06-09 14:07:47 -04:00
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
2022-04-08 09:35:33 -04:00
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
|
2020-06-09 14:07:47 -04:00
|
|
|
"""
|
2022-06-07 13:31:35 -04:00
|
|
|
|
2020-05-18 13:25:08 -04:00
|
|
|
import numpy as np
|
2020-06-16 10:34:17 -04:00
|
|
|
import xmltodict
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.city_model_structure.city import City
|
|
|
|
from hub.city_model_structure.building import Building
|
|
|
|
from hub.city_model_structure.parts_consisting_building import PartsConsistingBuilding
|
|
|
|
from hub.helpers.geometry_helper import GeometryHelper
|
|
|
|
from hub.imports.geometry.citygml_classes.citygml_lod2 import CityGmlLod2
|
|
|
|
from hub.imports.geometry.citygml_classes.citygml_lod1 import CityGmlLod1
|
2020-05-18 13:25:08 -04:00
|
|
|
|
2020-06-23 14:48:01 -04:00
|
|
|
|
2020-05-18 13:25:08 -04:00
|
|
|
class CityGml:
|
2020-06-11 15:45:11 -04:00
|
|
|
"""
|
|
|
|
CityGml class
|
|
|
|
"""
|
2022-11-24 17:58:45 -05:00
|
|
|
def __init__(self, path, extrusion_height_field=None, year_of_construction_field=None, function_field=None):
|
2020-05-18 13:25:08 -04:00
|
|
|
self._city = None
|
2022-11-25 15:32:31 -05:00
|
|
|
self._lod = None
|
2021-06-03 10:12:06 -04:00
|
|
|
self._lod1_tags = ['lod1Solid', 'lod1MultiSurface']
|
|
|
|
self._lod2_tags = ['lod2Solid', 'lod2MultiSurface', 'lod2MultiCurve']
|
2022-11-24 17:58:45 -05:00
|
|
|
self._extrusion_height_field = extrusion_height_field
|
|
|
|
self._year_of_construction_field = year_of_construction_field
|
|
|
|
self._function_field = function_field
|
2022-06-07 13:31:35 -04:00
|
|
|
self._lower_corner = None
|
|
|
|
self._upper_corner = None
|
2020-05-18 13:25:08 -04:00
|
|
|
with open(path) as gml:
|
2021-06-04 09:22:06 -04:00
|
|
|
# Clean the namespaces is an important task to prevent wrong ns:field due poor citygml_classes implementations
|
2021-08-30 11:38:28 -04:00
|
|
|
force_list = ('cityObjectMember', 'curveMember', 'boundedBy', 'surfaceMember', 'consistsOfBuildingPart')
|
2020-05-18 13:25:08 -04:00
|
|
|
self._gml = xmltodict.parse(gml.read(), process_namespaces=True, xml_attribs=True, namespaces={
|
|
|
|
'http://www.opengis.net/gml': None,
|
2020-06-22 13:26:50 -04:00
|
|
|
'http://www.opengis.net/citygml/1.0': None,
|
|
|
|
'http://www.opengis.net/citygml/building/1.0': None,
|
|
|
|
'http://schemas.opengis.net/citygml/building/1.0/building.xsd': None,
|
|
|
|
'http://www.opengis.net/citygml/appearance/1.0': None,
|
|
|
|
'http://schemas.opengis.net/citygml/appearance/1.0/appearance.xsd': None,
|
|
|
|
'http://www.opengis.net/citygml/relief/1.0': None,
|
|
|
|
'http://schemas.opengis.net/citygml/relief/1.0/relief.xsd': None,
|
|
|
|
'http://www.opengis.net/citygml/generics/1.0': None,
|
2020-05-18 13:25:08 -04:00
|
|
|
'http://www.w3.org/2001/XMLSchema-instance': None,
|
|
|
|
'urn:oasis:names:tc:ciq:xsdschema:xAL:2.0': None,
|
|
|
|
'http://www.w3.org/1999/xlink': None,
|
|
|
|
'http://www.opengis.net/citygml/relief/2.0': None,
|
|
|
|
'http://www.opengis.net/citygml/building/2.0': None,
|
|
|
|
'http://www.opengis.net/citygml/building/2.0 http://schemas.opengis.net/citygml/building/2.0/building.xsd '
|
|
|
|
'http://www.opengis.net/citygml/relief/2.0 http://schemas.opengis.net/citygml/relief/2.0/relief.xsd" '
|
|
|
|
'xmlns="http://www.opengis.net/citygml/2.0': None,
|
|
|
|
'http://www.opengis.net/citygml/2.0': None
|
2021-08-30 11:38:28 -04:00
|
|
|
}, force_list=force_list)
|
2021-06-03 12:42:00 -04:00
|
|
|
|
2020-06-16 16:19:14 -04:00
|
|
|
self._city_objects = None
|
2022-06-07 13:31:35 -04:00
|
|
|
if 'boundedBy' in self._gml['CityModel']:
|
|
|
|
for bound in self._gml['CityModel']['boundedBy']:
|
|
|
|
envelope = bound['Envelope']
|
|
|
|
if '#text' in envelope['lowerCorner']:
|
|
|
|
self._lower_corner = np.fromstring(envelope['lowerCorner']['#text'], dtype=float, sep=' ')
|
|
|
|
self._upper_corner = np.fromstring(envelope['upperCorner']['#text'], dtype=float, sep=' ')
|
|
|
|
else:
|
|
|
|
self._lower_corner = np.fromstring(envelope['lowerCorner'], dtype=float, sep=' ')
|
|
|
|
self._upper_corner = np.fromstring(envelope['upperCorner'], dtype=float, sep=' ')
|
|
|
|
if '@srsName' in envelope:
|
|
|
|
self._srs_name = envelope['@srsName']
|
2022-11-17 15:56:49 -05:00
|
|
|
else:
|
|
|
|
# If not coordinate system given assuming hub standard
|
2022-11-28 13:13:55 -05:00
|
|
|
self._srs_name = "EPSG:26911"
|
2022-06-07 13:31:35 -04:00
|
|
|
else:
|
|
|
|
# get the boundary from the city objects instead
|
|
|
|
for city_object_member in self._gml['CityModel']['cityObjectMember']:
|
|
|
|
city_object = city_object_member['Building']
|
|
|
|
if 'boundedBy' in city_object:
|
|
|
|
for bound in city_object['boundedBy']:
|
|
|
|
if 'Envelope' not in bound:
|
|
|
|
continue
|
|
|
|
envelope = bound['Envelope']
|
|
|
|
self._srs_name = envelope['@srsName']
|
|
|
|
if '#text' in envelope['lowerCorner']:
|
|
|
|
lower_corner = np.fromstring(envelope['lowerCorner']['#text'], dtype=float, sep=' ')
|
|
|
|
upper_corner = np.fromstring(envelope['upperCorner']['#text'], dtype=float, sep=' ')
|
|
|
|
else:
|
|
|
|
lower_corner = np.fromstring(envelope['lowerCorner'], dtype=float, sep=' ')
|
|
|
|
upper_corner = np.fromstring(envelope['upperCorner'], dtype=float, sep=' ')
|
|
|
|
if self._lower_corner is None:
|
|
|
|
self._lower_corner = lower_corner
|
|
|
|
self._upper_corner = upper_corner
|
|
|
|
else:
|
|
|
|
self._lower_corner[0] = min(self._lower_corner[0], lower_corner[0])
|
|
|
|
self._lower_corner[1] = min(self._lower_corner[1], lower_corner[1])
|
|
|
|
self._lower_corner[2] = min(self._lower_corner[2], lower_corner[2])
|
|
|
|
self._upper_corner[0] = max(self._upper_corner[0], upper_corner[0])
|
|
|
|
self._upper_corner[1] = max(self._upper_corner[1], upper_corner[1])
|
|
|
|
self._upper_corner[2] = max(self._upper_corner[2], upper_corner[2])
|
2020-05-18 13:25:08 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def content(self):
|
2020-06-11 15:45:11 -04:00
|
|
|
"""
|
2021-08-30 14:39:24 -04:00
|
|
|
Get cityGml raw content
|
2020-06-11 15:45:11 -04:00
|
|
|
:return: str
|
|
|
|
"""
|
2020-05-18 13:25:08 -04:00
|
|
|
return self._gml
|
|
|
|
|
2021-06-03 11:16:21 -04:00
|
|
|
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):
|
2022-11-25 15:25:59 -05:00
|
|
|
if self._lod is None or self._lod > 1:
|
|
|
|
self._lod = 1
|
2021-06-03 11:16:21 -04:00
|
|
|
surfaces = CityGmlLod1(city_object).surfaces
|
|
|
|
elif any(key in city_object for key in self._lod2_tags):
|
2022-11-25 15:25:59 -05:00
|
|
|
if self._lod is None or self._lod > 2:
|
|
|
|
self._lod = 2
|
2021-06-03 11:16:21 -04:00
|
|
|
surfaces = CityGmlLod2(city_object).surfaces
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("Not supported level of detail")
|
2022-11-28 13:13:55 -05:00
|
|
|
return Building(name, surfaces, year_of_construction, function, terrains=None)
|
2021-06-03 11:16:21 -04:00
|
|
|
|
2021-06-03 12:42:00 -04:00
|
|
|
def _create_parts_consisting_building(self, city_object):
|
|
|
|
name = city_object['@id']
|
|
|
|
building_parts = []
|
|
|
|
for part in city_object['consistsOfBuildingPart']:
|
|
|
|
building = self._create_building(part['BuildingPart'])
|
|
|
|
self._city.add_city_object(building)
|
|
|
|
building_parts.append(building)
|
|
|
|
return PartsConsistingBuilding(name, building_parts)
|
2021-06-03 10:12:06 -04:00
|
|
|
|
2020-05-18 13:25:08 -04:00
|
|
|
@property
|
2020-06-11 15:45:11 -04:00
|
|
|
def city(self) -> City:
|
|
|
|
"""
|
2021-08-30 14:39:24 -04:00
|
|
|
Get city model structure enriched with the geometry information
|
2020-06-11 15:45:11 -04:00
|
|
|
:return: City
|
|
|
|
"""
|
2021-06-03 11:16:21 -04:00
|
|
|
|
2020-05-18 13:25:08 -04:00
|
|
|
if self._city is None:
|
2020-05-28 12:07:36 -04:00
|
|
|
self._city = City(self._lower_corner, self._upper_corner, self._srs_name)
|
2021-08-30 11:38:28 -04:00
|
|
|
for city_object_member in self._gml['CityModel']['cityObjectMember']:
|
|
|
|
city_object = city_object_member['Building']
|
2021-06-03 11:16:21 -04:00
|
|
|
if 'consistsOfBuildingPart' in city_object:
|
2021-06-03 12:42:00 -04:00
|
|
|
self._city.add_city_objects_cluster(self._create_parts_consisting_building(city_object))
|
2020-06-22 13:46:41 -04:00
|
|
|
else:
|
2021-06-03 11:16:21 -04:00
|
|
|
self._city.add_city_object(self._create_building(city_object))
|
2022-11-25 15:25:59 -05:00
|
|
|
self._city.level_of_detail.geometry = self._lod
|
2020-05-18 13:25:08 -04:00
|
|
|
return self._city
|