feat: add postal code to simplified building
This commit is contained in:
parent
04214be864
commit
79edd8f6a2
File diff suppressed because it is too large
Load Diff
1265846
data/cmm_points_function_vintage_surface.csv
Normal file
1265846
data/cmm_points_function_vintage_surface.csv
Normal file
File diff suppressed because it is too large
Load Diff
|
@ -5,12 +5,13 @@ class SimplifiedBuilding:
|
|||
"""
|
||||
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._centroid = centroid
|
||||
self._total_floor_area = total_floor_area
|
||||
self._year_of_construction = year_of_construction
|
||||
self._function = function
|
||||
self._postal_code = postal_code
|
||||
self._city = city
|
||||
self._type = 'building'
|
||||
|
||||
|
@ -22,6 +23,10 @@ class SimplifiedBuilding:
|
|||
def centroid(self):
|
||||
return self._centroid
|
||||
|
||||
@property
|
||||
def postal_code(self):
|
||||
return self._postal_code
|
||||
|
||||
@property
|
||||
def total_floor_area(self):
|
||||
return self._total_floor_area
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
import csv
|
||||
import uuid
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from pyproj import Transformer
|
||||
from hub.city_model_structure.simplified_building import SimplifiedBuilding
|
||||
from hub.city_model_structure.simplified_city import SimplifiedCity
|
||||
|
||||
|
||||
class Csv:
|
||||
"""
|
||||
CSV class
|
||||
|
@ -11,17 +13,23 @@ class Csv:
|
|||
|
||||
def __init__(self,
|
||||
path,
|
||||
centroid=None,
|
||||
centroid_x_field=None,
|
||||
centroid_y_field=None,
|
||||
postal_code_field=None,
|
||||
year_of_construction_field=None,
|
||||
function_field=None,
|
||||
function_to_hub=None,
|
||||
total_floor_area_field=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._function_field = function_field
|
||||
self._function_to_hub = function_to_hub
|
||||
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:
|
||||
reader = csv.DictReader(csv_file)
|
||||
self._csv_data = [row for row in reader]
|
||||
|
@ -34,6 +42,14 @@ class Csv:
|
|||
except ValueError:
|
||||
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):
|
||||
if value in ('NULL', '', None):
|
||||
return None
|
||||
|
@ -54,10 +70,18 @@ class Csv:
|
|||
def city(self) -> SimplifiedCity:
|
||||
if self._simplified_city is None:
|
||||
self._simplified_city = SimplifiedCity()
|
||||
for row in self._csv_data:
|
||||
for index, row in enumerate(self._csv_data):
|
||||
centroid = None
|
||||
if self._centroid:
|
||||
centroid = self.safe_int(row[self._centroid])
|
||||
if self._centroid_x_field and self._centroid_y_field:
|
||||
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
|
||||
if self._year_of_construction_field:
|
||||
|
@ -73,14 +97,13 @@ class Csv:
|
|||
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())
|
||||
# Use index as the name of the building
|
||||
building_name = f"Building_{index + 1}"
|
||||
|
||||
building = SimplifiedBuilding(
|
||||
name=building_name,
|
||||
centroid=centroid,
|
||||
postal_code=postal_code,
|
||||
total_floor_area=total_floor_area,
|
||||
year_of_construction=year_of_construction,
|
||||
function=function
|
||||
|
|
|
@ -22,7 +22,9 @@ class GeometryFactory:
|
|||
path=None,
|
||||
aliases_field=None,
|
||||
height_field=None,
|
||||
centroid=None,
|
||||
centroid_x_field=None,
|
||||
centroid_y_field=None,
|
||||
position_x_field=None,
|
||||
year_of_construction_field=None,
|
||||
function_field=None,
|
||||
function_to_hub=None,
|
||||
|
@ -33,7 +35,9 @@ class GeometryFactory:
|
|||
self._path = path
|
||||
self._aliases_field = aliases_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._function_field = function_field
|
||||
self._function_to_hub = function_to_hub
|
||||
|
@ -77,7 +81,9 @@ class GeometryFactory:
|
|||
@property
|
||||
def _csv(self) -> SimplifiedCity:
|
||||
return Csv(self._path,
|
||||
self._centroid,
|
||||
self._centroid_x_field,
|
||||
self._centroid_y_field,
|
||||
self._position_x_field,
|
||||
self._year_of_construction_field,
|
||||
self._function_field,
|
||||
self._function_to_hub,
|
||||
|
|
33
main.py
33
main.py
|
@ -2,41 +2,18 @@ from hub.imports.geometry_factory import GeometryFactory
|
|||
from hub.helpers.dictionaries import Dictionaries
|
||||
import os
|
||||
import psycopg2
|
||||
import csv
|
||||
|
||||
password = os.getenv('PGPASSWORD')
|
||||
|
||||
input_file = "cmm_points_function_vintage_surface.csv"
|
||||
input_file = "data/cmm_points_function_vintage_surface.csv"
|
||||
|
||||
# Initialize city object from GeometryFactory
|
||||
city = GeometryFactory(
|
||||
file_type="csv",
|
||||
path=input_file,
|
||||
centroid_x_field="centroid_x",
|
||||
centroid_y_field="centroid_y",
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user