forked from s_ranjbar/city_retrofit
118 lines
5.0 KiB
Python
118 lines
5.0 KiB
Python
"""
|
|
CityGml module parses citygml files and import the geometry into the city model structure
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
|
|
"""
|
|
import numpy as np
|
|
import xmltodict
|
|
|
|
from city_model_structure.city import City
|
|
from city_model_structure.building import Building
|
|
from helpers.geometry_helper import GeometryHelper
|
|
from imports.geometry.citygml_lod2 import CityGmlLod2
|
|
from imports.geometry.citygml_lod1 import CityGmlLod1
|
|
from city_model_structure.parts_consisting_building import PartsConsistingBuilding
|
|
|
|
|
|
class CityGml:
|
|
"""
|
|
CityGml class
|
|
"""
|
|
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
|
|
fl = ('cityObjectMember', 'curveMember', 'boundedBy', 'surfaceMember', 'consistsOfBuildingPart')
|
|
self._gml = xmltodict.parse(gml.read(), process_namespaces=True, xml_attribs=True, namespaces={
|
|
'http://www.opengis.net/gml': None,
|
|
'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,
|
|
'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
|
|
}, force_list=fl)
|
|
|
|
self._city_objects = None
|
|
self._geometry = GeometryHelper()
|
|
|
|
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']
|
|
print(f'gmlread {path}')
|
|
|
|
@property
|
|
def content(self):
|
|
"""
|
|
CityGml raw content
|
|
:return: str
|
|
"""
|
|
return self._gml
|
|
|
|
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, city_object):
|
|
name = city_object['@id']
|
|
building_parts = []
|
|
for part in city_object['consistsOfBuildingPart']:
|
|
building = self._create_building(part['BuildingPart'])
|
|
print(f'add city building part {building.name}')
|
|
self._city.add_city_object(building)
|
|
building_parts.append(building)
|
|
print(f'add building cluster {name}')
|
|
return PartsConsistingBuilding(name, building_parts)
|
|
|
|
@property
|
|
def city(self) -> City:
|
|
"""
|
|
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']:
|
|
city_object = o['Building']
|
|
if 'consistsOfBuildingPart' in city_object:
|
|
print('create building parts')
|
|
self._city.add_city_objects_cluster(self._create_parts_consisting_building(city_object))
|
|
else:
|
|
self._city.add_city_object(self._create_building(city_object))
|
|
return self._city
|