from pathlib import Path from scripts.ep_workflow import energy_plus_workflow from hub.imports.geometry_factory import GeometryFactory from hub.helpers.dictionaries import Dictionaries from hub.imports.construction_factory import ConstructionFactory from hub.imports.usage_factory import UsageFactory from hub.imports.weather_factory import WeatherFactory import hub.helpers.constants as cte from hub.imports.energy_systems_factory import EnergySystemsFactory import geopandas as gpd # Specify the GeoJSON file path input_files_path = (Path(__file__).parent / 'input_files') building_type_2_modelling=2 #'Lachine_New_Developments.geojson' geojson_file_path = input_files_path / 'Lachine_moved_2024_type.geojson' if building_type_2_modelling==1: gdf = gpd.read_file(geojson_file_path) # Filter gdf when 'building_type_2' is not null filtered_gdf = gdf[gdf['building_type_2'].notnull()] output_geojson =input_files_path /'Lachine_moved_2024_type_2.geojson' geojson_file_path =output_geojson filtered_gdf.to_file(output_geojson, driver='GeoJSON') print(f"New GeoJSON saved in: {output_geojson}") if building_type_2_modelling==2: gdf = gpd.read_file(geojson_file_path) # Filter gdf when 'building_type_3' is not null filtered_gdf = gdf[gdf['building_type_3'].notnull()] output_geojson =input_files_path /'Lachine_moved_2024_type_3.geojson' geojson_file_path = output_geojson filtered_gdf.to_file(output_geojson, driver='GeoJSON') print(f"New GeoJSON saved in: {output_geojson}") output_path = (Path(__file__).parent / 'out_files').resolve() output_path.mkdir(parents=True, exist_ok=True) # Create city object from GeoJSON file city = GeometryFactory('geojson', path=geojson_file_path, height_field='maximum_roof_height', year_of_construction_field='year_built', function_field='building_type', function_to_hub=Dictionaries().montreal_function_to_hub_function).city # Enrich city data ConstructionFactory('nrcan', city).enrich() UsageFactory('nrcan', city).enrich() WeatherFactory('epw', city).enrich() energy_plus_workflow(city) def to_dict(building, total_floor_area): return { 'roof_area': building.floor_area, 'total_floor_area': total_floor_area, 'year_of_construction' : building.year_of_construction, 'type_function':building.function, 'beam_kWh_per_m2': sum(building.beam[cte.HOUR])/ (3.6e6), 'diffuse_kWh_per_m2': sum(building.diffuse[cte.HOUR])/ (3.6e6), 'direct_normal_kWh_per_m2': sum(building.direct_normal[cte.HOUR])/ (3.6e6), 'average_storey_height_meters': building.average_storey_height, 'max_height_meters_meters': building.max_height, 'global_horizontal_kWh_per_m2': sum(building.global_horizontal[cte.HOUR])/ (3.6e6), 'appliances_peak_load_kW':building.appliances_peak_load[cte.YEAR][0]/ (1e3), 'domestic_hot_water_peak_load_kW': building.domestic_hot_water_peak_load[cte.YEAR][0]/ (1e3), 'heating_peak_load_kW': building.heating_peak_load[cte.YEAR][0]/ (1e3), 'cooling_peak_load_kW': building.cooling_peak_load[cte.YEAR][0]/ (1e3), 'lighting_peak_load_kW': building.lighting_peak_load[cte.YEAR][0]/ (1e3), 'heating_demand_kWh_per_m2' : sum(building.heating_demand[cte.HOUR])/ (3.6e6 * total_floor_area), 'cooling_demand_kWh_per_m2' : sum(building.cooling_demand[cte.HOUR])/ (3.6e6 * total_floor_area), 'domestic_hot_water_heat_demand_kWh_per_m2': sum(building.domestic_hot_water_heat_demand[cte.HOUR])/ (3.6e6 * total_floor_area), 'appliances_electrical_demand_kWh_per_m2':sum(building.appliances_electrical_demand[cte.HOUR])/ (3.6e6 * total_floor_area), 'lighting_electrical_demand_kWh_per_m2': sum(building.lighting_electrical_demand[cte.HOUR])/ (3.6e6 * total_floor_area), 'heating_demand_kWh': [x / (cte.WATTS_HOUR_TO_JULES * 1000) for x in building.heating_demand[cte.HOUR]], 'cooling_demand_kWh':[x / (cte.WATTS_HOUR_TO_JULES * 1000) for x in building.cooling_demand[cte.HOUR]], 'domestic_hot_water_heat_demand_kWh': [x / (cte.WATTS_HOUR_TO_JULES * 1000) for x in building.domestic_hot_water_heat_demand[cte.HOUR]], 'appliances_electrical_demand_kWh':[x / (cte.WATTS_HOUR_TO_JULES * 1000) for x in building.appliances_electrical_demand[cte.HOUR]], 'lighting_electrical_demand_kWh': [x / (cte.WATTS_HOUR_TO_JULES * 1000) for x in building.lighting_electrical_demand[cte.HOUR]] } buildings_dic={} for building in city.buildings: total_floor_area = 0 for thermal_zone in building.thermal_zones_from_internal_zones: total_floor_area += thermal_zone.total_floor_area print(building.heating_demand[cte.YEAR][0] / (3.6e6 * total_floor_area)) building.energy_systems_archetype_name = 'system 1 gas' buildings_dic[building.name] = to_dict(building,total_floor_area) print('test') """"EXPORTERS""" import pandas as pd # Convert the dictionary to a DataFrame df = pd.DataFrame.from_dict(buildings_dic, orient='index') # Export the DataFrame to an Excel file excel_file_path = r'C:\Users\a_gabald\PycharmProjects\summer_course_2024\out_files\buildings.xlsx' df.to_excel(excel_file_path, index=True, index_label='Building') import json def make_json_serializable(data): if isinstance(data, (str, int, float, bool, type(None))): return data elif isinstance(data, dict): return {k: make_json_serializable(v) for k, v in data.items()} elif isinstance(data, list): return [make_json_serializable(item) for item in data] else: return str(data) # Convert any other type to a string # Load the existing GeoJSON file with open(geojson_file_path, 'r') as f: geojson_data = json.load(f) # Update the properties of each feature for feature in geojson_data['features']: # Attempt to retrieve the building_id from 'id' or 'properties' building_id = feature.get('id') or feature['properties'].get('id') if building_id in buildings_dic: serializable_properties = make_json_serializable(buildings_dic[building_id]) feature['properties'].update(serializable_properties) # Save the updated GeoJSON to a new file updated_geojson_file_path = r'C:\Users\a_gabald\PycharmProjects\summer_course_2024\out_files\updated_buildings.geojson' # Replace with your actual path with open(updated_geojson_file_path, 'w') as f: json.dump(geojson_data, f, indent=4) # EnergySystemsFactory('montreal_custom', city).enrich() # print('test') # for building in city.buildings: # energy_systems = building.energy_systems # for energy_system in energy_systems: # generation_units = energy_system.generation_systems # if cte.HEATING in energy_system.demand_types: # for generation_unit in generation_units: # generation_unit.heat_efficiency = 0.96 # # for building in city.buildings: # # building.function = cte.COMMERCIAL # # # # ConstructionFactory('nrcan', city).enrich() # # UsageFactory('nrcan', city).enrich() # # energy_plus_workflow(city) # # for building in city.buildings: # # print(building.heating_demand[cte.YEAR][0] / 3.6e6) # # print(building.name) # # total_floor_area = 0 # # for thermal_zone in building.thermal_zones_from_internal_zones: # # total_floor_area += thermal_zone.total_floor_area # # print(building.heating_demand[cte.YEAR][0] / (3.6e6 * total_floor_area)) # for building in city.buildings: # print(building.name) # print(building.year_of_construction) # print(building.usages_percentage)