2020-06-09 14:07:47 -04:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
2020-05-18 13:25:08 -04:00
|
|
|
import numpy as np
|
2020-06-16 10:34:17 -04:00
|
|
|
import xmltodict
|
|
|
|
|
2020-05-18 13:25:08 -04:00
|
|
|
from city_model_structure.city import City
|
2020-06-16 15:12:18 -04:00
|
|
|
from city_model_structure.building import Building
|
2020-05-18 13:25:08 -04:00
|
|
|
from city_model_structure.surface import Surface
|
2020-06-11 16:22:58 -04:00
|
|
|
from helpers.geometry_helper import GeometryHelper
|
2020-06-22 13:26:50 -04:00
|
|
|
from datetime import datetime
|
2020-05-18 13:25:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
class CityGml:
|
2020-06-11 15:45:11 -04:00
|
|
|
"""
|
|
|
|
CityGml class
|
|
|
|
"""
|
2020-05-18 13:25:08 -04:00
|
|
|
def __init__(self, path):
|
2020-06-22 13:26:50 -04:00
|
|
|
start = datetime.utcnow()
|
2020-05-18 13:25:08 -04:00
|
|
|
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,
|
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
|
2020-06-22 13:26:50 -04:00
|
|
|
}, force_list=('cityObjectMember', 'curveMember', 'boundedBy', 'surfaceMember'))
|
2020-06-16 16:19:14 -04:00
|
|
|
self._city_objects = None
|
2020-06-11 16:22:58 -04:00
|
|
|
self._geometry = GeometryHelper()
|
2020-06-22 13:26:50 -04:00
|
|
|
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=' ')
|
2020-05-18 13:25:08 -04:00
|
|
|
|
2020-06-22 13:26:50 -04:00
|
|
|
self._srs_name = envelope['@srsName']
|
|
|
|
print('city_gml constructor', datetime.utcnow() - start)
|
2020-05-18 13:25:08 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def content(self):
|
2020-06-11 15:45:11 -04:00
|
|
|
"""
|
|
|
|
CityGml raw content
|
|
|
|
:return: str
|
|
|
|
"""
|
2020-05-18 13:25:08 -04:00
|
|
|
return self._gml
|
|
|
|
|
|
|
|
@property
|
2020-06-11 15:45:11 -04:00
|
|
|
def city(self) -> City:
|
|
|
|
"""
|
|
|
|
City model structure enriched with the geometry information
|
|
|
|
:return: City
|
|
|
|
"""
|
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)
|
2020-06-22 13:26:50 -04:00
|
|
|
i = 0
|
2020-05-18 13:25:08 -04:00
|
|
|
for o in self._gml['CityModel']['cityObjectMember']:
|
2020-06-22 13:26:50 -04:00
|
|
|
i += 1
|
|
|
|
print('add city object', i)
|
2020-05-18 13:25:08 -04:00
|
|
|
lod = 0
|
2020-06-22 13:26:50 -04:00
|
|
|
surfaces = []
|
2020-05-18 13:25:08 -04:00
|
|
|
if 'lod1Solid' in o['Building']:
|
|
|
|
lod += 1
|
2020-06-22 13:26:50 -04:00
|
|
|
surfaces = self._lod1(o)
|
2020-06-22 13:46:41 -04:00
|
|
|
else:
|
|
|
|
for bound in o['Building']['boundedBy']:
|
|
|
|
surface_type = next(iter(bound))
|
|
|
|
if 'lod2MultiSurface' in bound[surface_type]:
|
|
|
|
start = datetime.utcnow()
|
|
|
|
lod = 2
|
|
|
|
surfaces = CityGml._lod2(bound)
|
|
|
|
print('lod2 ', surface_type, datetime.utcnow() - start)
|
|
|
|
if 'lod3Solid' in o['Building']:
|
|
|
|
lod += 4
|
|
|
|
if 'lod4Solid' in o['Building']:
|
|
|
|
lod += 8
|
2020-06-22 13:26:50 -04:00
|
|
|
|
2020-05-18 13:25:08 -04:00
|
|
|
name = o['Building']['@id']
|
|
|
|
|
2020-06-22 13:26:50 -04:00
|
|
|
lod_terrain_str = 'lod' + str(lod) + 'TerrainIntersection'
|
|
|
|
terrains = []
|
2020-05-18 13:25:08 -04:00
|
|
|
if lod_terrain_str in o['Building']:
|
2020-06-22 13:26:50 -04:00
|
|
|
terrains = self._terrains(o, lod_terrain_str)
|
2020-05-18 13:25:08 -04:00
|
|
|
|
|
|
|
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']
|
2020-06-16 15:12:18 -04:00
|
|
|
self._city.add_city_object(Building(name, lod, surfaces, terrains, year_of_construction, function,
|
|
|
|
self._lower_corner))
|
2020-05-18 13:25:08 -04:00
|
|
|
return self._city
|
2020-06-22 13:26:50 -04:00
|
|
|
|
|
|
|
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, True)
|
|
|
|
terrains.append(curve_points)
|
|
|
|
return terrains
|
|
|
|
|
|
|
|
def _lod1(self, o):
|
|
|
|
try:
|
|
|
|
surfaces = [Surface(s['Polygon']['exterior']['LinearRing']['posList']['#text'])
|
|
|
|
for s in o['Building']['lod1Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']]
|
|
|
|
except TypeError:
|
|
|
|
surfaces = [Surface(s['Polygon']['exterior']['LinearRing']['posList'])
|
|
|
|
for s in o['Building']['lod1Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']]
|
|
|
|
terrains = []
|
|
|
|
for s in surfaces:
|
|
|
|
ground = True
|
|
|
|
ground_points = []
|
|
|
|
for row in s.points:
|
|
|
|
# all surface vertex are in the lower z
|
|
|
|
ground_point = [row[0], row[1], self._lower_corner[2]]
|
|
|
|
ground_points = np.concatenate((ground_points, ground_point), axis=None)
|
|
|
|
ground = ground and self._geometry.almost_equal(row, ground_point)
|
|
|
|
if ground:
|
|
|
|
ground_points = self._geometry.to_points_matrix(ground_points)
|
|
|
|
# Do i need to remove the duplicated?
|
|
|
|
terrains.append(ground_points)
|
|
|
|
return surfaces
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _lod2(bound):
|
|
|
|
|
|
|
|
surfaces = []
|
|
|
|
for surface_type in iter(bound):
|
|
|
|
for s in bound[surface_type]['lod2MultiSurface']['MultiSurface']['surfaceMember']:
|
|
|
|
try:
|
|
|
|
surfaces = [Surface(s['Polygon']['exterior']['LinearRing']['posList']['#text'],
|
|
|
|
surface_type=GeometryHelper.gml_surface_to_libs(surface_type))
|
|
|
|
for s in bound[surface_type]['lod2MultiSurface']['MultiSurface']['surfaceMember']]
|
|
|
|
except TypeError:
|
|
|
|
surfaces = [Surface(s['Polygon']['exterior']['LinearRing']['posList'],
|
|
|
|
surface_type=GeometryHelper.gml_surface_to_libs(surface_type))
|
|
|
|
for s in bound[surface_type]['lod2MultiSurface']['MultiSurface']['surfaceMember']]
|
|
|
|
print(len(surfaces), 'surfaces')
|
|
|
|
return surfaces
|