import json import pandas as pd with open('./input_files/Lachine_Geojson_Corrected.geojson', 'r') as f: geojson_data = json.load(f) for feature in geojson_data['features']: properties = feature.get('properties', {}) properties.pop('detailed_model_filename', None) excel_df = pd.read_excel('./input_files/building_usage.xlsx') excel_df = excel_df.set_index('order of the file in geojson') excel_df = excel_df.sort_index() usage_codes = { 'Residential': "1000", 'Commercial': "6111", 'Industrial': "4413", 'Institutional': "6812", 'Office': "6591" } features = geojson_data['features'] assert len(features) == len(excel_df) print("The files' ranges match.") for idx, (feature, (excel_idx, row)) in enumerate(zip(features, excel_df.iterrows()), start=1): properties = feature.get('properties', {}) properties.pop('detailed_model_filename', None) # Get the surface areas from the Excel row commercial_area = row.get('Sup. commerciale (m²)', 0) or 0 industrial_area = row.get('Sup. indus.léger (m²)', 0) or 0 institutional_area = row.get('Sup. institutionnelle (m²)', 0) or 0 office_area = row.get('Sup. bureau (m²)', 0) or 0 residential_area = row.get('Résidentiels (m²)', 0) or 0 # Create a dictionary of areas with their corresponding usage codes areas = { usage_codes['Commercial']: commercial_area, usage_codes['Industrial']: industrial_area, usage_codes['Institutional']: institutional_area, usage_codes['Office']: office_area, usage_codes['Residential']: residential_area } # Remove entries with zero area areas = {code: area for code, area in areas.items() if area > 0} total_area = sum(areas.values()) if total_area == 0: print(f"Total area is zero for feature at index {idx}, skipping modification.") continue # Compute the percentage for each usage usages_list = [] for code, area in areas.items(): percentage = (area / total_area) * 100 usages_list.append({'usage': code, 'percentage': round(percentage)}) # Update 'building_type' and 'usages' in properties if len(usages_list) > 1: properties['building_type'] = 'mixed use' properties['usages'] = usages_list elif len(usages_list) == 1: # Single usage properties['building_type'] = list(areas.keys())[0] else: # No usages, skip modifying this feature continue # Add 'area' attribute from Excel properties['area'] = str(row.get('Area', '')) # Replace 'id' with 'development' from Excel development = row.get('development', '') cleaned_development = str(development).replace('-', '').replace(' ', '') properties['id'] = "Building" + cleaned_development with open('input_files/Lachine_Geojson_Mixed_Use.geojson', 'w') as f: json.dump(geojson_data, f, indent=2)