forked from s_ranjbar/city_retrofit
182 lines
7.7 KiB
Python
182 lines
7.7 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 city_model_structure.attributes.surface import Surface
|
|
from helpers.geometry_helper import GeometryHelper
|
|
|
|
|
|
class CityGml:
|
|
"""
|
|
CityGml class
|
|
"""
|
|
def __init__(self, path):
|
|
self._city = None
|
|
with open(path) as gml:
|
|
# Clean the namespaces is an important task to prevent wrong ns:field due poor citygml implementations
|
|
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=('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']:
|
|
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']
|
|
|
|
@property
|
|
def content(self):
|
|
"""
|
|
CityGml raw content
|
|
:return: str
|
|
"""
|
|
return self._gml
|
|
|
|
@property
|
|
def city(self) -> City:
|
|
"""
|
|
City model structure enriched with the geometry information
|
|
: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 'lod1Solid' in o['Building']:
|
|
lod += 1
|
|
surfaces = CityGml._lod1_solid(o)
|
|
elif 'lod1MultiSurface' in o['Building']:
|
|
lod += 1
|
|
surfaces = CityGml._lod1_multi_surface(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)
|
|
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)
|
|
if 'lod3Solid' in o['Building']:
|
|
lod += 4
|
|
if 'lod4Solid' in o['Building']:
|
|
lod += 8
|
|
name = o['Building']['@id']
|
|
lod_terrain_str = 'lod' + str(lod) + 'TerrainIntersection'
|
|
terrains = []
|
|
if lod_terrain_str in o['Building']:
|
|
terrains = self._terrains(o, lod_terrain_str)
|
|
year_of_construction = None
|
|
function = None
|
|
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, terrains, year_of_construction, function,
|
|
self._lower_corner))
|
|
return self._city
|
|
|
|
def _terrains(self, city_object, lod_terrain_str):
|
|
try:
|
|
curves = [c['LineString']['posList']['#text']
|
|
for c in city_object['Building'][lod_terrain_str]['MultiCurve']['curveMember']]
|
|
except TypeError:
|
|
curves = [c['LineString']['posList']
|
|
for c in city_object['Building'][lod_terrain_str]['MultiCurve']['curveMember']]
|
|
terrains = []
|
|
for curve in curves:
|
|
curve_points = np.fromstring(curve, dtype=float, sep=' ')
|
|
curve_points = self._geometry.to_points_matrix(curve_points)
|
|
terrains.append(curve_points)
|
|
return terrains
|
|
|
|
@staticmethod
|
|
def _lod1_solid(o):
|
|
try:
|
|
surfaces = [Surface(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']['#text']))
|
|
for s in o['Building']['lod1Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']]
|
|
except TypeError:
|
|
surfaces = [Surface(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']))
|
|
for s in o['Building']['lod1Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']]
|
|
return surfaces
|
|
|
|
@staticmethod
|
|
def _lod1_multi_surface(o):
|
|
surfaces = [Surface(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']))
|
|
for s in o['Building']['lod1MultiSurface']['MultiSurface']['surfaceMember']]
|
|
return surfaces
|
|
|
|
@staticmethod
|
|
def _lod2_solid_multi_surface(o):
|
|
surfaces = [Surface(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']))
|
|
for s in o['Building']['lod2MultiSurface']['MultiSurface']['surfaceMember']]
|
|
return surfaces
|
|
|
|
@staticmethod
|
|
def _lod2_composite_surface(s):
|
|
surfaces = [Surface(CityGml._remove_last_point(sm['Polygon']['exterior']['LinearRing']['posList']))
|
|
for sm in s['CompositeSurface']['surfaceMember']]
|
|
return surfaces
|
|
|
|
@staticmethod
|
|
def _lod2_multi_surface(s, surface_type):
|
|
# todo: this need to be changed into surface bounded?
|
|
try:
|
|
surfaces = [Surface(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']['#text']),
|
|
surface_type=GeometryHelper.gml_surface_to_libs(surface_type))]
|
|
except TypeError:
|
|
surfaces = [Surface(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']),
|
|
surface_type=GeometryHelper.gml_surface_to_libs(surface_type))]
|
|
return surfaces
|
|
|
|
@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])
|