106 lines
4.7 KiB
Python
106 lines
4.7 KiB
Python
import xmltodict
|
|
import numpy as np
|
|
from city_model_structure.city import City
|
|
from city_model_structure.city_object import CityObject
|
|
from city_model_structure.surface import Surface
|
|
from helpers.geometry import Geometry
|
|
|
|
|
|
class CityGml:
|
|
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.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'))
|
|
self._cityObjects = None
|
|
self._geometry = Geometry()
|
|
envelope = self._gml['CityModel']['boundedBy']['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=' ')
|
|
|
|
self._srs_name = envelope['@srsName']
|
|
|
|
@property
|
|
def content(self):
|
|
return self._gml
|
|
|
|
@property
|
|
def city(self):
|
|
if self._city is None:
|
|
self._city = City(self._lower_corner, self._upper_corner, self._srs_name)
|
|
for o in self._gml['CityModel']['cityObjectMember']:
|
|
lod = 0
|
|
if 'lod1Solid' in o['Building']:
|
|
lod += 1
|
|
if 'lod2Solid' in o['Building']:
|
|
lod += 2
|
|
if 'lod3Solid' in o['Building']:
|
|
lod += 4
|
|
if 'lod4Solid' in o['Building']:
|
|
lod += 8
|
|
# ToDo: this is specific for Lod1 need to be modeled for higher lod's and lod combinations
|
|
name = o['Building']['@id']
|
|
lod_str = 'lod' + str(lod) + 'Solid'
|
|
lod_terrain_str = 'lod' + str(lod) + 'TerrainIntersection'
|
|
try:
|
|
surfaces = [Surface(s['Polygon']['exterior']['LinearRing']['posList']['#text'])
|
|
for s in o['Building'][lod_str]['Solid']['exterior']['CompositeSurface']['surfaceMember']]
|
|
except TypeError:
|
|
surfaces = [Surface(s['Polygon']['exterior']['LinearRing']['posList'])
|
|
for s in o['Building'][lod_str]['Solid']['exterior']['CompositeSurface']['surfaceMember']]
|
|
|
|
if lod_terrain_str in o['Building']:
|
|
try:
|
|
curves = [c['LineString']['posList']['#text']
|
|
for c in o['Building'][lod_terrain_str]['MultiCurve']['curveMember']]
|
|
except TypeError:
|
|
curves = [c['LineString']['posList']
|
|
for c in o['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)
|
|
else:
|
|
|
|
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)
|
|
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(CityObject(name, lod, surfaces, terrains, year_of_construction, function,
|
|
self._lower_corner))
|
|
return self._city
|
|
|
|
|