summer_course_2024/processor.py
Andrea Gabaldon Moreno 76a6f5f930 2024-07-17_course_update
Lachine data model is assessed for several building types and U-values
2024-07-17 18:39:52 -04:00

146 lines
7.1 KiB
Python

import json
from pathlib import Path
import pandas as pd
import geopandas as gpd
# Specify the GeoJSON file path
input_files_path = (Path(__file__).parent / 'input_files')
geojson_file_path = input_files_path / 'Lachine_moved_existing_year_type.geojson'
#read it to use later
gdf = gpd.read_file(geojson_file_path)
#define output path
output_path = (Path(__file__).parent / 'out_files').resolve()
output_path.mkdir(parents=True, exist_ok=True)
# Define output folders
output_folders = ['building_type', 'building_type_2', 'building_type_3']
#output paths containing energy+ results are read
gdf_building_outputs={}
output_paths = {}
# Create directories for the output folders
for folder in output_folders:
path = output_path / folder
path.mkdir(parents=True, exist_ok=True)
output_paths[folder] = path
# Read the resulting GeoDataFrames saved in each folder
gdf_building_outputs = {}
for folder in output_folders:
path = output_paths[folder] / 'updated_buildings.geojson'
if path.exists():
gdf_building_outputs[folder] = gpd.read_file(path)
else:
gdf_building_outputs[folder] = gpd.GeoDataFrame()
percentage_data = {
1646: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1647: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1648: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1649: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1650: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1651: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1652: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1653: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1654: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1655: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1656: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1657: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1658: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1659: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1660: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1661: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1662: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1663: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1664: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1665: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1666: {"type1_%": 0.891045711, "type2_%": 0.108954289, "type3_%": 0},
1667: {"type1_%": 0.8, "type2_%": 0.2, "type3_%": 0},
1668: {"type1_%": 0.666666667, "type2_%": 0.333333333, "type3_%": 0},
1669: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1673: {"type1_%": 0.666666667, "type2_%": 0.3, "type3_%": 0},
1674: {"type1_%": 0.666666667, "type2_%": 0.3, "type3_%": 0},
1675: {"type1_%": 0.666666667, "type2_%": 0.3, "type3_%": 0},
1676: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1677: {"type1_%": 1, "type2_%": 0, "type3_%": 0},
1678: {"type1_%": 0.666666667, "type2_%": 0.3, "type3_%": 0},
1679: {"type1_%": 0.666666667, "type2_%": 0.3, "type3_%": 0},
1680: {"type1_%": 0.666666667, "type2_%": 0.3, "type3_%": 0},
1681: {"type1_%": 0.666666667, "type2_%": 0.3, "type3_%": 0},
1687: {"type1_%": 0.606611029, "type2_%": 0.28211422, "type3_%": 0.11127475},
1688: {"type1_%": 0.92, "type2_%": 0.1, "type3_%": 0},
1689: {"type1_%": 0.96, "type2_%": 0, "type3_%": 0},
1690: {"type1_%": 0.94, "type2_%": 0.1, "type3_%": 0},
1691: {"type1_%": 0.75, "type2_%": 0.3, "type3_%": 0},
}
# Define a function to calculate new demands based on percentages
def calculate_demands(building_id, percentages, gdf_building_type, gdf_building_type_2, gdf_building_type_3):
new_demands = {}
for demand_type in [
'heating_demand_kWh', 'cooling_demand_kWh',
'domestic_hot_water_heat_demand_kWh',
'appliances_electrical_demand_kWh',
'lighting_electrical_demand_kWh']:
demand_type_1 = gdf_building_type.loc[gdf_building_type['id'] == building_id, demand_type].values[
0] if not gdf_building_type.empty else 0
demand_type_2 = gdf_building_type_2.loc[gdf_building_type_2['id'] == building_id, demand_type].values[
0] if not gdf_building_type_2.empty else 0
demand_type_3 = gdf_building_type_3.loc[gdf_building_type_3['id'] == building_id, demand_type].values[
0] if not gdf_building_type_3.empty else 0
new_demand = (
demand_type_1 * percentages['type1_%'] +
demand_type_2 * percentages['type2_%'] +
demand_type_3 * percentages['type3_%']
)
new_demands[demand_type] = new_demand
return new_demands
# Process each building in the GeoJSON
for idx, feature in gdf.iterrows():
building_id = feature['id'] # Adjust this based on your GeoJSON structure
if building_id == 1673:
percentages = percentage_data[building_id]
print(percentages)
new_demands = calculate_demands(building_id, percentages,
gdf_building_outputs.get('building_type', gpd.GeoDataFrame()),
gdf_building_outputs.get('building_type_2', gpd.GeoDataFrame()),
gdf_building_outputs.get('building_type_3', gpd.GeoDataFrame()))
print(new_demands)
# Update the properties of the feature
gdf.at[idx, 'heating_demand_kWh'] = new_demands['heating_demand_kWh']
gdf.at[idx, 'cooling_demand_kWh'] = new_demands['cooling_demand_kWh']
gdf.at[idx, 'domestic_hot_water_heat_demand_kWh'] = new_demands['domestic_hot_water_heat_demand_kWh']
gdf.at[idx, 'appliances_electrical_demand_kWh'] = new_demands['appliances_electrical_demand_kWh']
gdf.at[idx, 'lighting_electrical_demand_kWh'] = new_demands['lighting_electrical_demand_kWh']
print('test')
#
# if building_id in percentage_data:
# percentages = percentage_data[building_id]
# new_demands = calculate_demands(building_id, percentages,
# gdf_building_outputs.get('building_type', gpd.GeoDataFrame()),
# gdf_building_outputs.get('building_type_2', gpd.GeoDataFrame()),
# gdf_building_outputs.get('building_type_3', gpd.GeoDataFrame()))
#
# # Update the properties of each feature
# gdf.at[idx, 'heating_demand_kWh'] = new_demands['heating_demand_kWh']
# gdf.at[idx, 'cooling_demand_kWh'] = new_demands['cooling_demand_kWh']
# gdf.at[idx, 'domestic_hot_water_heat_demand_kWh'] = new_demands['domestic_hot_water_heat_demand_kWh']
# gdf.at[idx, 'appliances_electrical_demand_kWh'] = new_demands['appliances_electrical_demand_kWh']
# gdf.at[idx, 'lighting_electrical_demand_kWh'] = new_demands['lighting_electrical_demand_kWh']
#
# # Save the updated GeoDataFrame to the appropriate folder
# for folder, key in zip(output_folders, ['type1_%', 'type2_%', 'type3_%']):
# if percentages[key] > 0:
# output_gdf = gdf_building_outputs[folder]
# output_gdf = output_gdf.append(gdf.iloc[[idx]], ignore_index=True)
# output_gdf.to_file(output_paths[folder] / 'buildings.geojson', driver='GeoJSON')
#