feat: implement geocoding evaluation scripts and enhance routing service with traffic-aware path processing

This commit is contained in:
Hamza-Ayed
2026-07-16 14:31:07 +03:00
parent 3cf87999f4
commit 3773ab56a4
20 changed files with 885 additions and 137 deletions
+64 -16
View File
@@ -66,10 +66,10 @@ export class MapsService {
const payload: any = {
points: ghPoints,
profile: profile,
locale: locale,
locale: locale === 'en' ? 'ar' : locale, // Default to Arabic if not specified or fallback
calc_points: true,
points_encoded: true,
instructions: steps,
instructions: steps || true, // Always request instructions to extract route name
};
// ── Closure-Aware Routing ─────────────────────────────────────────────
@@ -164,25 +164,47 @@ export class MapsService {
const baseDuration = route.time / 1000;
const trafficAwareDuration = baseDuration * trafficFactor;
// Process alternative routes if any without breaking existing frontend variables
const altRoutes = paths.slice(1).map(alt => ({
distance: alt.distance,
duration: Math.round(alt.time / 1000),
points: alt.points,
bbox: alt.bbox,
instructions: alt.instructions // Include instructions for alternatives if requested
}));
// Process all paths to add metadata (Names, Tags)
const processedPaths = paths.map((p: any, index: number) => {
const pCoords = this.decodePolyline(p.points);
const pTrafficFactor = this.trafficGrid.getTrafficFactor(pCoords, hr, dow);
const pDuration = Math.round((p.time / 1000) * pTrafficFactor);
const routeName = this.getRouteName(p.instructions);
// Tags assignment
const tags: string[] = [];
if (index === 0) tags.push('FASTEST');
if (paths.length > 1) {
const isShortest = paths.every((other: any) => p.distance <= other.distance);
if (isShortest) tags.push('SHORTEST');
if (index > 0 && !isShortest) tags.push('ALTERNATIVE');
}
return {
routeName: routeName ? `عبر ${routeName}` : `المسار ${index + 1}`,
tags,
distance: p.distance,
duration: pDuration,
points: p.points,
bbox: p.bbox,
instructions: steps ? p.instructions : undefined
};
});
const mainRoute = processedPaths[0];
const altRoutes = processedPaths.slice(1);
return {
distance: route.distance,
duration: Math.round(baseDuration),
trafficAwareDuration: Math.round(trafficAwareDuration),
routeName: mainRoute.routeName,
tags: mainRoute.tags,
distance: mainRoute.distance,
duration: mainRoute.duration,
trafficFactor: Math.round(trafficFactor * 100) / 100,
startName,
endName,
points: route.points,
bbox: route.bbox,
instructions: route.instructions, // Added: turn-by-turn maneuvers
points: mainRoute.points,
bbox: mainRoute.bbox,
instructions: mainRoute.instructions,
alternatives: altRoutes
};
} catch (error) {
@@ -192,6 +214,32 @@ export class MapsService {
}
}
/**
* Extract the most significant street name from instructions to name the route.
*/
private getRouteName(instructions: any[]): string | null {
if (!instructions || instructions.length === 0) return null;
const streetDistances: Record<string, number> = {};
for (const inst of instructions) {
if (inst.street_name && inst.street_name.trim() !== '') {
streetDistances[inst.street_name] = (streetDistances[inst.street_name] || 0) + (inst.distance || 0);
}
}
let longestStreet: string | null = null;
let maxDist = 0;
for (const [street, dist] of Object.entries(streetDistances)) {
if (dist > maxDist) {
maxDist = dist;
longestStreet = street;
}
}
return longestStreet;
}
/**
* Manual decoder for Google Polyline algorithm (Server-side spatial matching)
*/