2020-06-09 11:28:50 -04:00
|
|
|
"""
|
|
|
|
City module
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
|
|
|
|
"""
|
2020-12-02 11:56:33 -05:00
|
|
|
from __future__ import annotations
|
2020-06-09 11:28:50 -04:00
|
|
|
import sys
|
2020-12-02 11:56:33 -05:00
|
|
|
import pickle
|
2021-08-26 09:19:38 -04:00
|
|
|
import math
|
2021-06-09 14:23:45 -04:00
|
|
|
from typing import List, Union, TypeVar
|
2020-05-28 15:22:46 -04:00
|
|
|
import pyproj
|
2020-06-16 10:34:17 -04:00
|
|
|
from pyproj import Transformer
|
|
|
|
|
2021-08-26 09:19:38 -04:00
|
|
|
|
2020-06-16 15:12:18 -04:00
|
|
|
from city_model_structure.building import Building
|
2020-06-09 11:28:50 -04:00
|
|
|
from city_model_structure.city_object import CityObject
|
2021-06-03 11:45:17 -04:00
|
|
|
from city_model_structure.city_objects_cluster import CityObjectsCluster
|
|
|
|
from city_model_structure.buildings_cluster import BuildingsCluster
|
|
|
|
from city_model_structure.parts_consisting_building import PartsConsistingBuilding
|
2020-06-22 14:35:40 -04:00
|
|
|
from helpers.geometry_helper import GeometryHelper
|
2021-04-13 14:12:45 -04:00
|
|
|
from helpers.location import Location
|
2021-08-26 09:19:38 -04:00
|
|
|
|
2020-05-18 13:25:08 -04:00
|
|
|
|
2021-06-09 14:23:45 -04:00
|
|
|
Path = TypeVar('Path')
|
|
|
|
|
2020-05-19 17:23:29 -04:00
|
|
|
|
2020-05-18 13:25:08 -04:00
|
|
|
class City:
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
|
|
|
City class
|
|
|
|
"""
|
|
|
|
|
2021-06-03 11:45:17 -04:00
|
|
|
def __init__(self, lower_corner, upper_corner, srs_name):
|
2020-05-18 13:25:08 -04:00
|
|
|
self._name = None
|
|
|
|
self._lower_corner = lower_corner
|
|
|
|
self._upper_corner = upper_corner
|
2021-06-03 11:45:17 -04:00
|
|
|
self._buildings = None
|
2020-05-18 13:25:08 -04:00
|
|
|
self._srs_name = srs_name
|
2020-06-22 14:35:40 -04:00
|
|
|
self._geometry = GeometryHelper()
|
2020-05-28 15:22:46 -04:00
|
|
|
# todo: right now extracted at city level, in the future should be extracted also at building level if exist
|
|
|
|
self._location = None
|
2021-03-15 11:47:30 -04:00
|
|
|
self._country_code = None
|
2021-04-13 14:12:45 -04:00
|
|
|
self._climate_reference_city = None
|
|
|
|
self._climate_file = None
|
2021-04-13 15:09:13 -04:00
|
|
|
self._latitude = None
|
|
|
|
self._longitude = None
|
|
|
|
self._time_zone = None
|
2021-06-03 11:45:17 -04:00
|
|
|
self._buildings_clusters = None
|
|
|
|
self._parts_consisting_buildings = None
|
|
|
|
self._city_objects_clusters = None
|
2021-06-03 15:56:59 -04:00
|
|
|
self._city_objects = None
|
2020-05-28 15:22:46 -04:00
|
|
|
|
2021-04-13 14:12:45 -04:00
|
|
|
def _get_location(self) -> Location:
|
2020-05-28 15:22:46 -04:00
|
|
|
if self._location is None:
|
|
|
|
gps = pyproj.CRS('EPSG:4326') # LatLon with WGS84 datum used by GPS units and Google Earth
|
|
|
|
try:
|
|
|
|
input_reference = pyproj.CRS(self.srs_name) # Projected coordinate system from input data
|
|
|
|
except pyproj.exceptions.CRSError:
|
2021-01-07 17:33:55 -05:00
|
|
|
sys.stderr.write('Invalid projection reference system, please check the input data. '
|
|
|
|
'(e.g. in CityGML files: srs_name)\n')
|
2020-06-09 11:28:50 -04:00
|
|
|
sys.exit()
|
2020-06-03 16:56:17 -04:00
|
|
|
transformer = Transformer.from_crs(input_reference, gps)
|
|
|
|
coordinates = transformer.transform(self.lower_corner[0], self.lower_corner[1])
|
2020-06-26 14:34:37 -04:00
|
|
|
self._location = GeometryHelper.get_location(coordinates[0], coordinates[1])
|
2020-05-28 15:22:46 -04:00
|
|
|
return self._location
|
|
|
|
|
|
|
|
@property
|
|
|
|
def country_code(self):
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
|
|
|
City country code
|
|
|
|
:return: str
|
|
|
|
"""
|
2021-04-13 15:11:39 -04:00
|
|
|
return self._get_location().country
|
2020-05-28 15:22:46 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
|
|
|
City name
|
|
|
|
:return: str
|
|
|
|
"""
|
2021-04-13 14:12:45 -04:00
|
|
|
return self._get_location().city
|
|
|
|
|
|
|
|
@property
|
|
|
|
def climate_reference_city(self):
|
2021-06-09 14:23:45 -04:00
|
|
|
"""
|
|
|
|
Name of the city of reference for climatic information
|
|
|
|
:return: str
|
|
|
|
"""
|
2021-04-13 14:12:45 -04:00
|
|
|
return self._climate_reference_city
|
|
|
|
|
|
|
|
@climate_reference_city.setter
|
|
|
|
def climate_reference_city(self, value):
|
2021-06-09 14:23:45 -04:00
|
|
|
"""
|
|
|
|
Name of the city of reference for climatic information
|
|
|
|
:param value: str
|
|
|
|
"""
|
2021-04-13 14:12:45 -04:00
|
|
|
self._climate_reference_city = value
|
|
|
|
|
|
|
|
@property
|
2021-06-09 14:23:45 -04:00
|
|
|
def climate_file(self) -> Path:
|
|
|
|
"""
|
|
|
|
Full path of climate file
|
|
|
|
:return: Path
|
|
|
|
"""
|
2021-04-13 14:12:45 -04:00
|
|
|
return self._climate_file
|
|
|
|
|
|
|
|
@climate_file.setter
|
|
|
|
def climate_file(self, value):
|
2021-06-09 14:23:45 -04:00
|
|
|
"""
|
|
|
|
Full path of climate file
|
|
|
|
:param value: Path
|
|
|
|
"""
|
2021-04-13 14:12:45 -04:00
|
|
|
self._climate_file = value
|
2020-05-18 13:25:08 -04:00
|
|
|
|
|
|
|
@property
|
2020-05-28 12:07:36 -04:00
|
|
|
def city_objects(self) -> Union[List[CityObject], None]:
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
2020-06-16 15:12:18 -04:00
|
|
|
City objects belonging to the city
|
|
|
|
:return: None or [CityObject]
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
2021-06-03 15:56:59 -04:00
|
|
|
if self._city_objects is None:
|
|
|
|
if self.city_objects_clusters is None:
|
|
|
|
self._city_objects = []
|
|
|
|
else:
|
|
|
|
self._city_objects = self.city_objects_clusters
|
|
|
|
if self.buildings is not None:
|
|
|
|
for building in self.buildings:
|
|
|
|
self._city_objects.append(building)
|
|
|
|
return self._city_objects
|
2020-06-16 15:12:18 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def buildings(self) -> Union[List[Building], None]:
|
|
|
|
"""
|
|
|
|
Buildings belonging to the city
|
|
|
|
:return: None or [Building]
|
|
|
|
"""
|
|
|
|
return self._buildings
|
|
|
|
|
|
|
|
@property
|
|
|
|
def trees(self) -> NotImplementedError:
|
|
|
|
"""
|
|
|
|
Trees belonging to the city
|
|
|
|
:return: NotImplementedError
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@property
|
|
|
|
def bixi_features(self) -> NotImplementedError:
|
|
|
|
"""
|
|
|
|
Bixi features belonging to the city
|
|
|
|
:return: NotImplementedError
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@property
|
|
|
|
def composting_plants(self) -> NotImplementedError:
|
|
|
|
"""
|
|
|
|
Composting plants belonging to the city
|
|
|
|
:return: NotImplementedError
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
2020-05-18 13:25:08 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def lower_corner(self):
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
|
|
|
City lower corner
|
|
|
|
:return: [x,y,z]
|
|
|
|
"""
|
2020-05-18 13:25:08 -04:00
|
|
|
return self._lower_corner
|
|
|
|
|
|
|
|
@property
|
|
|
|
def upper_corner(self):
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
|
|
|
City upper corner
|
|
|
|
:return: [x,y,z]
|
|
|
|
"""
|
2020-05-18 13:25:08 -04:00
|
|
|
return self._upper_corner
|
|
|
|
|
|
|
|
def city_object(self, name) -> Union[CityObject, None]:
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
|
|
|
Retrieve the city CityObject with the given name
|
|
|
|
:param name:str
|
|
|
|
:return: None or CityObject
|
|
|
|
"""
|
2020-06-16 15:12:18 -04:00
|
|
|
for city_object in self.buildings:
|
2020-06-09 11:28:50 -04:00
|
|
|
if city_object.name == name:
|
|
|
|
return city_object
|
2020-05-18 13:25:08 -04:00
|
|
|
return None
|
|
|
|
|
2020-05-28 12:07:36 -04:00
|
|
|
def add_city_object(self, new_city_object):
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
|
|
|
Add a CityObject to the city
|
|
|
|
:param new_city_object:CityObject
|
2021-06-09 14:23:45 -04:00
|
|
|
:return: None or not implemented error
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
2021-06-02 11:56:38 -04:00
|
|
|
if new_city_object.type == 'building':
|
|
|
|
if self._buildings is None:
|
|
|
|
self._buildings = []
|
|
|
|
self._buildings.append(new_city_object)
|
|
|
|
else:
|
2021-06-03 11:52:00 -04:00
|
|
|
raise NotImplementedError(new_city_object.type)
|
2020-05-28 12:07:36 -04:00
|
|
|
|
2021-05-25 13:34:57 -04:00
|
|
|
def remove_city_object(self, city_object):
|
|
|
|
"""
|
|
|
|
Remove a CityObject from the city
|
|
|
|
:param city_object:CityObject
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
if city_object.type != 'building':
|
|
|
|
raise NotImplementedError(city_object.type)
|
|
|
|
if self._buildings is None or self._buildings == []:
|
|
|
|
sys.stderr.write('Warning: impossible to remove city_object, the city is empty\n')
|
|
|
|
else:
|
|
|
|
if city_object in self._buildings:
|
|
|
|
self._buildings.remove(city_object)
|
|
|
|
|
2020-05-18 13:25:08 -04:00
|
|
|
@property
|
|
|
|
def srs_name(self):
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
|
|
|
srs name
|
|
|
|
:return: str
|
|
|
|
"""
|
2020-05-18 13:25:08 -04:00
|
|
|
return self._srs_name
|
|
|
|
|
|
|
|
@name.setter
|
|
|
|
def name(self, value):
|
2020-06-09 11:28:50 -04:00
|
|
|
"""
|
|
|
|
Set the city name
|
2020-06-10 11:08:38 -04:00
|
|
|
:param value:str
|
2020-06-09 11:28:50 -04:00
|
|
|
:return: None
|
|
|
|
"""
|
2020-05-18 13:25:08 -04:00
|
|
|
self._name = value
|
2020-12-02 11:56:33 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load(city_filename) -> City:
|
|
|
|
"""
|
|
|
|
Load a city saved with city.save(city_filename)
|
|
|
|
:param city_filename: city filename
|
|
|
|
:return: City
|
|
|
|
"""
|
2021-08-26 09:19:38 -04:00
|
|
|
with open(city_filename, 'rb') as file:
|
|
|
|
return pickle.load(file)
|
2020-12-02 11:56:33 -05:00
|
|
|
|
|
|
|
def save(self, city_filename):
|
|
|
|
"""
|
|
|
|
Save a city into the given filename
|
|
|
|
:param city_filename: destination city filename
|
|
|
|
:return:
|
|
|
|
"""
|
2021-08-26 09:19:38 -04:00
|
|
|
with open(city_filename, 'wb') as file:
|
|
|
|
pickle.dump(self, file)
|
2021-01-27 16:08:11 -05:00
|
|
|
|
2021-06-09 14:23:45 -04:00
|
|
|
def region(self, center, radius) -> City:
|
2021-01-27 16:08:11 -05:00
|
|
|
"""
|
|
|
|
Save a city into the given filename
|
|
|
|
:param center: specific point in space [x, y, z]
|
|
|
|
:param radius: distance to center of the sphere selected in meters
|
|
|
|
:return: selected_region_city
|
|
|
|
"""
|
|
|
|
selected_region_lower_corner = [center[0] - radius, center[1] - radius, center[2] - radius]
|
|
|
|
selected_region_upper_corner = [center[0] + radius, center[1] + radius, center[2] + radius]
|
|
|
|
selected_region_city = City(selected_region_lower_corner, selected_region_upper_corner, srs_name=self.srs_name)
|
2021-05-25 13:34:57 -04:00
|
|
|
selected_region_city.climate_file = self.climate_file
|
|
|
|
# selected_region_city.climate_reference_city = self.climate_reference_city
|
2021-01-27 16:08:11 -05:00
|
|
|
for city_object in self.city_objects:
|
2021-03-25 09:11:42 -04:00
|
|
|
location = city_object.centroid
|
2021-05-25 13:34:57 -04:00
|
|
|
if location is not None:
|
|
|
|
distance = math.sqrt(math.pow(location[0]-center[0], 2) + math.pow(location[1]-center[1], 2)
|
|
|
|
+ math.pow(location[2]-center[2], 2))
|
|
|
|
if distance < radius:
|
|
|
|
selected_region_city.add_city_object(city_object)
|
2021-01-27 16:08:11 -05:00
|
|
|
return selected_region_city
|
2021-04-13 15:09:13 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def latitude(self):
|
|
|
|
"""
|
|
|
|
city latitude in degrees
|
2021-06-09 14:23:45 -04:00
|
|
|
:return: float
|
2021-04-13 15:09:13 -04:00
|
|
|
"""
|
|
|
|
return self._latitude
|
2021-04-13 19:00:28 -04:00
|
|
|
|
|
|
|
@latitude.setter
|
|
|
|
def latitude(self, value):
|
|
|
|
"""
|
|
|
|
city latitude in degrees
|
2021-06-09 14:23:45 -04:00
|
|
|
:parameter value: float
|
2021-04-13 19:00:28 -04:00
|
|
|
"""
|
|
|
|
self._latitude = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def longitude(self):
|
|
|
|
"""
|
|
|
|
city longitude in degrees
|
2021-06-09 14:23:45 -04:00
|
|
|
:return: float
|
2021-04-13 19:00:28 -04:00
|
|
|
"""
|
|
|
|
return self._longitude
|
|
|
|
|
|
|
|
@longitude.setter
|
|
|
|
def longitude(self, value):
|
|
|
|
"""
|
|
|
|
city longitude in degrees
|
2021-06-09 14:23:45 -04:00
|
|
|
:parameter value: float
|
2021-04-13 19:00:28 -04:00
|
|
|
"""
|
|
|
|
self._longitude = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def time_zone(self):
|
|
|
|
"""
|
|
|
|
city time_zone
|
2021-06-09 14:23:45 -04:00
|
|
|
:return: float
|
2021-04-13 19:00:28 -04:00
|
|
|
"""
|
|
|
|
return self._time_zone
|
|
|
|
|
|
|
|
@time_zone.setter
|
|
|
|
def time_zone(self, value):
|
|
|
|
"""
|
|
|
|
city time_zone
|
2021-06-09 14:23:45 -04:00
|
|
|
:parameter value: float
|
2021-04-13 19:00:28 -04:00
|
|
|
"""
|
|
|
|
self._time_zone = value
|
2021-06-03 11:45:17 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def buildings_clusters(self) -> Union[List[BuildingsCluster], None]:
|
|
|
|
"""
|
|
|
|
buildings clusters belonging to the city
|
|
|
|
:return: None or [BuildingsCluster]
|
|
|
|
"""
|
|
|
|
return self._buildings_clusters
|
|
|
|
|
|
|
|
@property
|
|
|
|
def parts_consisting_buildings(self) -> Union[List[PartsConsistingBuilding], None]:
|
|
|
|
"""
|
|
|
|
Parts consisting buildings belonging to the city
|
|
|
|
:return: None or [PartsConsistingBuilding]
|
|
|
|
"""
|
|
|
|
return self._parts_consisting_buildings
|
|
|
|
|
|
|
|
@property
|
|
|
|
def city_objects_clusters(self) -> Union[List[CityObjectsCluster], None]:
|
|
|
|
"""
|
|
|
|
City objects clusters belonging to the city
|
|
|
|
:return: None or [CityObjectsCluster]
|
|
|
|
"""
|
2021-06-03 15:56:59 -04:00
|
|
|
if self.buildings_clusters is None:
|
|
|
|
self._city_objects_clusters = []
|
|
|
|
else:
|
|
|
|
self._city_objects_clusters = self.buildings_clusters
|
|
|
|
if self.parts_consisting_buildings is not None:
|
|
|
|
self._city_objects_clusters.append(self.parts_consisting_buildings)
|
2021-06-03 11:45:17 -04:00
|
|
|
return self._city_objects_clusters
|
|
|
|
|
|
|
|
def add_city_objects_cluster(self, new_city_objects_cluster):
|
|
|
|
"""
|
|
|
|
Add a CityObject to the city
|
|
|
|
:param new_city_objects_cluster:CityObjectsCluster
|
2021-06-09 14:23:45 -04:00
|
|
|
:return: None or NotImplementedError
|
2021-06-03 11:45:17 -04:00
|
|
|
"""
|
|
|
|
if new_city_objects_cluster.type == 'buildings':
|
|
|
|
if self._buildings_clusters is None:
|
|
|
|
self._buildings_clusters = []
|
|
|
|
self._buildings_clusters.append(new_city_objects_cluster)
|
|
|
|
elif new_city_objects_cluster.type == 'building_parts':
|
|
|
|
if self._parts_consisting_buildings is None:
|
|
|
|
self._parts_consisting_buildings = []
|
|
|
|
self._parts_consisting_buildings.append(new_city_objects_cluster)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|