Compare commits

...

5 Commits

8 changed files with 1048800 additions and 3 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,42 @@
from typing import Union
class SimplifiedBuilding:
"""
SimplifiedBuilding class
"""
def __init__(self, name, total_floor_area=None, year_of_construction=None, function=None, city=None):
self._name = name
self._total_floor_area = total_floor_area
self._year_of_construction = year_of_construction
self._function = function
self._city = city
self._type = 'building'
@property
def name(self):
return self._name
@property
def total_floor_area(self):
return self._total_floor_area
@property
def year_of_construction(self):
return self._year_of_construction
@property
def function(self) -> Union[None, str]:
return self._function
@property
def type(self):
return self._type
@property
def city(self):
return self._city
@city.setter
def city(self, value):
self._city = value

View File

@ -0,0 +1,31 @@
from typing import List
from hub.city_model_structure.simplified_building import SimplifiedBuilding
class SimplifiedCity:
"""
SimplifiedCity class
"""
def __init__(self):
self._name = "Montreal Metropolitan Area"
self._buildings = []
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if value is not None:
self._name = str(value)
@property
def buildings(self) -> List[SimplifiedBuilding]:
return self._buildings
def add_city_object(self, new_city_object):
if new_city_object.type == 'building':
self._buildings.append(new_city_object)
new_city_object.city = self
else:
raise NotImplementedError(f"Type {new_city_object.type} is not supported")

View File

@ -0,0 +1,85 @@
import csv
import uuid
from decimal import Decimal, InvalidOperation
from hub.city_model_structure.simplified_building import SimplifiedBuilding
from hub.city_model_structure.simplified_city import SimplifiedCity
class Csv:
"""
CSV class
"""
def __init__(self,
path,
year_of_construction_field=None,
function_field=None,
function_to_hub=None,
total_floor_area_field=None):
self._simplified_city = None
self._year_of_construction_field = year_of_construction_field
self._function_field = function_field
self._function_to_hub = function_to_hub
self._total_floor_area_field = total_floor_area_field
with open(path, 'r', encoding='utf8') as csv_file:
reader = csv.DictReader(csv_file)
self._csv_data = [row for row in reader]
def safe_int(self, value):
if value in ('NULL', '', None):
return None
try:
return int(value)
except ValueError:
return None
def safe_float(self, value):
if value in ('NULL', '', None):
return None
try:
return float(value)
except ValueError:
return None
def safe_decimal(self, value):
if value in ('NULL', '', None):
return None
try:
return Decimal(value)
except InvalidOperation:
return None
@property
def city(self) -> SimplifiedCity:
if self._simplified_city is None:
self._simplified_city = SimplifiedCity()
for row in self._csv_data:
year_of_construction = None
if self._year_of_construction_field:
year_of_construction = self.safe_int(row[self._year_of_construction_field])
function = None
if self._function_field:
function = row[self._function_field]
if self._function_to_hub:
function = self._function_to_hub.get(function, function)
total_floor_area = None
if self._total_floor_area_field:
total_floor_area = self.safe_float(row[self._total_floor_area_field])
if 'id_provinc' in row and row['id_provinc']:
building_name = str(self.safe_decimal(row['id_provinc']))
else:
building_name = str(uuid.uuid4())
building = SimplifiedBuilding(
name=building_name,
total_floor_area=total_floor_area,
year_of_construction=year_of_construction,
function=function
)
if building.total_floor_area is not None and building.total_floor_area >= 25:
self._simplified_city.add_city_object(building)
return self._simplified_city

View File

@ -6,10 +6,12 @@ Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
""" """
from hub.city_model_structure.city import City from hub.city_model_structure.city import City
from hub.city_model_structure.simplified_city import SimplifiedCity
from hub.helpers.utils import validate_import_export_type from hub.helpers.utils import validate_import_export_type
from hub.imports.geometry.citygml import CityGml from hub.imports.geometry.citygml import CityGml
from hub.imports.geometry.geojson import Geojson from hub.imports.geometry.geojson import Geojson
from hub.imports.geometry.obj import Obj from hub.imports.geometry.obj import Obj
from hub.imports.geometry.csv import Csv
class GeometryFactory: class GeometryFactory:
@ -23,7 +25,8 @@ class GeometryFactory:
year_of_construction_field=None, year_of_construction_field=None,
function_field=None, function_field=None,
function_to_hub=None, function_to_hub=None,
hub_crs=None): hub_crs=None,
total_floor_area_field=None):
self._file_type = '_' + file_type.lower() self._file_type = '_' + file_type.lower()
validate_import_export_type(GeometryFactory, file_type) validate_import_export_type(GeometryFactory, file_type)
self._path = path self._path = path
@ -33,6 +36,7 @@ class GeometryFactory:
self._function_field = function_field self._function_field = function_field
self._function_to_hub = function_to_hub self._function_to_hub = function_to_hub
self._hub_crs = hub_crs self._hub_crs = hub_crs
self._total_floor_area_field = total_floor_area_field
@property @property
def _citygml(self) -> City: def _citygml(self) -> City:
@ -68,6 +72,14 @@ class GeometryFactory:
self._function_to_hub, self._function_to_hub,
self._hub_crs).city self._hub_crs).city
@property
def _csv(self) -> SimplifiedCity:
return Csv(self._path,
self._year_of_construction_field,
self._function_field,
self._function_to_hub,
self._total_floor_area_field).city
@property @property
def city(self) -> City: def city(self) -> City:
""" """
@ -75,3 +87,11 @@ class GeometryFactory:
:return: City :return: City
""" """
return getattr(self, self._file_type, lambda: None) return getattr(self, self._file_type, lambda: None)
@property
def simplified_city(self) -> SimplifiedCity:
"""
Enrich the city given to the class using the class given handler
:return: SimplifiedCity
"""
return getattr(self, self._file_type, lambda: None)

42
main.py Normal file
View File

@ -0,0 +1,42 @@
from hub.imports.geometry_factory import GeometryFactory
from hub.helpers.dictionaries import Dictionaries
import os
import psycopg2
password = os.getenv('PGPASSWORD')
input_file = "cmm_points_function_vintage_surface.csv"
city = GeometryFactory(
file_type="csv",
path=input_file,
year_of_construction_field="anne_cnstr",
function_field="code_util",
function_to_hub=Dictionaries().montreal_function_to_hub_function,
total_floor_area_field="supfi_etag"
).city
conn_params = {
'host': 'localhost',
'port': '5432',
'database': 'energydemanddb',
'user': 'postgres',
'password': password
}
try:
conn = psycopg2.connect(**conn_params)
print("Connection established successfully.")
except psycopg2.Error as e:
print(f"An error occurred while connecting to the database: {e}")
cursor = conn.cursor()
cursor.execute("SELECT * FROM energy_data LIMIT 5;")
results = cursor.fetchall()
for row in results:
print(row)
cursor.close()
conn.close()

View File

@ -25,3 +25,4 @@ psycopg2-binary
Pillow Pillow
pathlib pathlib
sqlalchemy_utils sqlalchemy_utils
build

View File

@ -10,7 +10,7 @@ with pathlib.Path('requirements.txt').open() as r:
for requirement for requirement
in r.readlines() in r.readlines()
] ]
install_requires.append('setuptools, build') install_requires.append('setuptools')
main_ns = {} main_ns = {}
version = convert_path('hub/version.py') version = convert_path('hub/version.py')