import json from pathlib import Path import pandas as pd import geopandas as gpd #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 path1=r'C:\Users\a_gabald\PycharmProjects\summer_course_2024\out_files\building_type\updated_buildings.geojson' path2=r'C:\Users\a_gabald\PycharmProjects\summer_course_2024\out_files\building_type_2\updated_buildings.geojson' path3=r'C:\Users\a_gabald\PycharmProjects\summer_course_2024\out_files\building_type_3\updated_buildings.geojson' with open(path1, 'r') as f: building_type_data = json.load(f) with open(path2, 'r') as f: building_type_data2 = json.load(f) with open(path3, 'r') as f: building_type_data3 = json.load(f) percentage_data = { 1646: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 2672.550473, "total_floor_area": 26725.50473}, 1647: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 2653.626087, "total_floor_area": 26536.26087}, 1648: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1056.787496, "total_floor_area": 10567.87496}, 1649: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1906.620746, "total_floor_area": 19066.20746}, 1650: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 659.1119416, "total_floor_area": 5272.895533}, 1651: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1167.208109, "total_floor_area": 9337.664871}, 1652: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1193.251653, "total_floor_area": 9546.013222}, 1653: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1491.722543, "total_floor_area": 11933.78035}, 1654: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1168.005028, "total_floor_area": 9344.040224}, 1655: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1264.906961, "total_floor_area": 10119.25569}, 1656: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1281.768818, "total_floor_area": 10254.15054}, 1657: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 290.3886018, "total_floor_area": 2323.108814}, 1658: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 847.5095193, "total_floor_area": 6780.076155}, 1659: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1115.319153, "total_floor_area": 8922.553224}, 1660: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 469.2918062, "total_floor_area": 3754.33445}, 1661: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1292.298346, "total_floor_area": 10338.38677}, 1662: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 625.7828863, "total_floor_area": 5006.263091}, 1663: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1876.02897, "total_floor_area": 15008.23176}, 1664: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1118.224781, "total_floor_area": 22364.49562}, 1665: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 1502.787808, "total_floor_area": 30055.75617}, 1666: {"type1_%": 0.891045711, "type2_%": 0.108954289, "type3_%": 0, "roof_area": 3038.486076, "total_floor_area": 30384.86076}, 1667: {"type1_%": 0.8, "type2_%": 0.2, "type3_%": 0, "roof_area": 1343.832818, "total_floor_area": 13438.32818}, 1668: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 961.0996956, "total_floor_area": 4805.498478}, 1669: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 489.1282111, "total_floor_area": 1956.512845}, 1673: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 1693.141465, "total_floor_area": 5079.424396}, 1674: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 3248.827576, "total_floor_area": 9746.482729}, 1675: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 4086.842191, "total_floor_area": 12260.52657}, 1676: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 2786.114146, "total_floor_area": 11144.45658}, 1677: {"type1_%": 1, "type2_%": 0, "type3_%": 0, "roof_area": 5142.784184, "total_floor_area": 15428.35255}, 1678: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 6068.664574, "total_floor_area": 18205.99372}, 1679: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 5646.751407, "total_floor_area": 16940.25422}, 1680: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 1601.765953, "total_floor_area": 4805.297859}, 1681: {"type1_%": 0.7, "type2_%": 0.3, "type3_%": 0, "roof_area": 9728.221797, "total_floor_area": 29184.66539}, 1687: {"type1_%": 0.606611029, "type2_%": 0.28211422, "type3_%": 0.11127475, "roof_area": 4268.608743, "total_floor_area": 59760.52241}, 1688: {"type1_%": 0.92, "type2_%": 0.08, "type3_%": 0, "roof_area": 2146.654828, "total_floor_area": 38639.7869}, 1689: {"type1_%": 0.96, "type2_%": 0.04, "type3_%": 0, "roof_area": 2860.270711, "total_floor_area": 57205.41421}, 1690: {"type1_%": 0.94, "type2_%": 0.06, "type3_%": 0, "roof_area": 2189.732519, "total_floor_area": 28466.52275}, 1691: {"type1_%": 0.75, "type2_%": 0.25, "type3_%": 0, "roof_area": 3159.077523, "total_floor_area": 31590.77523}, } # Define a function to calculate new demands based on percentages def calculate_demands(building_id, percentages, building_type_data, building_type_data2, building_type_data3): new_demands = {} for idx, feature in enumerate(building_type_data['features']): if feature['properties']['id'] == str(building_id): 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_list_1 = building_type_data['features'][idx]['properties'].get(demand_type, [0]) # Initialize demand lists for type 2 and type 3 with zeros demand_list_2 = [0] * len(demand_list_1) demand_list_3 = [0] * len(demand_list_1) # Update demand_list_2 if percentages['type2_%'] is not zero if percentages['type2_%'] != 0: for feature2 in building_type_data2['features']: if feature2['properties']['id'] == str(building_id): demand_list_2 = feature2['properties'].get(demand_type, [0]) break # Update demand_list_3 if percentages['type3_%'] is not zero if percentages['type3_%'] != 0: for feature3 in building_type_data3['features']: if feature3['properties']['id'] == str(building_id): demand_list_3 = feature3['properties'].get(demand_type, [0]) break # Ensure the demand lists are the same length by padding with zeros if necessary max_len = max(len(demand_list_1), len(demand_list_2), len(demand_list_3)) demand_list_1 += [0] * (max_len - len(demand_list_1)) demand_list_2 += [0] * (max_len - len(demand_list_2)) demand_list_3 += [0] * (max_len - len(demand_list_3)) new_demand_list = [ demand_list_1[i] * percentages['type1_%'] + demand_list_2[i] * percentages['type2_%'] + demand_list_3[i] * percentages['type3_%'] for i in range(max_len) ] new_demands[demand_type] = new_demand_list return new_demands # Calculate demand per square meter for each building def calculate_demand_per_m2(building_type_data, building_type_data2, building_type_data3, percentage_data): results = {} for building_id, percentages in percentage_data.items(): print(percentages ["total_floor_area"]) new_demands = calculate_demands(building_id, percentages, building_type_data, building_type_data2, building_type_data3) for idx, feature in enumerate(building_type_data['features']): if feature['properties']['id'] == str(building_id): total_floor_area = percentages ["total_floor_area"] results[building_id] = { 'roof_area':percentages["roof_area"], 'total_floor_area': percentages["total_floor_area"], 'heating_demand_kWh':new_demands.get('heating_demand_kWh', []), 'cooling_demand_kWh':new_demands.get('cooling_demand_kWh', []), 'domestic_hot_water_heat_demand_kWh': new_demands.get('domestic_hot_water_heat_demand_kWh', []), 'appliances_electrical_demand_kWh': new_demands.get('appliances_electrical_demand_kWh', []), 'lighting_electrical_demand_kWh': new_demands.get('lighting_electrical_demand_kWh', []), 'heating_demand_kWh_per_m2': sum(new_demands.get('heating_demand_kWh', [])) / total_floor_area, 'cooling_demand_kWh_per_m2': sum(new_demands.get('cooling_demand_kWh', [])) / total_floor_area, 'domestic_hot_water_heat_demand_kWh_per_m2': sum( new_demands.get('domestic_hot_water_heat_demand_kWh', [])) / total_floor_area, 'appliances_electrical_demand_kWh_per_m2': sum( new_demands.get('appliances_electrical_demand_kWh', [])) / total_floor_area, 'lighting_electrical_demand_kWh_per_m2': sum( new_demands.get('lighting_electrical_demand_kWh', [])) / total_floor_area, 'appliances_peak_load_kW': max( new_demands.get('appliances_electrical_demand_kWh', [])), 'domestic_hot_water_peak_load_kW': max( new_demands.get('domestic_hot_water_heat_demand_kWh', [])) , 'heating_peak_load_kW': max(new_demands.get('heating_demand_kWh', [])), 'cooling_peak_load_kW': max(new_demands.get('cooling_demand_kWh', [])), 'lighting_peak_load_kW': max( new_demands.get('lighting_electrical_demand_kWh', [])), } return results # Example usage new_demands = calculate_demand_per_m2(building_type_data, building_type_data2, building_type_data3, percentage_data) # for building_id, demand_data in results.items(): # print(f"Building ID: {building_id}") # for demand_type, value in demand_data.items(): # print(f" {demand_type}: {value}") # new_demands = {} # for building_id, percentages in percentage_data.items(): # print(building_id) # new_demands[building_id] = calculate_demands(building_id, percentages, building_type_data, building_type_data2, building_type_data3) # # import pandas as pd # Export the DataFrame to an Excel file output_path = r'C:\Users\a_gabald\PycharmProjects\summer_course_2024\out_files\new_demands.xlsx' # Create an Excel writer object with pd.ExcelWriter(output_path, engine='xlsxwriter') as writer: for building_id, demands in new_demands.items(): # Convert demands to a DataFrame df_demands = pd.DataFrame(demands) # Convert building_id to string and check its length sheet_name = str(building_id) if len(sheet_name) > 31: sheet_name = sheet_name[:31] # Truncate to 31 characters if necessary # Write the DataFrame to a specific sheet named after the building_id df_demands.to_excel(writer, sheet_name=sheet_name, index=False) import matplotlib.pyplot as plt import numpy as np # Assume calculate_demand_per_m2 and results have been computed as before def extract_demands(data, building_id, demand_type): for feature in data['features']: if feature['properties']['id'] == str(building_id): return feature['properties'].get(demand_type, [0]) return 0 # Create a list of building IDs building_ids = list(percentage_data.keys()) # Define the demand types to be plotted demand_types = [ 'heating_demand_kWh_per_m2', 'cooling_demand_kWh_per_m2', 'domestic_hot_water_heat_demand_kWh_per_m2', 'appliances_electrical_demand_kWh_per_m2', 'lighting_electrical_demand_kWh_per_m2' ] # Initialize lists to store values for plotting new_demands_values = {demand_type: [] for demand_type in demand_types} building_type_data_values = {demand_type: [] for demand_type in demand_types} building_type_data2_values = {demand_type: [] for demand_type in demand_types} building_type_data3_values = {demand_type: [] for demand_type in demand_types} # Populate the lists with the corresponding values for building_id in building_ids: for demand_type in demand_types: new_demands_values[demand_type].append(new_demands[building_id][demand_type]) building_type_data_values[demand_type].append(extract_demands(building_type_data, building_id, demand_type)) building_type_data2_values[demand_type].append(extract_demands(building_type_data2, building_id, demand_type)) building_type_data3_values[demand_type].append(extract_demands(building_type_data3, building_id, demand_type)) # Plot the values for each demand type for demand_type in demand_types: x = np.arange(len(building_ids)) # the label locations width = 0.2 # the width of the bars fig, ax = plt.subplots(figsize=(15, 7)) ax.bar(x - 1.5 * width, new_demands_values[demand_type], width, label='New Demands') ax.bar(x - 0.5 * width, building_type_data_values[demand_type], width, label='Old Demands - Type 1') ax.bar(x + 0.5 * width, building_type_data2_values[demand_type], width, label='Old Demands - Type 2') ax.bar(x + 1.5 * width, building_type_data3_values[demand_type], width, label='Old Demands - Type 3') # Add some text for labels, title, and custom x-axis tick labels, etc. ax.set_xlabel('Building ID') ax.set_ylabel(f'{demand_type} (kWh/m²)') ax.set_title(f'Comparison of {demand_type} by Building ID') ax.set_xticks(x) ax.set_xticklabels(building_ids, rotation=90) ax.legend() fig.tight_layout() plt.show() # Load the existing GeoJSON file geojson_file_path =r'C:\Users\a_gabald\PycharmProjects\summer_course_2024\out_files\building_type\updated_buildings.geojson' # Replace with the actual path with open(geojson_file_path, 'r') as f: geojson_data = json.load(f) # Define output path output_path = (Path(__file__).parent / 'out_files').resolve() output_path.mkdir(parents=True, exist_ok=True) # Load additional data from the CSV files solar_radiation_tilted_path = r'C:\Users\a_gabald\PycharmProjects\summer_course_2024\out_files\solar_radiation_tilted_selected_buildings.csv' solar_radiation_horizontal_path = r'C:\Users\a_gabald\PycharmProjects\summer_course_2024\out_files\solar_radiation_horizontal_selected_buildings.csv' import csv def load_solar_data(file_path, key_name): solar_data = {} with open(file_path, 'r') as f: reader = csv.reader(f) headers = next(reader)[1:] # Skip the first header and get building IDs for row in reader: for building_id, solar_value in zip(headers, row[1:]): # Skip the first column in each row building_id = int(building_id) solar_value = float(solar_value) if building_id not in solar_data: solar_data[building_id] = {} if key_name not in solar_data[building_id]: solar_data[building_id][key_name] = [] solar_data[building_id][key_name].append(solar_value) return solar_data solar_radiation_tilted = load_solar_data(solar_radiation_tilted_path, 'solar_radiation_tilted') solar_radiation_horizontal = load_solar_data(solar_radiation_horizontal_path, 'solar_radiation_horizontal') # Merge the solar data into a single dictionary solar_data = {} for building_id in set(solar_radiation_tilted.keys()).union(solar_radiation_horizontal.keys()): solar_data[building_id] = {} if building_id in solar_radiation_tilted: solar_data[building_id]['solar_radiation_tilted'] = solar_radiation_tilted[building_id][ 'solar_radiation_tilted'] if building_id in solar_radiation_horizontal: solar_data[building_id]['solar_radiation_horizontal'] = solar_radiation_horizontal[building_id][ 'solar_radiation_horizontal'] # Example data for results (use actual results data in practice) results = { 1646: { "appliances_peak_load_kW": 269.020198862769, "domestic_hot_water_peak_load_kW": 1477.6420222203596, "heating_peak_load_kW": 1650.4097261870008, "cooling_peak_load_kW": 1441.1027982978985, "lighting_peak_load_kW": 295.92221874904584, "heating_demand_kWh_per_m2": 40.4922413088911, "cooling_demand_kWh_per_m2": 20.33948801154316, "domestic_hot_water_heat_demand_kWh_per_m2": 60.25215210051923, "appliances_electrical_demand_kWh_per_m2": 19.352038723954898, "lighting_electrical_demand_kWh_per_m2": 11.246090428260413, } # Add similar dictionaries for other building IDs } # Update GeoJSON with percentage data, results, and solar data for feature in geojson_data['features']: building_id = int(feature['properties']['id']) # Update with percentage data if building_id in percentage_data: for key, value in percentage_data[building_id].items(): feature['properties'][key] = value # Update with results data if building_id in new_demands: for key, value in new_demands[building_id].items(): feature['properties'][key] = value # Update with solar data if building_id in solar_data: for key, value in solar_data[building_id].items(): feature['properties'][key] = value # Calculate and add the sums feature['properties']['sum_solar_radiation_tilted_kWh_per_m2'] = sum(solar_data[building_id]['solar_radiation_tilted']) / 3.6e6 feature['properties']['sum_solar_radiation_horizontal_kWh_per_m2'] = sum(solar_data[building_id]['solar_radiation_horizontal']) / 3.6e6 # Add 8760 arrays divided by 3.6e6 feature['properties']['solar_radiation_tilted_kWh_per_m2'] = [value / 3.6e6 for value in solar_data[building_id]['solar_radiation_tilted']] feature['properties']['solar_radiation_horizontal_kWh_per_m2'] = [value / 3.6e6 for value in solar_data[building_id]['solar_radiation_horizontal']] # Save the updated GeoJSON data to a new file updated_geojson_file_path = output_path / 'updated_buildings_with_all_data.geojson' with open(updated_geojson_file_path, 'w') as f: json.dump(geojson_data, f) print(f"Updated GeoJSON data saved to {updated_geojson_file_path}")