refactoring and code cleaning

This commit is contained in:
Guille Gutierrez 2020-06-09 11:28:50 -04:00
parent d7b24eba41
commit a9b0579e01
4 changed files with 60 additions and 22 deletions

View File

@ -1,11 +1,22 @@
from city_model_structure.city_object import CityObject """
City module
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
"""
import sys
from typing import List, Union from typing import List, Union
import pyproj import pyproj
from pyproj import Transformer from pyproj import Transformer
import reverse_geocoder as rg import reverse_geocoder as rg
from city_model_structure.city_object import CityObject
class City: class City:
"""
City class
"""
def __init__(self, lower_corner, upper_corner, srs_name, city_objects=None): def __init__(self, lower_corner, upper_corner, srs_name, city_objects=None):
self._city_objects = None self._city_objects = None
self._name = None self._name = None
@ -16,16 +27,14 @@ class City:
# todo: right now extracted at city level, in the future should be extracted also at building level if exist # todo: right now extracted at city level, in the future should be extracted also at building level if exist
self._location = None self._location = None
@property def _get_location(self):
def location(self):
if self._location is None: if self._location is None:
gps = pyproj.CRS('EPSG:4326') # LatLon with WGS84 datum used by GPS units and Google Earth gps = pyproj.CRS('EPSG:4326') # LatLon with WGS84 datum used by GPS units and Google Earth
input_reference = None
try: try:
input_reference = pyproj.CRS(self.srs_name) # Projected coordinate system from input data input_reference = pyproj.CRS(self.srs_name) # Projected coordinate system from input data
except pyproj.exceptions.CRSError: except pyproj.exceptions.CRSError:
print('Invalid projection reference system, please check the input data. (e.g. in CityGML files: srs_name)') print('Invalid projection reference system, please check the input data. (e.g. in CityGML files: srs_name)')
quit() sys.exit()
transformer = Transformer.from_crs(input_reference, gps) transformer = Transformer.from_crs(input_reference, gps)
coordinates = transformer.transform(self.lower_corner[0], self.lower_corner[1]) coordinates = transformer.transform(self.lower_corner[0], self.lower_corner[1])
self._location = rg.search(coordinates) self._location = rg.search(coordinates)
@ -33,33 +42,63 @@ class City:
@property @property
def country_code(self): def country_code(self):
return self.location[0]['cc'] """
City country code
:return: str
"""
return self._get_location()[0]['cc']
@property @property
def name(self): def name(self):
"""
City name
:return: str
"""
if self._name is None: if self._name is None:
self._name = self.location[0]['name'] self._name = self._get_location()[0]['name']
return self._name return self._name
@property @property
def city_objects(self) -> Union[List[CityObject], None]: def city_objects(self) -> Union[List[CityObject], None]:
"""
CityObjects belonging to the city
:return: None or a list of CityObjects
"""
return self._city_objects return self._city_objects
@property @property
def lower_corner(self): def lower_corner(self):
"""
City lower corner
:return: [x,y,z]
"""
return self._lower_corner return self._lower_corner
@property @property
def upper_corner(self): def upper_corner(self):
"""
City upper corner
:return: [x,y,z]
"""
return self._upper_corner return self._upper_corner
def city_object(self, name) -> Union[CityObject, None]: def city_object(self, name) -> Union[CityObject, None]:
for c in self.city_objects: """
if c.name == name: Retrieve the city CityObject with the given name
return c :param name:str
:return: None or CityObject
"""
for city_object in self.city_objects:
if city_object.name == name:
return city_object
return None return None
def add_city_object(self, new_city_object): def add_city_object(self, new_city_object):
"""
Add a CityObject to the city
:param new_city_object:CityObject
:return: None
"""
if self._city_objects is None: if self._city_objects is None:
self._city_objects = [] self._city_objects = []
for city_object in self.city_objects: for city_object in self.city_objects:
@ -70,8 +109,17 @@ class City:
@property @property
def srs_name(self): def srs_name(self):
"""
srs name
:return: str
"""
return self._srs_name return self._srs_name
@name.setter @name.setter
def name(self, value): def name(self, value):
"""
Set the city name
:param value:
:return: None
"""
self._name = value self._name = value

View File

@ -1,5 +1,5 @@
from city_model_structure.thermal_opening import ThermalOpening from city_model_structure.thermal_opening import ThermalOpening
from city_model_structure.surface import Surface from city_model_structure.thermal_zone import ThermalZone
from city_model_structure.layer import Layer from city_model_structure.layer import Layer
from helpers.configuration import Configuration from helpers.configuration import Configuration
from typing import List from typing import List
@ -21,7 +21,7 @@ class ThermalBoundary:
self._shortwave_reflectance = None self._shortwave_reflectance = None
@property @property
def delimits(self) -> List[Surface]: def delimits(self) -> List[ThermalZone]:
return self._delimits return self._delimits
@property @property

View File

@ -3,7 +3,6 @@ from helpers.configuration import Configuration
class ThermalOpening: class ThermalOpening:
def __init__(self): def __init__(self):
self._area = None
self._openable_ratio = None self._openable_ratio = None
self._conductivity_w_mk = None self._conductivity_w_mk = None
self._frame_ratio = Configuration().frame_ratio self._frame_ratio = Configuration().frame_ratio
@ -13,14 +12,6 @@ class ThermalOpening:
self._outside_reflectance = None self._outside_reflectance = None
self._u_value = None self._u_value = None
@property
def area(self):
return self._area
@area.setter
def area(self, value):
self._area = value
@property @property
def openable_ratio(self): def openable_ratio(self):
raise Exception('Not implemented') raise Exception('Not implemented')

View File

@ -27,7 +27,6 @@ class TestGeometryFactory(TestCase):
self.assertIsNotNone(city.upper_corner, 'upper_corner is none') self.assertIsNotNone(city.upper_corner, 'upper_corner is none')
self.assertIsNotNone(city.name, 'name is none') self.assertIsNotNone(city.name, 'name is none')
self.assertIsNotNone(city.country_code, 'country code is none') self.assertIsNotNone(city.country_code, 'country code is none')
self.assertIsNotNone(city.location, 'location is none')
def test_citygml_city_objects(self): def test_citygml_city_objects(self):
city = self.get_citygml() city = self.get_citygml()