feat: add postal code to simplified building

This commit is contained in:
Majid Rezaei 2024-10-08 15:07:32 -04:00
parent 6ac9110bf0
commit 53cd8586ea
6 changed files with 1265898 additions and 1048617 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -5,12 +5,13 @@ class SimplifiedBuilding:
""" """
SimplifiedBuilding class SimplifiedBuilding class
""" """
def __init__(self, name, centroid=None, total_floor_area=None, year_of_construction=None, function=None, city=None): def __init__(self, name, centroid=None, postal_code=None, total_floor_area=None, year_of_construction=None, function=None, city=None):
self._name = name self._name = name
self._centroid = centroid self._centroid = centroid
self._total_floor_area = total_floor_area self._total_floor_area = total_floor_area
self._year_of_construction = year_of_construction self._year_of_construction = year_of_construction
self._function = function self._function = function
self._postal_code = postal_code
self._city = city self._city = city
self._type = 'building' self._type = 'building'
@ -22,6 +23,10 @@ class SimplifiedBuilding:
def centroid(self): def centroid(self):
return self._centroid return self._centroid
@property
def postal_code(self):
return self._postal_code
@property @property
def total_floor_area(self): def total_floor_area(self):
return self._total_floor_area return self._total_floor_area

View File

@ -1,9 +1,11 @@
import csv import csv
import uuid import uuid
from decimal import Decimal, InvalidOperation from decimal import Decimal, InvalidOperation
from pyproj import Transformer
from hub.city_model_structure.simplified_building import SimplifiedBuilding from hub.city_model_structure.simplified_building import SimplifiedBuilding
from hub.city_model_structure.simplified_city import SimplifiedCity from hub.city_model_structure.simplified_city import SimplifiedCity
class Csv: class Csv:
""" """
CSV class CSV class
@ -11,17 +13,23 @@ class Csv:
def __init__(self, def __init__(self,
path, path,
centroid=None, centroid_x_field=None,
centroid_y_field=None,
postal_code_field=None,
year_of_construction_field=None, year_of_construction_field=None,
function_field=None, function_field=None,
function_to_hub=None, function_to_hub=None,
total_floor_area_field=None): total_floor_area_field=None):
self._simplified_city = None self._simplified_city = None
self._centroid = centroid self._centroid_x_field = centroid_x_field
self._centroid_y_field = centroid_y_field
self._postal_code_field = postal_code_field
self._year_of_construction_field = year_of_construction_field self._year_of_construction_field = year_of_construction_field
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._total_floor_area_field = total_floor_area_field self._total_floor_area_field = total_floor_area_field
self.transformer = Transformer.from_crs("EPSG:3857", "EPSG:4326", always_xy=True)
with open(path, 'r', encoding='utf8') as csv_file: with open(path, 'r', encoding='utf8') as csv_file:
reader = csv.DictReader(csv_file) reader = csv.DictReader(csv_file)
self._csv_data = [row for row in reader] self._csv_data = [row for row in reader]
@ -34,6 +42,14 @@ class Csv:
except ValueError: except ValueError:
return None return None
def safe_str(self, value):
if value in ('NULL', '', None):
return None
try:
return str(value)
except ValueError:
return None
def safe_float(self, value): def safe_float(self, value):
if value in ('NULL', '', None): if value in ('NULL', '', None):
return None return None
@ -54,10 +70,18 @@ class Csv:
def city(self) -> SimplifiedCity: def city(self) -> SimplifiedCity:
if self._simplified_city is None: if self._simplified_city is None:
self._simplified_city = SimplifiedCity() self._simplified_city = SimplifiedCity()
for row in self._csv_data: for index, row in enumerate(self._csv_data):
centroid = None centroid = None
if self._centroid: if self._centroid_x_field and self._centroid_y_field:
centroid = self.safe_int(row[self._centroid]) centroid_x = self.safe_float(row[self._centroid_x_field])
centroid_y = self.safe_float(row.get(self._centroid_y_field))
if centroid_x is not None and centroid_y is not None:
lon, lat = self.transformer.transform(centroid_x, centroid_y)
centroid = (lon, lat)
postal_code = None
if self._postal_code_field:
postal_code = self.safe_float(row[self._postal_code_field])
year_of_construction = None year_of_construction = None
if self._year_of_construction_field: if self._year_of_construction_field:
@ -73,14 +97,13 @@ class Csv:
if self._total_floor_area_field: if self._total_floor_area_field:
total_floor_area = self.safe_float(row[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']: # Use index as the name of the building
building_name = str(self.safe_decimal(row['id_provinc'])) building_name = f"Building_{index + 1}"
else:
building_name = str(uuid.uuid4())
building = SimplifiedBuilding( building = SimplifiedBuilding(
name=building_name, name=building_name,
centroid=centroid, centroid=centroid,
postal_code=postal_code,
total_floor_area=total_floor_area, total_floor_area=total_floor_area,
year_of_construction=year_of_construction, year_of_construction=year_of_construction,
function=function function=function

View File

@ -22,7 +22,9 @@ class GeometryFactory:
path=None, path=None,
aliases_field=None, aliases_field=None,
height_field=None, height_field=None,
centroid=None, centroid_x_field=None,
centroid_y_field=None,
position_x_field=None,
year_of_construction_field=None, year_of_construction_field=None,
function_field=None, function_field=None,
function_to_hub=None, function_to_hub=None,
@ -33,7 +35,9 @@ class GeometryFactory:
self._path = path self._path = path
self._aliases_field = aliases_field self._aliases_field = aliases_field
self._height_field = height_field self._height_field = height_field
self._centroid = centroid self._centroid_x_field = centroid_x_field
self._centroid_y_field = centroid_y_field
self._position_x_field = position_x_field
self._year_of_construction_field = year_of_construction_field self._year_of_construction_field = year_of_construction_field
self._function_field = function_field self._function_field = function_field
self._function_to_hub = function_to_hub self._function_to_hub = function_to_hub
@ -77,7 +81,9 @@ class GeometryFactory:
@property @property
def _csv(self) -> SimplifiedCity: def _csv(self) -> SimplifiedCity:
return Csv(self._path, return Csv(self._path,
self._centroid, self._centroid_x_field,
self._centroid_y_field,
self._position_x_field,
self._year_of_construction_field, self._year_of_construction_field,
self._function_field, self._function_field,
self._function_to_hub, self._function_to_hub,

33
main.py
View File

@ -2,41 +2,18 @@ from hub.imports.geometry_factory import GeometryFactory
from hub.helpers.dictionaries import Dictionaries from hub.helpers.dictionaries import Dictionaries
import os import os
import psycopg2 import psycopg2
import csv
password = os.getenv('PGPASSWORD') input_file = "data/cmm_points_function_vintage_surface.csv"
input_file = "cmm_points_function_vintage_surface.csv"
# Initialize city object from GeometryFactory
city = GeometryFactory( city = GeometryFactory(
file_type="csv", file_type="csv",
path=input_file, path=input_file,
centroid_x_field="centroid_x",
centroid_y_field="centroid_y",
year_of_construction_field="anne_cnstr", year_of_construction_field="anne_cnstr",
function_field="code_util", function_field="code_util",
function_to_hub=Dictionaries().montreal_function_to_hub_function, function_to_hub=Dictionaries().montreal_function_to_hub_function,
total_floor_area_field="supfi_etag" total_floor_area_field="supfi_etag"
).city ).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()