42Cummer commited on
Commit
64e5831
·
verified ·
1 Parent(s): e780e98

Upload app.py

Browse files
Files changed (1) hide show
  1. src/app.py +43 -0
src/app.py CHANGED
@@ -329,6 +329,49 @@ async def get_alerts_for_route(route_id: str):
329
  "alerts": route_alerts
330
  }
331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332
  if __name__ == "__main__":
333
  import uvicorn # type: ignore
334
  # Start the server
 
329
  "alerts": route_alerts
330
  }
331
 
332
+ @app.get("/api/nearby")
333
+ async def get_nearby_context(lat: float, lon: float):
334
+ # Bounding box approximation for ~500m radius (fast, no Haversine needed)
335
+ # At Toronto's latitude (~43.6°): 1° lat ≈ 111km, 1° lon ≈ 55.4km
336
+ # For 500m: lat_range = 0.0045°, lon_range ≈ 0.0072°
337
+ lat_range = 0.0045 # ~500m in latitude (constant globally)
338
+ lon_range = 0.0072 # ~500m in longitude at Toronto's latitude
339
+
340
+ # 1. Find all stops within the bounding box
341
+ query_stops = """
342
+ SELECT stop_id, stop_code, stop_name, stop_lat, stop_lon
343
+ FROM stops
344
+ WHERE stop_lat BETWEEN ? AND ?
345
+ AND stop_lon BETWEEN ? AND ?
346
+ """
347
+ stops = db.execute(query_stops, [lat - lat_range, lat + lat_range, lon - lon_range, lon + lon_range]).fetchall()
348
+
349
+ stop_ids = [str(s[0]) for s in stops]
350
+ if not stop_ids:
351
+ return {"stops": [], "routes": []}
352
+
353
+ # 2. Find all unique routes serving these specific stops
354
+ placeholders = ','.join(['?'] * len(stop_ids))
355
+ query_routes = f"""
356
+ SELECT DISTINCT r.route_id, r.route_short_name, r.route_long_name, r.route_color
357
+ FROM routes r
358
+ JOIN trips t ON r.route_id = t.route_id
359
+ JOIN stop_times st ON t.trip_id = st.trip_id
360
+ WHERE CAST(st.stop_id AS VARCHAR) IN ({placeholders})
361
+ """
362
+ routes = db.execute(query_routes, stop_ids).fetchall()
363
+
364
+ return {
365
+ "stops": [
366
+ {"id": s[0], "code": s[1], "name": s[2], "lat": s[3], "lon": s[4]}
367
+ for s in stops
368
+ ],
369
+ "routes": [
370
+ {"id": r[0], "short_name": r[1], "long_name": r[2], "color": f"#{r[3]}"}
371
+ for r in routes
372
+ ]
373
+ }
374
+
375
  if __name__ == "__main__":
376
  import uvicorn # type: ignore
377
  # Start the server