""" map_utils.py Builds interactive Folium maps on top of real OpenStreetMap tiles. """ import folium from graph_builder import get_city_coords, AP_CITIES COL_DIJKSTRA = "#00e676" COL_GROVER = "#ff1744" COL_BOTH = "#ffea00" def _edge_set(path): return {frozenset([path[i], path[i+1]]) for i in range(len(path)-1)} def build_map(G, dijkstra_result, grover_result, source, target, nearby_cities=None): src_ll = get_city_coords(source) dst_ll = get_city_coords(target) if src_ll and dst_ll: centre = ((src_ll[0]+dst_ll[0])/2, (src_ll[1]+dst_ll[1])/2) else: centre = (16.0, 80.5) m = folium.Map( location=centre, zoom_start=7, tiles="CartoDB dark_matter", control_scale=True, ) d_edges = _edge_set(dijkstra_result.get("path", [])) g_edges = _edge_set(grover_result.get("path", [])) for u, v, data in G.edges(data=True): edge_key = frozenset([u, v]) ll_u = get_city_coords(u) ll_v = get_city_coords(v) if not ll_u or not ll_v: continue in_d = edge_key in d_edges in_g = edge_key in g_edges if in_d and in_g: color, weight, dash, opacity = COL_BOTH, 7, None, 0.95 elif in_d: color, weight, dash, opacity = COL_DIJKSTRA, 6, None, 0.9 elif in_g: color, weight, dash, opacity = COL_GROVER, 5, "10 5", 0.85 else: color, weight, dash, opacity = "#334", 1.2, None, 0.3 folium.PolyLine( locations=[ll_u, ll_v], color=color, weight=weight, opacity=opacity, dash_array=dash, tooltip=f"{u} \u2194 {v} ({data.get('weight','')} km)", ).add_to(m) d_path = dijkstra_result.get("path", []) g_path = grover_result.get("path", []) path_cities = set(d_path + g_path) for city in G.nodes(): ll = get_city_coords(city) if not ll: continue if city == source: icon_color, icon = "orange", "play" elif city == target: icon_color, icon = "red", "flag" elif city in path_cities: icon_color, icon = "green", "map-marker" else: icon_color, icon = "purple", "circle" popup_html = f"""