Spaces:
Configuration error
Configuration error
File size: 4,663 Bytes
fa8a0ea | 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 | import { getCached, setCache, generateRouteCacheKey, generateGeocodeCacheKey } from './routeCache';
export async function geocode(query: string) {
const cacheKey = generateGeocodeCacheKey(query);
const cached = getCached<any[]>(cacheKey);
if (cached && cached.length > 0) return cached;
try {
// Use server proxy for geocoding (bypasses CORS)
const response = await fetch(`/api/geocode?q=${encodeURIComponent(query)}`);
if (response.ok) {
const data = await response.json();
if (data && data.length > 0) {
const results = data.map((item: any) => ({
id: item.place_id || Math.random(),
place_name: item.display_name || item.place_name,
center: [parseFloat(item.lon || item.center?.[0] || 0), parseFloat(item.lat || item.center?.[1] || 0)]
}));
setCache(cacheKey, results);
return results;
}
}
} catch (error) {
console.error('Geocode error:', error);
}
return [];
}
export async function getRoute(start: [number, number], end: [number, number], profile: string = 'driving') {
const cacheKey = generateRouteCacheKey(start, end, profile);
const cached = getCached<any[]>(cacheKey);
if (cached && cached.length > 0) return cached;
try {
const response = await fetch(
`https://router.project-osrm.org/route/v1/driving/${start[0]},${start[1]};${end[0]},${end[1]}?geometries=geojson&overview=full&steps=true&alternatives=true`,
{ signal: AbortSignal.timeout(10000) }
);
if (response.ok) {
const data = await response.json();
if (data.routes && data.routes.length > 0) {
const routes = data.routes;
if (profile === 'cycling' || profile === 'walking') {
const speedMs = profile === 'cycling' ? 4.16 : 1.38;
routes.forEach((route: any) => {
route.duration = route.distance / speedMs;
});
}
setCache(cacheKey, routes);
return routes;
}
}
} catch (error) {
console.error('Route error:', error);
}
return generateMockRoute(start, end);
}
function generateMockRoute(start: [number, number], end: [number, number]) {
const distance = Math.sqrt(
Math.pow(end[0] - start[0], 2) + Math.pow(end[1] - start[1], 2)
) * 111;
const coords: [number, number][] = [];
const steps = 20;
for (let i = 0; i <= steps; i++) {
const t = i / steps;
coords.push([
start[0] + (end[0] - start[0]) * t,
start[1] + (end[1] - start[1]) * t
]);
}
return [{
geometry: { coordinates: coords },
distance: distance * 1000,
duration: (distance / 40) * 3600,
legs: [{
steps: [
{ maneuver: { type: 'depart', instruction: 'Start from your location' }, distance: 500 },
{ maneuver: { type: 'continue', instruction: 'Continue straight' }, distance: distance * 400 },
{ maneuver: { type: 'arrive', instruction: 'Arrive at your destination' }, distance: 500 }
]
}]
}];
}
export async function getWeather(lat: number, lon: number) {
try {
const response = await fetch(`/api/weather?lat=${lat}&lon=${lon}`);
if (response.ok) {
return await response.json();
}
} catch {}
return { weather: [{ main: 'Clear' }], main: { temp: 298 } };
}
export async function analyzeRoute(coordinates: [number, number][], weatherCondition: string, timeOfDay: number, vehicleType: string = 'driving') {
try {
const response = await fetch('/api/route-analysis', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ coordinates, weather_condition: weatherCondition, time_of_day: timeOfDay, vehicle_type: vehicleType })
});
if (response.ok) {
return await response.json();
}
} catch {}
const segments = [];
const segmentSize = Math.max(1, Math.floor(coordinates.length / 10));
for (let i = 0; i < coordinates.length - 1; i += segmentSize) {
const endIdx = Math.min(i + segmentSize, coordinates.length - 1);
const baseRisk = 0.15 + Math.random() * 0.25;
const weatherMultiplier = weatherCondition === 'Clear' ? 1 : weatherCondition === 'Rain' ? 1.4 : 1.2;
const nightMultiplier = (timeOfDay >= 22 || timeOfDay <= 5) ? 1.25 : 1;
segments.push({
start: coordinates[i],
end: coordinates[endIdx],
risk_probability: Math.min(0.85, baseRisk * weatherMultiplier * nightMultiplier),
traffic_level: 0.25 + Math.random() * 0.4
});
}
return {
segments,
incidents: [],
overallRisk: segments.length > 0 ? segments.reduce((a, b) => a + (b.risk_probability || 0), 0) / segments.length : 0.25
};
}
|