coolride-telegram / testing /generate_final_menu.py
Swaminaatha krishna
chore: frontend wiring audited and secured
3f68f4c
Raw History Blame Contribute Delete
18.2 kB
"""
CoolRide β€” Final Field Test Menu Generator (v2)
=================================================
Generates SEPARATE blind KML files per route per location.
6 total KML files + briefing + secret key.
Usage:
python testing/generate_final_menu.py
"""
import os
import json
import time
from datetime import datetime
import networkx as nx
import osmnx as ox
import simplekml
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
from shapely.strtree import STRtree
import shapely
# ============================================================
# CONFIG
# ============================================================
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
GRAPH_PATH = os.path.join(ROOT_DIR, "Data", "singapore_enriched.graphml.gz")
OUTPUT_DIR = os.path.join(ROOT_DIR, "output")
LOGS_DIR = os.path.join(ROOT_DIR, "logs")
ROUTES = [
{
"name": "PUNGGOL",
"tag": "Blue Space Test",
"concept": "Waterway Breeze vs. Asphalt Heat",
"test_id": "TEST-PUNGGOL-03",
"start": (1.41959, 103.90714),
"end": (1.40200, 103.89920),
"start_label": "Near Waterway Point",
"end_label": "Punggol Settlement",
},
{
"name": "CBD",
"tag": "Urban Canyon Test",
"concept": "Skyscraper Shade vs. Exposed Main Roads",
"test_id": "TEST-CBD-03",
"start": (1.28895, 103.85648),
"end": (1.27279, 103.85114),
"start_label": "Suntec / Marina",
"end_label": "Tanjong Pagar / Maxwell",
},
{
"name": "FORT_CANNING",
"tag": "Tunnel vs Hill Test",
"concept": "Flat Tunnel/River Route vs. Canning Rise Hill Climb",
"test_id": "TEST-FC-TUNNEL",
"start": (1.29050, 103.84650),
"end": (1.29950, 103.84550),
"start_label": "Clarke Quay Central",
"end_label": "Plaza Singapura",
},
]
SIM_SUN_ALTITUDE = 50
SIM_SUN_AZIMUTH = 250
UPHILL_FACTOR = 15
UPHILL_CAP = 2.0
DOWNHILL_FACTOR = 3
DOWNHILL_CAP = 0.6
DOWNHILL_THRESHOLD = -0.05
COLOR_RED = "ff0000ff"
COLOR_BLUE = "ffff0000"
# ============================================================
# LOAD GRAPH & SPATIAL LAYERS
# ============================================================
print("=" * 65)
print(" FINAL FIELD TEST MENU v2 β€” SEPARATE KMLs")
print("=" * 65)
print(f"\nLoading graph...")
t0 = time.time()
G = ox.load_graphml(GRAPH_PATH)
print(f" Loaded in {time.time() - t0:.1f}s β€” {len(G.nodes):,} nodes, {len(G.edges):,} edges")
_shapely_v2 = int(shapely.__version__.split(".")[0]) >= 2
print("Loading spatial layers...")
tree_index, trees_gdf = None, None
try:
trees_df = pd.read_csv(os.path.join(ROOT_DIR, "Data", "trees.csv"))
geometry = [Point(xy) for xy in zip(trees_df.lng, trees_df.lat)]
trees_gdf = gpd.GeoDataFrame(trees_df, geometry=geometry, crs="EPSG:4326")
tree_index = STRtree(trees_gdf.geometry)
print(f" Trees: {len(trees_gdf):,}")
except Exception as e:
print(f" Trees: SKIPPED ({e})")
water_index, water_gdf = None, None
try:
water_gdf = gpd.read_file(os.path.join(ROOT_DIR, "Data", "URA_Waterbody.geojson"))
water_index = STRtree(water_gdf.geometry)
print(f" Water bodies: {len(water_gdf):,}")
except Exception as e:
print(f" Water: SKIPPED ({e})")
pcn_index, pcn_gdf = None, None
try:
pcn_gdf = gpd.read_file(os.path.join(ROOT_DIR, "Data", "ParkConnectorLoop.geojson"))
pcn_index = STRtree(pcn_gdf.geometry)
print(f" PCN segments: {len(pcn_gdf):,}")
except Exception as e:
print(f" PCN: SKIPPED ({e})")
def _nearest_dist(index, gdf, point):
if index is None:
return float("inf")
idx = index.nearest(point)
if _shapely_v2:
return point.distance(gdf.geometry.iloc[idx])
return point.distance(gdf.iloc[idx].geometry)
# ============================================================
# PRE-COMPUTE cool_weight
# ============================================================
print("Pre-computing cool_weight...")
t0 = time.time()
heat_index = max(0, (32.0 - 28) / 10)
humidity_index = max(0, (75.0 - 60) / 40)
weather_amplifier = 1.0 + (heat_index + humidity_index) * 0.25
for u, v, data in G.edges(data=True):
length = data.get("length", 10)
highway = data.get("highway", "")
surface = data.get("surface", "unknown")
if isinstance(highway, list): highway = highway[0]
if isinstance(surface, list): surface = surface[0]
base_weight = 1.0
if highway in ("cycleway", "path", "track"): base_weight = 0.8
elif highway in ("primary", "trunk", "secondary"): base_weight = 1.5
elif highway in ("residential", "service"): base_weight = 1.1
if surface in ("concrete", "paving_stones"): base_weight += 0.1
elif surface == "unpaved": base_weight += 0.3
bonus = 0
if tree_index or water_index or pcn_index:
if "geometry" in data:
midpoint = data["geometry"].interpolate(0.5, normalized=True)
else:
midpoint = Point(G.nodes[u]["x"], G.nodes[u]["y"])
if tree_index and _nearest_dist(tree_index, trees_gdf, midpoint) < 0.00015:
bonus += 0.3
if water_index and _nearest_dist(water_index, water_gdf, midpoint) < 0.0003:
bonus += 0.1
if pcn_index and _nearest_dist(pcn_index, pcn_gdf, midpoint) < 0.0001:
bonus += 0.4
lat1, lon1 = G.nodes[u]["y"], G.nodes[u]["x"]
lat2, lon2 = G.nodes[v]["y"], G.nodes[v]["x"]
bearing = ox.bearing.calculate_bearing(lat1, lon1, lat2, lon2)
sun_penalty = 0
if SIM_SUN_ALTITUDE >= 7:
diff = abs(bearing - SIM_SUN_AZIMUTH)
if diff > 180: diff = 360 - diff
sun_penalty = max(0, (90 - diff) / 90) * 0.2
weight = base_weight - (bonus * weather_amplifier) + sun_penalty
grade = float(data.get("grade", 0) or 0)
if grade > 0:
grade_penalty = min(grade * UPHILL_FACTOR, UPHILL_CAP)
elif grade < DOWNHILL_THRESHOLD:
grade_penalty = min(abs(grade) * DOWNHILL_FACTOR, DOWNHILL_CAP)
else:
grade_penalty = 0.0
data["cool_weight"] = length * max(0.5, weight) * (1.0 + grade_penalty)
print(f" Done in {time.time() - t0:.1f}s")
# ============================================================
# HELPERS
# ============================================================
def analyze_path(path):
total_dist = 0.0
elev_gain = 0.0
steep = 0
cum = [0.0]
for i in range(len(path) - 1):
u, v = path[i], path[i + 1]
edata = G.edges[u, v, 0] if G.has_edge(u, v) else {}
seg_len = edata.get("length", 0)
grade = float(edata.get("grade", 0) or 0)
total_dist += seg_len
cum.append(total_dist)
if grade > 0:
elev_gain += grade * seg_len
if abs(grade) > 0.05:
steep += 1
return total_dist, elev_gain, steep, cum
def find_checkpoints(path, cum, total_dist):
"""Return checkpoints at 0%, 50%, 100% of distance."""
results = []
for pct in [0.0, 0.5, 1.0]:
target = pct * total_dist
best_idx = min(range(len(cum)), key=lambda i: abs(cum[i] - target))
node = path[best_idx]
results.append({
"pct": pct,
"lat": float(G.nodes[node]["y"]),
"lon": float(G.nodes[node]["x"]),
"dist_m": cum[best_idx],
})
return results
def create_kml(path, checkpoints, test_id, route_label, color, filename):
"""Generate a single KML file with route line and 3 checkpoint pins."""
kml = simplekml.Kml(name=f"{route_label} β€” {test_id}")
# Route line
coords = [(float(G.nodes[n]["x"]), float(G.nodes[n]["y"])) for n in path]
ls = kml.newlinestring(name=route_label)
ls.coords = coords
ls.style.linestyle.color = color
ls.style.linestyle.width = 4
ls.description = f"Route ID: {test_id}\nFollow the line."
# Checkpoint pins
pin_names = ["START", "MIDPOINT", "FINISH"]
pin_icons = [
"http://maps.google.com/mapfiles/kml/paddle/grn-circle.png",
"http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png",
"http://maps.google.com/mapfiles/kml/paddle/red-circle.png",
]
for cp, name, icon in zip(checkpoints, pin_names, pin_icons):
pm = kml.newpoint(name=name)
pm.coords = [(cp["lon"], cp["lat"])]
pm.description = (
f"Stop & Log. ID: {test_id}\n"
f"{name} β€” {cp['dist_m']:.0f}m along route"
)
pm.style.iconstyle.icon.href = icon
pm.style.iconstyle.scale = 1.3
filepath = os.path.join(OUTPUT_DIR, filename)
kml.save(filepath)
return filepath
# ============================================================
# GENERATE ALL 6 KMLs
# ============================================================
os.makedirs(OUTPUT_DIR, exist_ok=True)
os.makedirs(LOGS_DIR, exist_ok=True)
all_results = []
generated_files = []
for route_cfg in ROUTES:
name = route_cfg["name"]
test_id = route_cfg["test_id"]
start = route_cfg["start"]
end = route_cfg["end"]
print(f"\n{'─' * 65}")
print(f" {name} β€” {route_cfg['tag']}")
print(f"{'─' * 65}")
orig = ox.distance.nearest_nodes(G, start[1], start[0])
dest = ox.distance.nearest_nodes(G, end[1], end[0])
orig_lat = float(G.nodes[orig]["y"])
orig_lon = float(G.nodes[orig]["x"])
dest_lat = float(G.nodes[dest]["y"])
dest_lon = float(G.nodes[dest]["x"])
orig_elev = float(G.nodes[orig].get("elevation", 0) or 0)
dest_elev = float(G.nodes[dest].get("elevation", 0) or 0)
print(f" Start: {orig_lat:.6f}, {orig_lon:.6f} (elev {orig_elev:.1f}m)")
print(f" End: {dest_lat:.6f}, {dest_lon:.6f} (elev {dest_elev:.1f}m)")
try:
path_fast = nx.shortest_path(G, orig, dest, weight="length")
path_cool = nx.shortest_path(G, orig, dest, weight="cool_weight")
except nx.NetworkXNoPath:
print(f" ERROR: No path found. Skipping.")
continue
dist_fast, gain_fast, steep_fast, cum_fast = analyze_path(path_fast)
dist_cool, gain_cool, steep_cool, cum_cool = analyze_path(path_cool)
edges_fast = set(zip(path_fast[:-1], path_fast[1:]))
edges_cool = set(zip(path_cool[:-1], path_cool[1:]))
union = edges_fast | edges_cool
jaccard = len(edges_fast & edges_cool) / len(union) if union else 1.0
print(f" Route A (Red): {dist_fast:.0f}m, gain {gain_fast:.1f}m, steep {steep_fast}")
print(f" Route B (Blue): {dist_cool:.0f}m, gain {gain_cool:.1f}m, steep {steep_cool}")
print(f" Overlap: {jaccard:.2f} | Elev saved: {gain_fast - gain_cool:.1f}m | Steep saved: {steep_fast - steep_cool}")
cp_fast = find_checkpoints(path_fast, cum_fast, dist_fast)
cp_cool = find_checkpoints(path_cool, cum_cool, dist_cool)
# Generate SEPARATE KML files
fname_a = f"FINAL_{name}_RouteA_Red.kml"
fname_b = f"FINAL_{name}_RouteB_Blue.kml"
f_a = create_kml(path_fast, cp_fast, test_id, "Route A (Red)", COLOR_RED, fname_a)
f_b = create_kml(path_cool, cp_cool, test_id, "Route B (Blue)", COLOR_BLUE, fname_b)
print(f" -> {f_a}")
print(f" -> {f_b}")
generated_files.extend([f_a, f_b])
all_results.append({
"name": name,
"tag": route_cfg["tag"],
"concept": route_cfg["concept"],
"test_id": test_id,
"start": {"lat": orig_lat, "lon": orig_lon, "label": route_cfg["start_label"]},
"end": {"lat": dest_lat, "lon": dest_lon, "label": route_cfg["end_label"]},
"route_a": {"distance_m": round(dist_fast, 1), "elev_gain_m": round(gain_fast, 1), "steep": steep_fast},
"route_b": {"distance_m": round(dist_cool, 1), "elev_gain_m": round(gain_cool, 1), "steep": steep_cool},
"comparison": {
"dist_delta_pct": round((dist_cool / dist_fast - 1) * 100, 1) if dist_fast > 0 else 0,
"elev_saved_m": round(gain_fast - gain_cool, 1),
"steep_saved": steep_fast - steep_cool,
"jaccard": round(jaccard, 3),
},
})
# ============================================================
# SECRET CODE REVEAL
# ============================================================
secret_path = os.path.join(OUTPUT_DIR, "secret_code_reveal.txt")
lines = [
"=" * 65,
" SECRET CODE REVEAL β€” FOR ROUTE MASTER ONLY",
" DO NOT SHARE WITH TESTERS UNTIL AFTER DATA COLLECTION",
"=" * 65,
"",
" For ALL options:",
" Route A (Red) = STANDARD route (shortest distance)",
" Route B (Blue) = ALGORITHM route (CoolRide heat/hill avoidance)",
"",
]
for r in all_results:
c = r["comparison"]
lines += [
f" {r['name']} ({r['test_id']})",
f" A (Std): {r['route_a']['distance_m']:.0f}m | gain {r['route_a']['elev_gain_m']:.1f}m | steep {r['route_a']['steep']}",
f" B (Alg): {r['route_b']['distance_m']:.0f}m | gain {r['route_b']['elev_gain_m']:.1f}m | steep {r['route_b']['steep']}",
f" Delta: dist {c['dist_delta_pct']:+.1f}% | elev saved {c['elev_saved_m']:.1f}m | steep saved {c['steep_saved']} | overlap {c['jaccard']:.2f}",
"",
]
lines += ["=" * 65, f" Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", "=" * 65]
with open(secret_path, "w") as f:
f.write("\n".join(lines) + "\n")
generated_files.append(secret_path)
# ============================================================
# TEAM BRIEFING
# ============================================================
briefing_path = os.path.join(OUTPUT_DIR, "Team_Briefing_Menu.txt")
briefing = f"""================================================================
COOLRIDE FIELD TEST β€” ROUTE MENU
Wednesday, February 11, 2026 | 3:00 PM β€” 5:00 PM
================================================================
Team! We have 3 options for tomorrow's field test.
Each has a Red route and a Blue route. You'll each ride one.
Vote for your preferred location!
────────────────────────────────────────────────────────────────
OPTION 1: PUNGGOL β€” "Blue Space Test"
────────────────────────────────────────────────────────────────
What: Waterway Breeze vs. Asphalt Heat
Start: Near Waterway Point
End: Punggol Settlement
Dist: Route A {all_results[0]['route_a']['distance_m']:.0f}m / Route B {all_results[0]['route_b']['distance_m']:.0f}m
Vibe: Suburban waterway. Good food at Punggol Settlement!
ID: TEST-PUNGGOL-03
────────────────────────────────────────────────────────────────
OPTION 2: CBD β€” "Urban Canyon Test"
────────────────────────────────────────────────────────────────
What: Skyscraper Shade vs. Exposed Main Roads
Start: Suntec / Marina area
End: Tanjong Pagar / Maxwell
Dist: Route A {all_results[1]['route_a']['distance_m']:.0f}m / Route B {all_results[1]['route_b']['distance_m']:.0f}m
Vibe: Downtown towers. Maxwell Food Centre at finish.
ID: TEST-CBD-03
────────────────────────────────────────────────────────────────
OPTION 3: FORT CANNING β€” "Tunnel vs Hill Test"
────────────────────────────────────────────────────────────────
What: Flat Tunnel/River Route vs. Canning Rise Hill Climb
Start: Clarke Quay Central
End: Plaza Singapura
Dist: Route A {all_results[2]['route_a']['distance_m']:.0f}m / Route B {all_results[2]['route_b']['distance_m']:.0f}m
Vibe: Heritage district. Does the algorithm dodge the hill?
ID: TEST-FC-TUNNEL
════════════════════════════════════════════════════════════════
PROTOCOL:
- You get ONE KML file. Follow ONLY your colored line.
- Use "blue dot" method. Do NOT start Google Maps navigation.
- Stop at each pin (START / MIDPOINT / FINISH) to log data.
- Do NOT discuss routes until all data is submitted.
- Hydrate. Helmet on. Safety > Data.
Reply with: 1, 2, or 3!
════════════════════════════════════════════════════════════════"""
with open(briefing_path, "w") as f:
f.write(briefing + "\n")
generated_files.append(briefing_path)
# ============================================================
# REGISTRY LOG
# ============================================================
registry_path = os.path.join(LOGS_DIR, "field_test_registry.jsonl")
with open(registry_path, "a") as f:
f.write(json.dumps({
"batch": "FINAL_MENU_V2",
"generated_at": datetime.now().isoformat(),
"options": all_results,
}) + "\n")
# ============================================================
# SUMMARY
# ============================================================
print(f"\n{'=' * 65}")
print(f" COMPLETE β€” {len(generated_files)} files generated")
print(f"{'=' * 65}")
print(f"\n {'#':<3} {'Location':<16} {'A (Red)':>9} {'B (Blue)':>9} {'ElevSvd':>9} {'SteepSvd':>9} {'Overlap':>8}")
print(f" {'-'*65}")
for i, r in enumerate(all_results, 1):
c = r["comparison"]
print(
f" {i:<3} {r['name']:<16} "
f"{r['route_a']['distance_m']:>8.0f}m "
f"{r['route_b']['distance_m']:>8.0f}m "
f"{c['elev_saved_m']:>+8.1f}m "
f"{c['steep_saved']:>+9} "
f"{c['jaccard']:>8.2f}"
)
print(f"\n KML Files (6):")
for f in generated_files:
if f.endswith(".kml"):
print(f" {f}")
print(f"\n Documents:")
print(f" {briefing_path}")
print(f" {secret_path}")
print(f"{'=' * 65}")