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-10-28 13:14:05 -04:00
|
|
|
from city_model_structure.attributes.surface import Surface
|
2020-06-11 16:22:58 -04:00
|
|
|
from helpers.geometry_helper import GeometryHelper
|
2021-04-06 13:48:18 -04:00
|
|
|
from city_model_structure.attributes.polygon import Polygon
|
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
|
|
|
|
"""
|
2020-05-18 13:25:08 -04:00
|
|
|
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
|
2021-03-16 20:14:40 -04:00
|
|
|
|
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
|
2020-12-01 07:33:23 -05: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-10-26 10:00:42 -04:00
|
|
|
if '@srsName' in envelope:
|
|
|
|
self._srs_name = envelope['@srsName']
|
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-12-21 09:42:54 -05:00
|
|
|
# todo: refactor this method to clearly choose the gml type
|
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
|
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-11-27 11:31:25 -05:00
|
|
|
surfaces = CityGml._lod1_solid(o)
|
|
|
|
elif 'lod1MultiSurface' in o['Building']:
|
|
|
|
lod += 1
|
2020-12-21 09:42:54 -05:00
|
|
|
surfaces = CityGml._lod1_multi_surface(o)
|
2020-12-01 07:33:23 -05:00
|
|
|
elif 'lod2MultiSurface' in o['Building']:
|
2020-12-21 09:42:54 -05:00
|
|
|
# todo: check if this is a real case or a miss-formed citygml
|
2020-12-01 07:33:23 -05:00
|
|
|
lod = 2
|
2020-12-21 09:42:54 -05:00
|
|
|
surfaces = surfaces + CityGml._lod2_solid_multi_surface(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]:
|
|
|
|
lod = 2
|
2020-06-26 10:06:43 -04:00
|
|
|
surfaces = surfaces + CityGml._lod2(bound)
|
2020-06-22 13:46:41 -04:00
|
|
|
if 'lod3Solid' in o['Building']:
|
|
|
|
lod += 4
|
|
|
|
if 'lod4Solid' in o['Building']:
|
|
|
|
lod += 8
|
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']
|
2021-04-07 14:20:13 -04:00
|
|
|
self._city.add_city_object(Building(name, lod, surfaces, year_of_construction, function,
|
|
|
|
self._lower_corner, terrains))
|
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=' ')
|
2021-03-08 18:27:14 -05:00
|
|
|
curve_points = self._geometry.to_points_matrix(curve_points)
|
2020-06-22 13:26:50 -04:00
|
|
|
terrains.append(curve_points)
|
|
|
|
return terrains
|
|
|
|
|
2020-11-27 11:31:25 -05:00
|
|
|
@staticmethod
|
|
|
|
def _lod1_solid(o):
|
2020-06-22 13:26:50 -04:00
|
|
|
try:
|
2021-04-06 18:06:19 -04:00
|
|
|
solid_points = [CityGml._solid_points(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']['#text']))
|
2020-06-22 13:26:50 -04:00
|
|
|
for s in o['Building']['lod1Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']]
|
|
|
|
except TypeError:
|
2021-04-06 18:06:19 -04:00
|
|
|
solid_points = [CityGml._solid_points(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']))
|
2020-06-22 13:26:50 -04:00
|
|
|
for s in o['Building']['lod1Solid']['Solid']['exterior']['CompositeSurface']['surfaceMember']]
|
2021-04-06 18:06:19 -04:00
|
|
|
|
|
|
|
return [Surface(Polygon(sp),Polygon(sp)) for sp in solid_points]
|
2020-11-27 11:31:25 -05:00
|
|
|
|
|
|
|
@staticmethod
|
2020-12-21 09:42:54 -05:00
|
|
|
def _lod1_multi_surface(o):
|
2021-04-06 18:06:19 -04:00
|
|
|
solid_points = [CityGml._solid_points(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']))
|
2020-11-27 11:31:25 -05:00
|
|
|
for s in o['Building']['lod1MultiSurface']['MultiSurface']['surfaceMember']]
|
2021-04-06 18:06:19 -04:00
|
|
|
return [Surface(Polygon(sp),Polygon(sp)) for sp in solid_points]
|
2020-06-22 13:26:50 -04:00
|
|
|
|
2020-12-01 07:33:23 -05:00
|
|
|
@staticmethod
|
2020-12-21 09:42:54 -05:00
|
|
|
def _lod2_solid_multi_surface(o):
|
2021-04-06 18:06:19 -04:00
|
|
|
polygons = [Polygon(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']))
|
2020-12-01 07:33:23 -05:00
|
|
|
for s in o['Building']['lod2MultiSurface']['MultiSurface']['surfaceMember']]
|
2021-04-06 18:06:19 -04:00
|
|
|
return [Surface(p,p) for p in polygons]
|
2020-12-01 07:33:23 -05:00
|
|
|
|
2020-12-21 09:42:54 -05:00
|
|
|
@staticmethod
|
|
|
|
def _lod2_composite_surface(s):
|
2021-04-06 18:06:19 -04:00
|
|
|
solid_points = [CityGml._solid_points((CityGml._remove_last_point(sm['Polygon']['exterior']['LinearRing']['posList'])))
|
2020-12-21 09:42:54 -05:00
|
|
|
for sm in s['CompositeSurface']['surfaceMember']]
|
2021-04-06 18:06:19 -04:00
|
|
|
return [Surface(Polygon(sp),Polygon(sp)) for sp in solid_points]
|
2020-12-21 09:42:54 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _lod2_multi_surface(s, surface_type):
|
|
|
|
# todo: this need to be changed into surface bounded?
|
|
|
|
try:
|
2021-04-06 18:06:19 -04:00
|
|
|
solid_points = [CityGml._solid_points(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']['posList']
|
|
|
|
['#text']))]
|
2020-12-21 09:42:54 -05:00
|
|
|
except TypeError:
|
2021-04-06 18:06:19 -04:00
|
|
|
solid_points = [CityGml._solid_points(CityGml._remove_last_point(s['Polygon']['exterior']['LinearRing']
|
|
|
|
['posList']))]
|
|
|
|
return [Surface(Polygon(sp),Polygon(sp), surface_type=surface_type) for sp in solid_points]
|
2020-12-21 09:42:54 -05:00
|
|
|
|
2020-06-22 13:26:50 -04:00
|
|
|
@staticmethod
|
|
|
|
def _lod2(bound):
|
|
|
|
surfaces = []
|
|
|
|
for surface_type in iter(bound):
|
2020-12-21 09:42:54 -05:00
|
|
|
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)
|
2020-06-22 13:26:50 -04:00
|
|
|
return surfaces
|
2021-03-08 12:05:05 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _remove_last_point(points):
|
|
|
|
array = points.split(' ')
|
|
|
|
res = " "
|
2021-03-08 18:27:14 -05:00
|
|
|
return res.join(array[0:len(array) - 3])
|
2021-04-06 13:48:18 -04:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _holes_points(holes_coordinates) -> [np.ndarray]:
|
|
|
|
"""
|
|
|
|
Holes surfaces point matrices [[[x, y, z],[x, y, z],...]]
|
|
|
|
:parameter holes_coordinates: strings from file
|
|
|
|
:return: [np.ndarray]
|
|
|
|
"""
|
|
|
|
holes_points = []
|
|
|
|
for hole_coordinates in holes_coordinates:
|
|
|
|
hole_points = np.fromstring(hole_coordinates, dtype=float, sep=' ')
|
|
|
|
hole_points = GeometryHelper.to_points_matrix(hole_points)
|
|
|
|
holes_points.append(hole_points)
|
|
|
|
return holes_points
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _perimeter_points(coordinates, solid_points, holes_points) -> np.ndarray:
|
|
|
|
"""
|
|
|
|
Matrix of points of the perimeter in the same order as in coordinates [[x, y, z],[x, y, z],...]
|
|
|
|
:parameter coordinates: string from file
|
|
|
|
:parameter solid_points: np.ndarray points that define the solid part of a surface
|
|
|
|
:parameter holes_points: [np.ndarray] points that define each the holes in a surface
|
|
|
|
:return: np.ndarray
|
|
|
|
"""
|
|
|
|
if holes_points is None:
|
|
|
|
perimeter_points = solid_points
|
|
|
|
else:
|
|
|
|
_perimeter_coordinates = coordinates
|
|
|
|
for hole_points in holes_points:
|
|
|
|
_hole = np.append(hole_points, hole_points[0])
|
|
|
|
_closed_hole = ' '.join(str(e) for e in [*_hole[:]])
|
|
|
|
# add a mark 'M' to ensure that the recombination of points does not provoke errors in finding holes
|
|
|
|
_perimeter_coordinates = _perimeter_coordinates.replace(_closed_hole, 'M')
|
|
|
|
_perimeter_coordinates = _perimeter_coordinates.replace('M', '')
|
|
|
|
perimeter_points = np.fromstring(_perimeter_coordinates, dtype=float, sep=' ')
|
|
|
|
perimeter_points = GeometryHelper.to_points_matrix(perimeter_points)
|
|
|
|
# remove duplicated points
|
|
|
|
pv = np.array([perimeter_points[0]])
|
|
|
|
for point in perimeter_points:
|
|
|
|
duplicated_point = False
|
|
|
|
for p in pv:
|
|
|
|
if GeometryHelper().almost_equal(0.0, p, point):
|
|
|
|
duplicated_point = True
|
|
|
|
if not duplicated_point:
|
|
|
|
pv = np.append(pv, [point], axis=0)
|
|
|
|
perimeter_points = pv
|
|
|
|
return perimeter_points
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _holes_polygons(holes_points) -> [Polygon]:
|
|
|
|
"""
|
|
|
|
hole surfaces, a list of hole polygons found in a surface
|
|
|
|
:parameter holes_points: [np.ndarray] that define each of the holes
|
|
|
|
:return: None, [] or [Polygon]
|
|
|
|
None -> not known whether holes exist in reality or not due to low level of detail of input data
|
|
|
|
[] -> no holes in the surface
|
|
|
|
[Polygon] -> one or more holes in the surface
|
|
|
|
"""
|
|
|
|
if holes_points is None:
|
|
|
|
holes_polygons = None
|
|
|
|
else:
|
|
|
|
holes_polygons = []
|
|
|
|
for hole_points in holes_points:
|
|
|
|
holes_polygons.append(Polygon(hole_points))
|
|
|
|
return holes_polygons
|