-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
217 lines (179 loc) · 6.84 KB
/
main.py
File metadata and controls
217 lines (179 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from fastapi import FastAPI, Request, Form, Query
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from typing import Optional
import random
import math
import httpx
app = FastAPI(title="Running Router")
# Mount static files
app.mount("/static", StaticFiles(directory="static"), name="static")
# Templates
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
"""Serve the main form page"""
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/api/geocode")
async def geocode_search(q: str = Query(..., min_length=1)):
"""
Proxy endpoint for Nominatim geocoding API.
Searches for locations based on query string.
"""
if len(q) < 3:
return JSONResponse(content={"results": []})
# Nominatim API endpoint
nominatim_url = "https://nominatim.openstreetmap.org/search"
# Parameters for the request
params = {
# Force searches to Sydney area
"q": "Sydney%20" + q,
"format": "json",
"limit": 5,
"addressdetails": 1
}
# Headers required by Nominatim usage policy
headers = {
"User-Agent": "RunningRouter/1.0"
}
try:
async with httpx.AsyncClient() as client:
response = await client.get(
nominatim_url,
params=params,
headers=headers,
timeout=10.0
)
print(response)
if response.status_code == 200:
data = response.json()
# Format results for frontend
results = []
for item in data:
results.append({
"display_name": item.get("display_name", ""),
"lat": item.get("lat", ""),
"lon": item.get("lon", ""),
"place_id": item.get("place_id", ""),
"type": item.get("type", ""),
"address": item.get("address", {})
})
return JSONResponse(content={"results": results})
else:
return JSONResponse(
content={"error": "Geocoding service unavailable"},
status_code=response.status_code
)
except httpx.TimeoutException:
return JSONResponse(
content={"error": "Request timeout"},
status_code=504
)
except Exception as e:
return JSONResponse(
content={"error": f"An error occurred: {str(e)}"},
status_code=500
)
@app.post("/plan-route")
async def plan_route(
route_type: str = Form(...),
distance: float = Form(...),
start_location: Optional[str] = Form(None),
end_location: Optional[str] = Form(None),
start_lat: Optional[float] = Form(None),
start_lon: Optional[float] = Form(None),
end_lat: Optional[float] = Form(None),
end_lon: Optional[float] = Form(None)
):
"""
Generate a running route based on user inputs.
Currently returns dummy data - will integrate with OSM and routing engine later.
"""
# Validate inputs
if route_type not in ["circular", "out_and_back"]:
return {"error": "Invalid route type"}
if distance <= 0:
return {"error": "Distance must be positive"}
# Generate dummy route data (will use coordinates when routing engine is integrated)
route_data = generate_dummy_route(
route_type, distance, start_location, end_location,
start_lat, start_lon, end_lat, end_lon
)
return {
"success": True,
"route_type": route_type,
"distance": distance,
"start_location": start_location,
"end_location": end_location,
"start_coordinates": {"lat": start_lat, "lon": start_lon} if start_lat and start_lon else None,
"end_coordinates": {"lat": end_lat, "lon": end_lon} if end_lat and end_lon else None,
"route": route_data
}
def generate_dummy_route(
route_type: str, distance: float, start_loc: str, end_loc: str,
start_lat: Optional[float] = None, start_lon: Optional[float] = None,
end_lat: Optional[float] = None, end_lon: Optional[float] = None
):
"""
Generate dummy route waypoints.
In production, this will call OpenStreetMaps API and routing engine.
"""
# Use provided coordinates or fall back to dummy coordinates
if start_lat and start_lon:
base_lat = start_lat
base_lon = start_lon
else:
# Dummy coordinates (roughly around San Francisco as example)
base_lat = 37.7749
base_lon = -122.4194
# Calculate number of waypoints based on distance (one per km)
num_waypoints = max(int(distance), 2)
waypoints = []
if route_type == "circular":
# Generate circular route that returns to start
for i in range(num_waypoints + 1):
angle = (2 * math.pi * i) / num_waypoints
# Create circular path
lat_offset = (distance / 111) * 0.5 * math.sin(angle) # 111 km per degree
lon_offset = (distance / 111) * 0.5 * math.cos(angle) / math.cos(math.radians(base_lat))
waypoints.append({
"lat": base_lat + lat_offset,
"lon": base_lon + lon_offset,
"name": f"Waypoint {i+1}" if i < num_waypoints else "Start/End"
})
else:
# Generate out-and-back route
half_distance = distance / 2
# Generate waypoints to the turnaround point
for i in range(num_waypoints // 2 + 1):
progress = i / (num_waypoints // 2)
lat_offset = (half_distance / 111) * progress
lon_offset = (half_distance / 111) * progress * 0.5
waypoints.append({
"lat": base_lat + lat_offset,
"lon": base_lon + lon_offset,
"name": f"Waypoint {i+1}"
})
# Add turnaround point
waypoints[-1]["name"] = "Turnaround Point"
# Return journey (reverse)
for i in range(len(waypoints) - 2, -1, -1):
waypoints.append({
"lat": waypoints[i]["lat"],
"lon": waypoints[i]["lon"],
"name": f"Return {len(waypoints) - i}"
})
return {
"waypoints": waypoints,
"total_distance": distance,
"estimated_time": distance * 6, # Assuming 6 min/km pace
"elevation_gain": random.randint(10, 100) # Dummy elevation
}
@app.get("/results", response_class=HTMLResponse)
async def results(request: Request):
"""Serve the results page"""
return templates.TemplateResponse("results.html", {"request": request})
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)