2022-11-17 15:56:49 -05:00
|
|
|
"""
|
|
|
|
Geojson module parses geojson files and import the geometry into the city model structure
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Guillermo Gutierrez Guillermo.GutierrezMorote@concordia.ca
|
|
|
|
"""
|
2023-03-27 13:55:32 -04:00
|
|
|
|
2023-06-09 13:12:00 -04:00
|
|
|
import uuid
|
2022-11-23 10:20:33 -05:00
|
|
|
import json
|
2022-11-24 17:58:45 -05:00
|
|
|
|
2023-03-15 14:21:38 -04:00
|
|
|
import numpy as np
|
2022-11-24 17:58:45 -05:00
|
|
|
|
2022-11-23 10:20:33 -05:00
|
|
|
from pyproj import Transformer
|
2022-11-24 17:58:45 -05:00
|
|
|
|
2023-01-24 10:51:50 -05:00
|
|
|
import hub.helpers.constants as cte
|
2023-03-10 14:00:51 -05:00
|
|
|
from hub.helpers.geometry_helper import GeometryHelper
|
|
|
|
from hub.imports.geometry.helpers.geometry_helper import GeometryHelper as igh
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.city_model_structure.attributes.polygon import Polygon
|
|
|
|
from hub.city_model_structure.building import Building
|
|
|
|
from hub.city_model_structure.building_demand.surface import Surface
|
|
|
|
from hub.city_model_structure.city import City
|
2022-11-24 17:58:45 -05:00
|
|
|
|
2022-11-17 15:56:49 -05:00
|
|
|
|
|
|
|
class Geojson:
|
|
|
|
"""
|
2022-11-18 16:05:43 -05:00
|
|
|
Geojson class
|
2022-11-17 15:56:49 -05:00
|
|
|
"""
|
2023-05-26 12:01:54 -04:00
|
|
|
_X = 0
|
|
|
|
_Y = 1
|
2022-11-17 15:56:49 -05:00
|
|
|
|
2023-02-02 13:00:58 -05:00
|
|
|
def __init__(self,
|
|
|
|
path,
|
2023-05-26 18:21:35 -04:00
|
|
|
aliases_field=None,
|
2023-02-02 13:00:58 -05:00
|
|
|
extrusion_height_field=None,
|
|
|
|
year_of_construction_field=None,
|
|
|
|
function_field=None,
|
2023-10-24 09:46:40 -04:00
|
|
|
function_to_hub=None,
|
|
|
|
hub_crs=None
|
|
|
|
):
|
|
|
|
self._hub_crs = hub_crs
|
|
|
|
if hub_crs is None :
|
|
|
|
self._hub_crs = 'epsg:26911'
|
|
|
|
self._transformer = Transformer.from_crs('epsg:4326', self._hub_crs)
|
2022-11-23 10:20:33 -05:00
|
|
|
self._min_x = cte.MAX_FLOAT
|
|
|
|
self._min_y = cte.MAX_FLOAT
|
|
|
|
self._max_x = cte.MIN_FLOAT
|
|
|
|
self._max_y = cte.MIN_FLOAT
|
2022-11-24 17:58:45 -05:00
|
|
|
self._max_z = 0
|
2022-11-17 15:56:49 -05:00
|
|
|
self._city = None
|
2023-05-26 18:21:35 -04:00
|
|
|
self._aliases_field = aliases_field
|
2022-11-24 17:58:45 -05:00
|
|
|
self._extrusion_height_field = extrusion_height_field
|
|
|
|
self._year_of_construction_field = year_of_construction_field
|
|
|
|
self._function_field = function_field
|
2023-02-02 13:00:58 -05:00
|
|
|
self._function_to_hub = function_to_hub
|
2023-05-30 17:13:49 -04:00
|
|
|
with open(path, 'r', encoding='utf8') as json_file:
|
2022-11-23 10:20:33 -05:00
|
|
|
self._geojson = json.loads(json_file.read())
|
|
|
|
|
|
|
|
def _save_bounds(self, x, y):
|
|
|
|
if x > self._max_x:
|
|
|
|
self._max_x = x
|
|
|
|
if x < self._min_x:
|
|
|
|
self._min_x = x
|
|
|
|
if y > self._max_y:
|
|
|
|
self._max_y = y
|
|
|
|
if y < self._min_y:
|
|
|
|
self._min_y = y
|
|
|
|
|
2023-03-10 14:00:51 -05:00
|
|
|
@staticmethod
|
|
|
|
def _find_wall(line_1, line_2):
|
|
|
|
for i in range(0, 2):
|
2023-03-20 11:40:20 -04:00
|
|
|
j = 1 - i
|
2023-03-10 14:00:51 -05:00
|
|
|
point_1 = line_1[i]
|
2023-03-20 11:40:20 -04:00
|
|
|
point_2 = line_2[j]
|
2023-03-10 14:00:51 -05:00
|
|
|
distance = GeometryHelper.distance_between_points(point_1, point_2)
|
|
|
|
if distance > 1e-2:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _store_shared_percentage_to_walls(self, city, city_mapped):
|
|
|
|
for building in city.buildings:
|
2023-03-15 14:21:38 -04:00
|
|
|
if building.name not in city_mapped.keys():
|
2023-03-20 11:40:20 -04:00
|
|
|
for wall in building.walls:
|
|
|
|
wall.percentage_shared = 0
|
2023-03-15 14:21:38 -04:00
|
|
|
continue
|
2023-03-10 14:00:51 -05:00
|
|
|
building_mapped = city_mapped[building.name]
|
2023-03-10 15:15:18 -05:00
|
|
|
for wall in building.walls:
|
|
|
|
percentage = 0
|
|
|
|
ground_line = []
|
|
|
|
for point in wall.perimeter_polygon.coordinates:
|
|
|
|
if point[2] < 0.5:
|
|
|
|
ground_line.append(point)
|
|
|
|
for entry in building_mapped:
|
2023-04-25 08:24:56 -04:00
|
|
|
if building_mapped[entry]['shared_points'] <= 2:
|
2023-03-10 14:00:51 -05:00
|
|
|
continue
|
2023-03-10 15:15:18 -05:00
|
|
|
line = [building_mapped[entry]['line_start'], building_mapped[entry]['line_end']]
|
|
|
|
neighbour_line = [building_mapped[entry]['neighbour_line_start'],
|
|
|
|
building_mapped[entry]['neighbour_line_end']]
|
|
|
|
neighbour_height = city.city_object(building_mapped[entry]['neighbour_name']).max_height
|
2023-03-10 14:00:51 -05:00
|
|
|
if self._find_wall(line, ground_line):
|
|
|
|
line_shared = (GeometryHelper.distance_between_points(line[0], line[1]) +
|
|
|
|
GeometryHelper.distance_between_points(neighbour_line[0], neighbour_line[1]) -
|
|
|
|
GeometryHelper.distance_between_points(line[1], neighbour_line[0]) -
|
|
|
|
GeometryHelper.distance_between_points(line[0], neighbour_line[1])) / 2
|
2023-03-10 15:15:18 -05:00
|
|
|
percentage_ground = line_shared / GeometryHelper.distance_between_points(line[0], line[1])
|
2023-03-10 14:00:51 -05:00
|
|
|
percentage_height = neighbour_height / building.max_height
|
2023-05-30 17:13:49 -04:00
|
|
|
percentage_height = min(percentage_height, 1)
|
2023-03-10 15:15:18 -05:00
|
|
|
percentage += percentage_ground * percentage_height
|
|
|
|
wall.percentage_shared = percentage
|
2023-03-10 14:00:51 -05:00
|
|
|
|
2022-11-17 15:56:49 -05:00
|
|
|
@property
|
|
|
|
def city(self) -> City:
|
|
|
|
"""
|
2022-11-23 10:20:33 -05:00
|
|
|
Get city out of a Geojson file
|
2022-11-17 15:56:49 -05:00
|
|
|
"""
|
|
|
|
if self._city is None:
|
2022-11-23 10:20:33 -05:00
|
|
|
buildings = []
|
2023-05-01 18:05:09 -04:00
|
|
|
lod = 0
|
2022-11-23 10:20:33 -05:00
|
|
|
for feature in self._geojson['features']:
|
2022-11-24 17:58:45 -05:00
|
|
|
extrusion_height = 0
|
|
|
|
if self._extrusion_height_field is not None:
|
|
|
|
extrusion_height = float(feature['properties'][self._extrusion_height_field])
|
2023-05-12 09:27:29 -04:00
|
|
|
lod = 1
|
2023-09-29 01:54:46 -04:00
|
|
|
self._max_z = max(self._max_z, extrusion_height)
|
2022-11-24 17:58:45 -05:00
|
|
|
year_of_construction = None
|
|
|
|
if self._year_of_construction_field is not None:
|
|
|
|
year_of_construction = int(feature['properties'][self._year_of_construction_field])
|
|
|
|
function = None
|
|
|
|
if self._function_field is not None:
|
2023-06-09 13:36:21 -04:00
|
|
|
function = str(feature['properties'][self._function_field])
|
2023-02-02 13:00:58 -05:00
|
|
|
if self._function_to_hub is not None:
|
|
|
|
# use the transformation dictionary to retrieve the proper function
|
2023-02-20 07:24:27 -05:00
|
|
|
if function in self._function_to_hub:
|
|
|
|
function = self._function_to_hub[function]
|
2022-11-23 10:20:33 -05:00
|
|
|
geometry = feature['geometry']
|
2023-05-26 18:21:35 -04:00
|
|
|
building_aliases = []
|
2022-11-24 17:58:45 -05:00
|
|
|
if 'id' in feature:
|
|
|
|
building_name = feature['id']
|
2024-08-14 13:08:09 -04:00
|
|
|
elif 'id' in feature['properties']:
|
|
|
|
building_name = feature['properties']['id']
|
2023-06-09 13:12:00 -04:00
|
|
|
else:
|
|
|
|
building_name = uuid.uuid4()
|
2023-05-26 18:21:35 -04:00
|
|
|
if self._aliases_field is not None:
|
|
|
|
|
|
|
|
for alias_field in self._aliases_field:
|
|
|
|
building_aliases.append(feature['properties'][alias_field])
|
2022-11-25 14:39:35 -05:00
|
|
|
|
2023-05-01 18:05:09 -04:00
|
|
|
if str(geometry['type']).lower() == 'polygon':
|
|
|
|
buildings.append(self._parse_polygon(geometry['coordinates'],
|
|
|
|
building_name,
|
2023-05-26 18:21:35 -04:00
|
|
|
building_aliases,
|
2023-05-01 18:05:09 -04:00
|
|
|
function,
|
|
|
|
year_of_construction,
|
|
|
|
extrusion_height))
|
|
|
|
|
|
|
|
elif str(geometry['type']).lower() == 'multipolygon':
|
|
|
|
buildings.append(self._parse_multi_polygon(geometry['coordinates'],
|
|
|
|
building_name,
|
2023-05-26 18:21:35 -04:00
|
|
|
building_aliases,
|
2023-05-01 18:05:09 -04:00
|
|
|
function,
|
|
|
|
year_of_construction,
|
|
|
|
extrusion_height))
|
|
|
|
else:
|
|
|
|
raise NotImplementedError(f'Geojson geometry type [{geometry["type"]}] unknown')
|
2023-10-24 09:46:40 -04:00
|
|
|
self._city = City([self._min_x, self._min_y, 0.0], [self._max_x, self._max_y, self._max_z], self._hub_crs)
|
2022-11-24 17:58:45 -05:00
|
|
|
for building in buildings:
|
2023-03-29 18:22:23 -04:00
|
|
|
# Do not include "small building-like structures" to buildings
|
|
|
|
if building.floor_area >= 25:
|
|
|
|
self._city.add_city_object(building)
|
2023-02-13 05:17:25 -05:00
|
|
|
self._city.level_of_detail.geometry = lod
|
2023-05-12 09:27:29 -04:00
|
|
|
for building in self._city.buildings:
|
|
|
|
building.level_of_detail.geometry = lod
|
2023-05-01 18:05:09 -04:00
|
|
|
if lod > 0:
|
2023-03-17 16:32:54 -04:00
|
|
|
lines_information = GeometryHelper.city_mapping(self._city, plot=False)
|
2023-03-10 15:15:18 -05:00
|
|
|
self._store_shared_percentage_to_walls(self._city, lines_information)
|
2022-11-17 15:56:49 -05:00
|
|
|
return self._city
|
2023-05-01 18:05:09 -04:00
|
|
|
|
|
|
|
def _polygon_coordinates_to_3d(self, polygon_coordinates):
|
|
|
|
transformed_coordinates = ''
|
|
|
|
for coordinate in polygon_coordinates:
|
2023-05-26 12:01:54 -04:00
|
|
|
transformed = self._transformer.transform(coordinate[self._Y], coordinate[self._X])
|
|
|
|
self._save_bounds(transformed[self._X], transformed[self._Y])
|
|
|
|
transformed_coordinates = f'{transformed_coordinates} {transformed[self._X]} {transformed[self._Y]} 0.0'
|
2023-05-01 18:05:09 -04:00
|
|
|
return transformed_coordinates.lstrip(' ')
|
|
|
|
|
2023-05-26 18:21:35 -04:00
|
|
|
def _parse_polygon(self, coordinates, building_name, building_aliases, function, year_of_construction, extrusion_height):
|
2023-05-03 17:02:28 -04:00
|
|
|
surfaces = []
|
2023-05-01 18:05:09 -04:00
|
|
|
for polygon_coordinates in coordinates:
|
2023-05-03 17:02:28 -04:00
|
|
|
points = igh.points_from_string(
|
|
|
|
igh.remove_last_point_from_string(
|
|
|
|
self._polygon_coordinates_to_3d(polygon_coordinates)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
points = igh.invert_points(points)
|
|
|
|
polygon = Polygon(points)
|
|
|
|
polygon.area = igh.ground_area(points)
|
|
|
|
surface = Surface(polygon, polygon)
|
|
|
|
if surface.type == cte.GROUND:
|
|
|
|
surfaces.append(surface)
|
|
|
|
else:
|
|
|
|
distance = cte.MAX_FLOAT
|
|
|
|
hole_connect = 0
|
|
|
|
surface_connect = 0
|
|
|
|
for hole_index, hole_coordinate in enumerate(polygon.coordinates):
|
|
|
|
for surface_index, ground_coordinate in enumerate(surfaces[-1].solid_polygon.coordinates):
|
|
|
|
current_distance = GeometryHelper.distance_between_points(hole_coordinate, ground_coordinate)
|
|
|
|
if current_distance < distance:
|
|
|
|
distance = current_distance
|
|
|
|
hole_connect = hole_index
|
|
|
|
surface_connect = surface_index
|
2023-05-04 11:28:16 -04:00
|
|
|
|
|
|
|
hole = polygon.coordinates[hole_connect:] + polygon.coordinates[:hole_connect] + [polygon.coordinates[hole_connect]]
|
|
|
|
prefix_coordinates = surfaces[-1].solid_polygon.coordinates[:surface_connect+1]
|
2023-05-03 17:02:28 -04:00
|
|
|
trail_coordinates = surfaces[-1].solid_polygon.coordinates[surface_connect:]
|
2023-05-04 11:28:16 -04:00
|
|
|
coordinates = prefix_coordinates + hole + trail_coordinates
|
2023-05-03 17:02:28 -04:00
|
|
|
polygon = Polygon(coordinates)
|
|
|
|
polygon.area = igh.ground_area(coordinates)
|
|
|
|
surfaces[-1] = Surface(polygon, polygon)
|
2023-05-04 13:45:34 -04:00
|
|
|
building = Building(f'{building_name}', surfaces, year_of_construction, function)
|
2023-05-26 18:21:35 -04:00
|
|
|
for alias in building_aliases:
|
|
|
|
building.add_alias(alias)
|
2023-05-03 17:02:28 -04:00
|
|
|
if extrusion_height == 0:
|
|
|
|
return building
|
2023-05-30 17:13:49 -04:00
|
|
|
|
|
|
|
volume = 0
|
|
|
|
for ground in building.grounds:
|
|
|
|
volume += ground.solid_polygon.area * extrusion_height
|
|
|
|
roof_coordinates = []
|
|
|
|
# adding a roof means invert the polygon coordinates and change the Z value
|
|
|
|
for coordinate in ground.solid_polygon.coordinates:
|
|
|
|
roof_coordinate = np.array([coordinate[0], coordinate[1], extrusion_height])
|
|
|
|
# insert the roof rotated already
|
|
|
|
roof_coordinates.insert(0, roof_coordinate)
|
|
|
|
roof_polygon = Polygon(roof_coordinates)
|
|
|
|
roof_polygon.area = ground.solid_polygon.area
|
|
|
|
roof = Surface(roof_polygon, roof_polygon)
|
|
|
|
surfaces.append(roof)
|
|
|
|
# adding a wall means add the point coordinates and the next point coordinates with Z's height and 0
|
|
|
|
coordinates_length = len(roof.solid_polygon.coordinates)
|
|
|
|
for i, coordinate in enumerate(roof.solid_polygon.coordinates):
|
|
|
|
j = i + 1
|
|
|
|
if j == coordinates_length:
|
|
|
|
j = 0
|
|
|
|
next_coordinate = roof.solid_polygon.coordinates[j]
|
|
|
|
wall_coordinates = [
|
|
|
|
np.array([coordinate[0], coordinate[1], 0.0]),
|
|
|
|
np.array([next_coordinate[0], next_coordinate[1], 0.0]),
|
|
|
|
np.array([next_coordinate[0], next_coordinate[1], next_coordinate[2]]),
|
|
|
|
np.array([coordinate[0], coordinate[1], coordinate[2]])
|
|
|
|
]
|
|
|
|
polygon = Polygon(wall_coordinates)
|
|
|
|
wall = Surface(polygon, polygon)
|
|
|
|
surfaces.append(wall)
|
|
|
|
building = Building(f'{building_name}', surfaces, year_of_construction, function)
|
|
|
|
for alias in building_aliases:
|
|
|
|
building.add_alias(alias)
|
|
|
|
building.volume = volume
|
2023-05-03 17:02:28 -04:00
|
|
|
return building
|
|
|
|
|
2023-05-26 18:21:35 -04:00
|
|
|
def _parse_multi_polygon(self, polygons_coordinates, building_name, building_aliases, function, year_of_construction, extrusion_height):
|
2023-05-01 18:05:09 -04:00
|
|
|
surfaces = []
|
2023-05-03 17:02:28 -04:00
|
|
|
for coordinates in polygons_coordinates:
|
|
|
|
for polygon_coordinates in coordinates:
|
|
|
|
points = igh.points_from_string(
|
|
|
|
igh.remove_last_point_from_string(
|
|
|
|
self._polygon_coordinates_to_3d(polygon_coordinates)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
points = igh.invert_points(points)
|
|
|
|
polygon = Polygon(points)
|
|
|
|
polygon.area = igh.ground_area(points)
|
|
|
|
surface = Surface(polygon, polygon)
|
2023-05-01 18:05:09 -04:00
|
|
|
if surface.type == cte.GROUND:
|
|
|
|
surfaces.append(surface)
|
|
|
|
else:
|
2023-05-03 17:02:28 -04:00
|
|
|
distance = cte.MAX_FLOAT
|
|
|
|
hole_connect = 0
|
|
|
|
surface_connect = 0
|
|
|
|
for hole_index, hole_coordinate in enumerate(polygon.coordinates):
|
|
|
|
for surface_index, ground_coordinate in enumerate(surfaces[-1].solid_polygon.coordinates):
|
|
|
|
current_distance = GeometryHelper.distance_between_points(hole_coordinate, ground_coordinate)
|
|
|
|
if current_distance < distance:
|
|
|
|
distance = current_distance
|
|
|
|
hole_connect = hole_index
|
|
|
|
surface_connect = surface_index
|
|
|
|
hole = polygon.coordinates[hole_connect:] + polygon.coordinates[:hole_connect]
|
|
|
|
prefix_coordinates = surfaces[-1].solid_polygon.coordinates[:surface_connect]
|
|
|
|
trail_coordinates = surfaces[-1].solid_polygon.coordinates[surface_connect:]
|
|
|
|
coordinates = prefix_coordinates + hole + [hole[0]] + trail_coordinates
|
|
|
|
polygon = Polygon(coordinates)
|
|
|
|
polygon.area = igh.ground_area(coordinates)
|
2023-05-01 18:05:09 -04:00
|
|
|
surfaces[-1] = Surface(polygon, polygon)
|
2023-05-04 13:45:34 -04:00
|
|
|
building = Building(f'{building_name}', surfaces, year_of_construction, function)
|
2023-05-26 18:21:35 -04:00
|
|
|
for alias in building_aliases:
|
|
|
|
building.add_alias(alias)
|
2023-05-01 18:05:09 -04:00
|
|
|
if extrusion_height == 0:
|
2023-05-03 17:02:28 -04:00
|
|
|
return building
|
2023-05-30 17:13:49 -04:00
|
|
|
|
|
|
|
volume = 0
|
|
|
|
for ground in building.grounds:
|
|
|
|
volume += ground.solid_polygon.area * extrusion_height
|
|
|
|
roof_coordinates = []
|
|
|
|
# adding a roof means invert the polygon coordinates and change the Z value
|
|
|
|
for coordinate in ground.solid_polygon.coordinates:
|
|
|
|
roof_coordinate = np.array([coordinate[0], coordinate[1], extrusion_height])
|
|
|
|
# insert the roof rotated already
|
|
|
|
roof_coordinates.insert(0, roof_coordinate)
|
|
|
|
roof_polygon = Polygon(roof_coordinates)
|
|
|
|
roof_polygon.area = ground.solid_polygon.area
|
|
|
|
roof = Surface(roof_polygon, roof_polygon)
|
|
|
|
surfaces.append(roof)
|
|
|
|
# adding a wall means add the point coordinates and the next point coordinates with Z's height and 0
|
|
|
|
coordinates_length = len(roof.solid_polygon.coordinates)
|
|
|
|
for i, coordinate in enumerate(roof.solid_polygon.coordinates):
|
|
|
|
j = i + 1
|
|
|
|
if j == coordinates_length:
|
|
|
|
j = 0
|
|
|
|
next_coordinate = roof.solid_polygon.coordinates[j]
|
|
|
|
wall_coordinates = [
|
|
|
|
np.array([coordinate[0], coordinate[1], 0.0]),
|
|
|
|
np.array([next_coordinate[0], next_coordinate[1], 0.0]),
|
|
|
|
np.array([next_coordinate[0], next_coordinate[1], next_coordinate[2]]),
|
|
|
|
np.array([coordinate[0], coordinate[1], coordinate[2]])
|
|
|
|
]
|
|
|
|
polygon = Polygon(wall_coordinates)
|
|
|
|
wall = Surface(polygon, polygon)
|
|
|
|
surfaces.append(wall)
|
|
|
|
building = Building(f'{building_name}', surfaces, year_of_construction, function)
|
|
|
|
for alias in building_aliases:
|
|
|
|
building.add_alias(alias)
|
|
|
|
building.volume = volume
|
2023-07-07 14:33:34 -04:00
|
|
|
return building
|