import folium import geopandas as gpd from pathlib import Path import json # Load the GeoJSON file and ensure the 'id' field is included geojson_path = Path(__file__).parent / 'input_files' / 'buildings_to_map.geojson' with open(geojson_path, 'r') as f: geojson_data = json.load(f) # Convert GeoJSON to GeoDataFrame, explicitly adding 'id' as a column geo_data = gpd.GeoDataFrame.from_features(geojson_data["features"]) geo_data["id"] = [feature["id"] for feature in geojson_data["features"]] # Ensure the GeoDataFrame has a CRS (assuming WGS84 if not already defined) if geo_data.crs is None: geo_data.set_crs(epsg=4326, inplace=True) # Function to assign colors based on building function def get_color(building_function): color_map = { "residential": "lightblue", # Changed from blue to lightblue for better text visibility "secondary school": "green", "medium office": "red", "sports location": "orange", # Example for your data "stand alone retail": "purple" } return color_map.get(building_function, "gray") # Default color is gray # Create a style function for the GeoJson layer def style_function(feature): building_function = feature["properties"].get("function", "unknown") return { "fillColor": get_color(building_function), "color": "black", # Outline color "weight": 1, "fillOpacity": 0.6 } # Get the centroid coordinates for the map center centroid = geo_data.geometry.centroid.iloc[0] center = (centroid.y, centroid.x) # lat, lon # Create a Folium map with CartoDB Positron tiles m = folium.Map(location=center, zoom_start=50, tiles="CartoDB positron") # Add the GeoJSON layer with color-coded styles folium.GeoJson( geojson_data, name="Buildings", style_function=style_function ).add_to(m) # Add labels for each building for _, row in geo_data.iterrows(): feature_id = row['id'] # Access the 'id' attribute feature_centroid = row['geometry'].centroid # Get the centroid of the feature # Create a rotated label folium.Marker( location=(feature_centroid.y, feature_centroid.x), icon=folium.DivIcon(html=f'