2026-04-12-1

This commit is contained in:
Hamza-Ayed
2026-04-12 22:35:38 +03:00
parent e3799c422c
commit 5ebd7ea3b1
84 changed files with 532 additions and 5136 deletions
+34 -8
View File
@@ -10,14 +10,40 @@ export class MapsController {
constructor(private readonly mapsService: MapsService) {}
@Get('route')
@ApiOperation({ summary: 'Calculate a route between two points 🚗' })
async getRoute(
@Query('fromLat') fromLat: number,
@Query('fromLng') fromLng: number,
@Query('toLat') toLat: number,
@Query('toLng') toLng: number,
) {
return this.mapsService.getRoute([fromLat, fromLng], [toLat, toLng]);
@ApiOperation({ summary: 'Calculate a route with dynamic waypoints 🚗' })
async getRoute(@Query() query: any) {
const waypoints: [number, number][] = [];
// 1. Extract Origin (fromLat, fromLng)
if (query.fromLat && query.fromLng) {
waypoints.push([parseFloat(query.fromLat), parseFloat(query.fromLng)]);
}
// 2. Extract Intermediate Stops (stop1Lat, stop1Lng, stop2Lat, etc.)
// We sort keys to ensure stops are in order: stop1, stop2, stop3...
const stopKeys = Object.keys(query)
.filter(key => key.startsWith('stop') && key.endsWith('Lat'))
.sort((a, b) => {
const numA = parseInt(a.replace('stop', '').replace('Lat', ''), 10);
const numB = parseInt(b.replace('stop', '').replace('Lat', ''), 10);
return numA - numB;
});
for (const latKey of stopKeys) {
const prefix = latKey.replace('Lat', '');
const lngKey = `${prefix}Lng`;
if (query[latKey] && query[lngKey]) {
waypoints.push([parseFloat(query[latKey]), parseFloat(query[lngKey])]);
}
}
// 3. Extract Destination (toLat, toLng)
if (query.toLat && query.toLng) {
waypoints.push([parseFloat(query.toLat), parseFloat(query.toLng)]);
}
const profile = query.profile || 'car';
return this.mapsService.getRoute(waypoints, profile);
}
@Get('config')
+12 -5
View File
@@ -10,18 +10,25 @@ export class MapsService {
this.graphHopperUrl = this.configService.get('GRAPH_HOPPER_URL', 'http://routing:8080');
}
async getRoute(from: [number, number], to: [number, number]) {
async getRoute(waypoints: [number, number][], profile: string = 'car') {
if (waypoints.length < 2) {
throw new HttpException('At least two waypoints are required', HttpStatus.BAD_REQUEST);
}
try {
// GraphHopper expects [lng, lat] order
const ghPoints = waypoints.map(wp => [wp[1], wp[0]]);
const payload: any = {
points: [[from[1], from[0]], [to[1], to[0]]],
profile: 'car',
points: ghPoints,
profile: profile,
locale: 'en',
calc_points: true,
points_encoded: true,
};
console.log(`Routing Request: ${from} -> ${to} via ${this.graphHopperUrl}`);
const response = await axios.post(`${this.graphHopperUrl}/route`, payload, { timeout: 5000 });
console.log(`Routing Request: ${waypoints.length} points via ${profile} on ${this.graphHopperUrl}`);
const response = await axios.post(`${this.graphHopperUrl}/route`, payload, { timeout: 10000 });
console.log('Routing SUCCESS');
const route = response.data.paths[0];
@@ -0,0 +1,23 @@
import { Entity, Column, PrimaryColumn, Index, CreateDateColumn } from 'typeorm';
@Entity('road_speed_profiles')
@Index(['segmentId', 'hourOfDay', 'dayOfWeek'], { unique: true })
export class RoadSpeedProfile {
@PrimaryColumn()
segmentId: string;
@PrimaryColumn()
hourOfDay: number; // 0-23
@PrimaryColumn()
dayOfWeek: number; // 0-6 (0 = Sunday)
@Column('float', { default: 0 })
averageSpeed: number;
@Column('int', { default: 0 })
sampleCount: number;
@CreateDateColumn()
lastUpdated: Date;
}