Spaces:
Running
Running
Upload app.py
Browse files- src/app.py +42 -6
src/app.py
CHANGED
|
@@ -99,8 +99,43 @@ async def get_route_view(route_id: str):
|
|
| 99 |
all_buses = data.get("vehicles", [])
|
| 100 |
route_buses = [v for v in all_buses if v['route'] == route_id]
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if not route_buses:
|
| 103 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
# IMPORTANT: Cast Trip IDs to strings to ensure they match the DB
|
| 106 |
trip_ids = [str(v['trip_id']) for v in route_buses]
|
|
@@ -157,7 +192,8 @@ async def get_route_view(route_id: str):
|
|
| 157 |
return {
|
| 158 |
"route": route_id,
|
| 159 |
"count": len(enriched),
|
| 160 |
-
"vehicles": enriched
|
|
|
|
| 161 |
}
|
| 162 |
|
| 163 |
@app.get("/api/vehicles/{vehicle_id}")
|
|
@@ -341,11 +377,11 @@ async def get_alerts_for_route(route_id: str):
|
|
| 341 |
|
| 342 |
@app.get("/api/nearby")
|
| 343 |
async def get_nearby_context(lat: float, lon: float):
|
| 344 |
-
# Bounding box approximation for ~
|
| 345 |
# At Toronto's latitude (~43.6°): 1° lat ≈ 111km, 1° lon ≈ 55.4km
|
| 346 |
-
# For
|
| 347 |
-
lat_range = 0.
|
| 348 |
-
lon_range = 0.
|
| 349 |
|
| 350 |
# 1. Find all stops within the bounding box
|
| 351 |
query_stops = """
|
|
|
|
| 99 |
all_buses = data.get("vehicles", [])
|
| 100 |
route_buses = [v for v in all_buses if v['route'] == route_id]
|
| 101 |
|
| 102 |
+
# Get all stops for this route (regardless of whether there are active vehicles)
|
| 103 |
+
stops_query = """
|
| 104 |
+
SELECT DISTINCT
|
| 105 |
+
s.stop_id,
|
| 106 |
+
s.stop_code,
|
| 107 |
+
s.stop_name,
|
| 108 |
+
s.stop_lat,
|
| 109 |
+
s.stop_lon
|
| 110 |
+
FROM routes r
|
| 111 |
+
JOIN trips t ON r.route_id = t.route_id
|
| 112 |
+
JOIN stop_times st ON CAST(t.trip_id AS VARCHAR) = CAST(st.trip_id AS VARCHAR)
|
| 113 |
+
JOIN stops s ON CAST(st.stop_id AS VARCHAR) = CAST(s.stop_id AS VARCHAR)
|
| 114 |
+
WHERE CAST(r.route_id AS VARCHAR) = ?
|
| 115 |
+
ORDER BY s.stop_name
|
| 116 |
+
"""
|
| 117 |
+
stops_results = db.execute(stops_query, [str(route_id)]).fetchall()
|
| 118 |
+
|
| 119 |
+
stops = [
|
| 120 |
+
{
|
| 121 |
+
"stop_id": str(r[0]),
|
| 122 |
+
"stop_code": str(r[1]) if r[1] else None,
|
| 123 |
+
"stop_name": r[2],
|
| 124 |
+
"location": {
|
| 125 |
+
"lat": r[3],
|
| 126 |
+
"lon": r[4]
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
for r in stops_results
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
# If no active vehicles, return just the stops
|
| 133 |
if not route_buses:
|
| 134 |
+
return {
|
| 135 |
+
"route": route_id,
|
| 136 |
+
"vehicles": [],
|
| 137 |
+
"stops": stops
|
| 138 |
+
}
|
| 139 |
|
| 140 |
# IMPORTANT: Cast Trip IDs to strings to ensure they match the DB
|
| 141 |
trip_ids = [str(v['trip_id']) for v in route_buses]
|
|
|
|
| 192 |
return {
|
| 193 |
"route": route_id,
|
| 194 |
"count": len(enriched),
|
| 195 |
+
"vehicles": enriched,
|
| 196 |
+
"stops": stops
|
| 197 |
}
|
| 198 |
|
| 199 |
@app.get("/api/vehicles/{vehicle_id}")
|
|
|
|
| 377 |
|
| 378 |
@app.get("/api/nearby")
|
| 379 |
async def get_nearby_context(lat: float, lon: float):
|
| 380 |
+
# Bounding box approximation for ~1km radius (fast, no Haversine needed)
|
| 381 |
# At Toronto's latitude (~43.6°): 1° lat ≈ 111km, 1° lon ≈ 55.4km
|
| 382 |
+
# For 1km: lat_range = 0.009°, lon_range ≈ 0.0144°
|
| 383 |
+
lat_range = 0.009 # ~1km in latitude (constant globally)
|
| 384 |
+
lon_range = 0.0144 # ~1km in longitude at Toronto's latitude
|
| 385 |
|
| 386 |
# 1. Find all stops within the bounding box
|
| 387 |
query_stops = """
|