-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend-map.js
More file actions
457 lines (370 loc) · 13.6 KB
/
frontend-map.js
File metadata and controls
457 lines (370 loc) · 13.6 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Urban Mobility Risk Engine - Frontend JavaScript
const API_BASE_URL = 'http://localhost:5000';
// Application state
const state = {
map: null,
startMarker: null,
endMarker: null,
routeLayer: null,
heatmapLayer: null,
trafficMarkers: [],
crimeMarkers: [],
startCoords: null,
endCoords: null,
selectingStart: false,
selectingEnd: false
};
document.addEventListener('DOMContentLoaded', () => {
initializeMap();
setupEventListeners();
checkAPIStatus();
});
function initializeMap() {
// Default center (Louisville, KY)
state.map = L.map('map').setView([38.2527, -85.7585], 12);
// OpenStreetMap tiles
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors',
maxZoom: 19
}).addTo(state.map);
// map click handler
state.map.on('click', handleMapClick);
// custom controls
addLegend();
}
// event listeners
function setupEventListeners() {
// Location input focus handlers
document.getElementById('startInput').addEventListener('focus', () => {
state.selectingStart = true;
state.selectingEnd = false;
document.getElementById('startInput').placeholder = 'Click on map to select';
});
document.getElementById('endInput').addEventListener('focus', () => {
state.selectingStart = false;
state.selectingEnd = true;
document.getElementById('endInput').placeholder = 'Click on map to select';
});
// Preference sliders
const sliders = ['speed', 'safety', 'weather', 'crime'];
sliders.forEach(slider => {
const element = document.getElementById(`${slider}Pref`);
element.addEventListener('input', (e) => {
document.getElementById(`${slider}Value`).textContent = `${e.target.value}%`;
});
});
// Action buttons
document.getElementById('calculateBtn').addEventListener('click', calculateRoute);
document.getElementById('clearBtn').addEventListener('click', clearAll);
// View options
document.getElementById('showHeatmap').addEventListener('change', toggleHeatmap);
document.getElementById('showTraffic').addEventListener('change', toggleTraffic);
document.getElementById('showCrime').addEventListener('change', toggleCrime);
}
// map clicks
function handleMapClick(e) {
const { lat, lng } = e.latlng;
if (state.selectingStart) {
setStartLocation(lat, lng);
state.selectingStart = false;
document.getElementById('startInput').placeholder = 'Enter start address or click map';
} else if (state.selectingEnd) {
setEndLocation(lat, lng);
state.selectingEnd = false;
document.getElementById('endInput').placeholder = 'Enter end address or click map';
}
}
// start location
function setStartLocation(lat, lon) {
state.startCoords = { lat, lon };
// remove existing marker
if (state.startMarker) {
state.map.removeLayer(state.startMarker);
}
// Add new marker
state.startMarker = L.marker([lat, lon], {
icon: createCustomIcon('green')
}).addTo(state.map);
// Update UI
document.getElementById('startCoords').textContent = `${lat.toFixed(4)}, ${lon.toFixed(4)}`;
document.getElementById('startInput').value = `${lat.toFixed(4)}, ${lon.toFixed(4)}`;
}
// Set end location
function setEndLocation(lat, lon) {
state.endCoords = { lat, lon };
// Remove existing marker
if (state.endMarker) {
state.map.removeLayer(state.endMarker);
}
// Add new marker
state.endMarker = L.marker([lat, lon], {
icon: createCustomIcon('red')
}).addTo(state.map);
// Update UI
document.getElementById('endCoords').textContent = `${lat.toFixed(4)}, ${lon.toFixed(4)}`;
document.getElementById('endInput').value = `${lat.toFixed(4)}, ${lon.toFixed(4)}`;
}
function createCustomIcon(color) {
return L.divIcon({
className: 'custom-marker',
html: `<div style="
background: ${color};
width: 30px;
height: 30px;
border-radius: 50%;
border: 3px solid white;
box-shadow: 0 2px 5px rgba(0,0,0,0.3);
"></div>`,
iconSize: [30, 30],
iconAnchor: [15, 15]
});
}
// Calculate route
async function calculateRoute() {
if (!state.startCoords || !state.endCoords) {
alert('Please select both start and end locations');
return;
}
document.getElementById('loadingOverlay').style.display = 'flex';
try {
// Get preferences
const preferences = {
speed: parseFloat(document.getElementById('speedPref').value) / 100,
safety: parseFloat(document.getElementById('safetyPref').value) / 100,
weather: parseFloat(document.getElementById('weatherPref').value) / 100,
crime: parseFloat(document.getElementById('crimePref').value) / 100
};
// Make API request
const response = await fetch(`${API_BASE_URL}/api/route`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
start: state.startCoords,
end: state.endCoords,
preferences: preferences
})
});
if (!response.ok) {
throw new Error('Failed to calculate route');
}
const data = await response.json();
displayRoute(data);
updateResults(data);
document.getElementById('resultsPanel').style.display = 'block';
} catch (error) {
console.error('Error calculating route:', error);
alert('Failed to calculate route. Please try again.');
} finally {
document.getElementById('loadingOverlay').style.display = 'none';
}
}
function displayRoute(data) {
if (state.routeLayer) {
state.map.removeLayer(state.routeLayer);
}
const routeCoords = data.route.map(point => [point[0], point[1]]);
const color = getRiskColor(data.risk_score);
state.routeLayer = L.polyline(routeCoords, {
color: color,
weight: 6,
opacity: 0.7
}).addTo(state.map);
state.map.fitBounds(state.routeLayer.getBounds(), { padding: [50, 50] });
addRiskMarkers(data);
}
// Update results panel
function updateResults(data) {
// Overall risk score
document.getElementById('overallRisk').textContent = data.risk_score.toFixed(1);
document.getElementById('riskLevel').textContent = data.risk_breakdown.risk_level + ' Risk';
// Change color based on risk level
const scoreElement = document.getElementById('overallRisk');
scoreElement.style.color = getRiskColor(data.risk_score);
// Route metrics
document.getElementById('distanceValue').textContent = `${data.distance} km`;
document.getElementById('timeValue').textContent = `${data.estimated_time} min`;
// Risk breakdown
updateRiskBar('traffic', data.risk_breakdown.traffic_risk);
updateRiskBar('crime', data.risk_breakdown.crime_risk);
updateRiskBar('weather', data.risk_breakdown.weather_risk);
document.getElementById('trafficRisk').textContent = data.risk_breakdown.traffic_risk.toFixed(1);
document.getElementById('crimeRiskValue').textContent = data.risk_breakdown.crime_risk.toFixed(1);
document.getElementById('weatherRiskValue').textContent = data.risk_breakdown.weather_risk.toFixed(1);
// Warnings
displayWarnings(data.warnings);
}
// Update risk bar
function updateRiskBar(type, value) {
const bar = document.getElementById(`${type}Bar`);
bar.style.width = `${value}%`;
}
// Display warnings
function displayWarnings(warnings) {
const container = document.getElementById('warningsContainer');
if (warnings.length === 0) {
container.innerHTML = '<div style="color: #10b981; font-weight: 500;">✓ No warnings for this route</div>';
return;
}
container.innerHTML = warnings.map(warning => `
<div class="warning-item">${warning}</div>
`).join('');
}
// Get color based on risk score
function getRiskColor(score) {
if (score >= 75) return '#ef4444'; // Red
if (score >= 50) return '#f59e0b'; // Orange
if (score >= 30) return '#eab308'; // Yellow
return '#10b981'; // Green
}
function addRiskMarkers(data) {
const route = data.route;
const segmentSize = Math.floor(route.length / 5);
for (let i = 1; i < 5; i++) {
const point = route[i * segmentSize];
const marker = L.circleMarker([point[0], point[1]], {
radius: 5,
fillColor: getRiskColor(data.risk_score),
color: '#fff',
weight: 2,
opacity: 1,
fillOpacity: 0.8
}).addTo(state.map);
marker.bindPopup(`
<div style="padding: 10px;">
<strong>Segment Risk: ${data.risk_score.toFixed(1)}</strong><br>
<small>Traffic: ${data.risk_breakdown.traffic_risk.toFixed(1)}</small><br>
<small>Crime: ${data.risk_breakdown.crime_risk.toFixed(1)}</small><br>
<small>Weather: ${data.risk_breakdown.weather_risk.toFixed(1)}</small>
</div>
`);
}
}
// Toggle heatmap
async function toggleHeatmap(e) {
if (e.target.checked) {
await loadHeatmap();
} else {
if (state.heatmapLayer) {
state.map.removeLayer(state.heatmapLayer);
}
}
}
// Load and display risk heatmap
async function loadHeatmap() {
try {
const center = state.map.getCenter();
const response = await fetch(`${API_BASE_URL}/api/risk-heatmap`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
center: { lat: center.lat, lon: center.lng },
radius_km: 5,
grid_size: 20
})
});
const data = await response.json();
// Convert to heatmap format
const heatData = data.data.map(point => [
point.lat,
point.lon,
point.risk / 100 // Normalize to 0-1
]);
// Remove existing heatmap
if (state.heatmapLayer) {
state.map.removeLayer(state.heatmapLayer);
}
// Add new heatmap
state.heatmapLayer = L.heatLayer(heatData, {
radius: 25,
blur: 15,
maxZoom: 17,
max: 1.0,
gradient: {
0.0: '#10b981',
0.3: '#eab308',
0.5: '#f59e0b',
0.7: '#ef4444',
1.0: '#dc2626'
}
}).addTo(state.map);
} catch (error) {
console.error('Error loading heatmap:', error);
}
}
// Toggle traffic display
function toggleTraffic(e) {
// In production, this would show/hide real traffic data
console.log('Traffic toggle:', e.target.checked);
}
// Toggle crime display
function toggleCrime(e) {
// In production, this would show/hide crime incidents
console.log('Crime toggle:', e.target.checked);
}
// Clear all
function clearAll() {
// Remove markers
if (state.startMarker) state.map.removeLayer(state.startMarker);
if (state.endMarker) state.map.removeLayer(state.endMarker);
if (state.routeLayer) state.map.removeLayer(state.routeLayer);
if (state.heatmapLayer) state.map.removeLayer(state.heatmapLayer);
// Reset state
state.startCoords = null;
state.endCoords = null;
state.startMarker = null;
state.endMarker = null;
state.routeLayer = null;
// Clear UI
document.getElementById('startInput').value = '';
document.getElementById('endInput').value = '';
document.getElementById('startCoords').textContent = '—';
document.getElementById('endCoords').textContent = '—';
// Hide results
document.getElementById('resultsPanel').style.display = 'none';
// Reset map view
state.map.setView([38.2527, -85.7585], 12);
}
// legend to map
function addLegend() {
const legend = L.control({ position: 'bottomright' });
legend.onAdd = function(map) {
const div = L.DomUtil.create('div', 'legend');
div.style.cssText = `
background: white;
padding: 10px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
`;
div.innerHTML = `
<div style="font-weight: bold; margin-bottom: 5px;">Risk Level</div>
<div><span style="color: #10b981;">●</span> Low (0-30)</div>
<div><span style="color: #eab308;">●</span> Moderate (30-50)</div>
<div><span style="color: #f59e0b;">●</span> High (50-75)</div>
<div><span style="color: #ef4444;">●</span> Very High (75+)</div>
`;
return div;
};
legend.addTo(state.map);
}
// Check API status
async function checkAPIStatus() {
try {
const response = await fetch(`${API_BASE_URL}/health`);
const data = await response.json();
if (data.status === 'healthy') {
console.log('API is online');
}
} catch (error) {
console.warn('API is not available. Using demo mode.');
document.getElementById('statusBadge').innerHTML =
'<i class="fas fa-circle" style="color: #f59e0b;"></i> Demo Mode';
}
}
// Utility function to format numbers
function formatNumber(num, decimals = 2) {
return num.toFixed(decimals);
}