import re import folium from bs4 import BeautifulSoup from folium.features import DivIcon def get_color(point_type): return { "start": "#28a745", # green "end": "#dc3545", # red "service": "#007bff" # blue }.get(point_type, "#6c757d") # fallback: gray def generate_route_map(route_data, output_path="optimized_route_map.html"): """ Generates an interactive HTML map with a route visualization. Parameters: route_data (dict): Contains 'point_type', 'location_id', 'lat', and 'lon' lists. output_path (str): Path to save the resulting HTML file. """ # Combine route into a list of tuples full_route = [ (lat, lon, loc_id, p_type) for lat, lon, loc_id, p_type in zip( route_data["lat"], route_data["lon"], route_data["location_id"], route_data["point_type"] ) ] # Detect circular route (start and end are identical) start = full_route[0] end = full_route[-1] is_circular = ( start[2] == end[2] and start[0] == end[0] and start[1] == end[1] ) # For markers: exclude duplicate end point if circular display_route = full_route[:-1] if is_circular else full_route # Create map centered on start route_map = folium.Map(location=[start[0], start[1]], zoom_start=10) # Add numbered markers for idx, (lat, lon, loc_id, p_type) in enumerate(display_route, start=1): folium.map.Marker( location=(lat, lon), icon=DivIcon( icon_size=(30, 30), icon_anchor=(15, 15), html=f"""