Spaces:
Runtime error
Runtime error
"feat(viz): add alt distance and stop rating to map popups"
Browse files151 unmodified lines15215315415515615715814 unmodified lines17317417517617717817918018118218356 unmodified lines2402412422432442452462472482492502512522532542552562572582592601 unmodified line2622632642652662672682692702712724 unmodified lines2772782792802812822832842852862872882892901 unmodified line2922932942952962975 unmodified lines3033043053063073083092 unmodified lines3123133143153163171 unmodified line319320321322323324325326327328329330331332333151 unmodified linesif "lat" in sl and "lng" in sl:center_lat, center_lon = sl["lat"], sl["lng"]# 🔥 Map修正 1: height="100%" 讓它自動填滿父容器 (ui/theme.py 定義的 650px)m = folium.Map(location=[center_lat, center_lon], zoom_start=13, tiles="OpenStreetMap",height="100%",width="100%"14 unmodified linesindex_to_name = {stop.get("stop_index"): stop.get("location") for stop in timeline}poi_id_to_name = {}for t in raw_tasks:for cand in t.get("candidates", []):if cand.get("poi_id"):poi_id_to_name[cand["poi_id"]] = cand.get("name")task_id_to_seq = {}for r in route_info:56 unmodified linesroute_group.add_to(m)# --- Layer 2: 備用方案 (分組修正) ---# 🔥 Map修正 2: 不再使用單一的 alt_group,而是為每個任務建立獨立的 FeatureGroupfor idx, task in enumerate(tasks_detail):tid = task.get("task_id")step_seq = task_id_to_seq.get(tid, 0)# 取得該任務的主題色theme_idx = step_seq % len(THEMES)theme_color, theme_name = THEMES[theme_idx]chosen = task.get("chosen_poi", {})alternatives = task.get("alternative_pois", [])# 如果沒有備選點,就跳過if not chosen or not alternatives:continue1 unmodified lineif not center_lat or not center_lng:continue# 取得主地點名稱 (用於圖層標籤)chosen_name = poi_id_to_name.get(chosen.get("poi_id"), f"Task {idx + 1}")# 🔥 建立該任務專屬的 Group,並預設顯示 (show=True)# 圖層名稱範例: "↳ Alt: Taipei 101"specific_alt_group = folium.FeatureGroup(name=f"↳ Alt: {chosen_name}",show=True4 unmodified linesif alat and alng:bounds.append([alat, alng])# 虛線連接folium.PolyLine(locations=[[center_lat, center_lng], [alat, alng]],color=theme_color, weight=2, dash_array='5, 5', opacity=0.5).add_to(specific_alt_group)poi_name = poi_id_to_name.get(alt.get("poi_id"), "Alternative Option")extra_min = alt.get("delta_travel_time_min", 0)popup_html = create_popup_html(title="ALTERNATIVE",1 unmodified linecolor="gray",metrics={"Add. Time": f"+{extra_min} min",},is_alternative=True)5 unmodified linestooltip=f"Alt: {poi_name}").add_to(specific_alt_group)# 將這個任務的專屬 Group 加入地圖specific_alt_group.add_to(m)# --- Layer 3: 主要站點 ---2 unmodified linesfor i, stop in enumerate(timeline):coords = stop.get("coordinates", {})lat, lng = coords.get("lat"), coords.get("lng")if lat and lng:bounds.append([lat, lng])1 unmodified linecolor_code, theme_name = THEMES[theme_idx]loc_name = stop.get("location", "")popup_html = create_popup_html(title=f"STOP {i + 1}",subtitle=loc_name,color=theme_name,metrics={"Arrival": stop.get("time", ""),"Weather": stop.get("weather", "").split(',')[0],"AQI": stop.get("aqi", {}).get("label", "").split(' ')[-1]})if i == 0:151 unmodified lines15215315415515615715814 unmodified lines17317417517617717817918018118218318418518618718818919019119219319419519656 unmodified lines2532542552562572582592602612622632642652662672682691 unmodified line2712722732742752762772782792804 unmodified lines2852862872882892902912922932942952962972982993003011 unmodified line3033043053063073083095 unmodified lines3153163173183193202 unmodified lines3233243253263273283291 unmodified line331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361151 unmodified linesif "lat" in sl and "lng" in sl:center_lat, center_lon = sl["lat"], sl["lng"]# 🔥 Map修正 1: height="100%" 讓它自動填滿父容器m = folium.Map(location=[center_lat, center_lon], zoom_start=13, tiles="OpenStreetMap",height="100%",width="100%"14 unmodified linesindex_to_name = {stop.get("stop_index"): stop.get("location") for stop in timeline}# 🔥 Data Prep 1: 建立 POI 詳細資訊對照表 (包含 Name 和 Rating)poi_ref = {}for t in raw_tasks:for cand in t.get("candidates", []):pid = cand.get("poi_id")if pid:poi_ref[pid] = {"name": cand.get("name"),"rating": cand.get("rating")}# 🔥 Data Prep 2: 建立 Step 到 POI ID 的對照表 (用於將 Timeline 連結到 Rating)step_to_poi_id = {}for r in route_info:step_id = r.get("step")pid = r.get("poi_id")if step_id is not None and pid:step_to_poi_id[step_id] = pidtask_id_to_seq = {}for r in route_info:56 unmodified linesroute_group.add_to(m)# --- Layer 2: 備用方案 (FeatureGroup 分組) ---for idx, task in enumerate(tasks_detail):tid = task.get("task_id")step_seq = task_id_to_seq.get(tid, 0)theme_idx = step_seq % len(THEMES)theme_color, theme_name = THEMES[theme_idx]chosen = task.get("chosen_poi", {})alternatives = task.get("alternative_pois", [])if not chosen or not alternatives:continue1 unmodified lineif not center_lat or not center_lng:continue# 取得主地點名稱chosen_pid = chosen.get("poi_id")chosen_name = poi_ref.get(chosen_pid, {}).get("name", f"Task {idx + 1}")specific_alt_group = folium.FeatureGroup(name=f"↳ Alt: {chosen_name}",show=True4 unmodified linesif alat and alng:bounds.append([alat, alng])folium.PolyLine(locations=[[center_lat, center_lng], [alat, alng]],color=theme_color, weight=2, dash_array='5, 5', opacity=0.5).add_to(specific_alt_group)alt_pid = alt.get("poi_id")poi_name = poi_ref.get(alt_pid, {}).get("name", "Alternative Option")extra_min = alt.get("delta_travel_time_min", 0)# 🔥 更新:獲取 delta distance (注意 JSON key 是 'm' 而非 'meters')extra_dist = alt.get("delta_travel_distance_m", 0)popup_html = create_popup_html(title="ALTERNATIVE",1 unmodified linecolor="gray",metrics={"Add. Time": f"+{extra_min} min","Add. Dist": f"+{extra_dist} m" # 🔥 新增距離顯示},is_alternative=True)5 unmodified linestooltip=f"Alt: {poi_name}").add_to(specific_alt_group)specific_alt_group.add_to(m)# --- Layer 3: 主要站點 ---2 unmodified linesfor i, stop in enumerate(timeline):coords = stop.get("coordinates", {})lat, lng = coords.get("lat"), coords.get("lng")stop_idx = stop.get("stop_index")if lat and lng:bounds.append([lat, lng])1 unmodified linecolor_code, theme_name = THEMES[theme_idx]loc_name = stop.get("location", "")# 🔥 更新:透過 stop_index -> route -> poi_id -> rating 獲取評分rating_display = ""if stop_idx is not None:# 嘗試從 route map 找到 poi_idpid = step_to_poi_id.get(stop_idx)if pid and pid in poi_ref:rating_val = poi_ref[pid].get("rating")if rating_val:rating_display = f"{rating_val} ⭐"metrics_data = {"Arrival": stop.get("time", ""),"Weather": stop.get("weather", "").split(',')[0],"AQI": stop.get("aqi", {}).get("label", "").split(' ')[-1]}# 🔥 新增 Rating 到 Metricsif rating_display:metrics_data["Rating"] = rating_displaypopup_html = create_popup_html(title=f"STOP {i + 1}",subtitle=loc_name,color=theme_name,metrics=metrics_data)if i == 0:
core/visualizers.py+49-21