1
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { Controller, Get, Query } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation } from '@nestjs/swagger';
|
||||
import { MapsService } from './maps.service';
|
||||
|
||||
@ApiTags('maps')
|
||||
@Controller('maps')
|
||||
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]);
|
||||
}
|
||||
|
||||
@Get('config')
|
||||
@ApiOperation({ summary: 'Get map configuration for the region 🗺️' })
|
||||
async getConfig() {
|
||||
return this.mapsService.getMapConfig();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { MapsService } from './maps.service';
|
||||
import { MapsController } from './maps.controller';
|
||||
import { RoadSegmentStat } from './road-stat.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([RoadSegmentStat])],
|
||||
controllers: [MapsController],
|
||||
providers: [MapsService],
|
||||
exports: [MapsService],
|
||||
})
|
||||
export class MapsModule {}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import axios from 'axios';
|
||||
|
||||
@Injectable()
|
||||
export class MapsService {
|
||||
private readonly graphHopperUrl: string;
|
||||
|
||||
constructor(private configService: ConfigService) {
|
||||
this.graphHopperUrl = this.configService.get('GRAPH_HOPPER_URL', 'http://map-routing:8080');
|
||||
}
|
||||
|
||||
async getRoute(from: [number, number], to: [number, number]) {
|
||||
// Proxy routing request to GraphHopper
|
||||
// تحويل طلب حساب المسارم إلى GraphHopper
|
||||
try {
|
||||
const response = await axios.get(`${this.graphHopperUrl}/route`, {
|
||||
params: {
|
||||
point: [`${from[0]},${from[1]}`, `${to[0]},${to[1]}`],
|
||||
profile: 'car',
|
||||
locale: 'en',
|
||||
calc_points: true,
|
||||
points_encoded: false,
|
||||
elevation: false,
|
||||
type: 'json',
|
||||
},
|
||||
paramsSerializer: (params) => {
|
||||
const searchParams = new URLSearchParams();
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach(v => searchParams.append(key, v));
|
||||
} else {
|
||||
searchParams.append(key, value as any);
|
||||
}
|
||||
});
|
||||
return searchParams.toString();
|
||||
},
|
||||
});
|
||||
|
||||
console.log('Routing Successful:', response.request.path);
|
||||
|
||||
const route = response.data.paths[0];
|
||||
if (!route) {
|
||||
throw new HttpException('No route found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
return {
|
||||
distance: route.distance, // In meters
|
||||
duration: route.time / 1000, // In seconds
|
||||
points: route.points.coordinates, // Array of [lng, lat]
|
||||
bbox: route.bbox,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Routing Error:', error.message);
|
||||
if (error.response) {
|
||||
console.error('GraphHopper Response:', error.response.data);
|
||||
}
|
||||
throw new HttpException('Error calculating route', HttpStatus.BAD_GATEWAY);
|
||||
}
|
||||
}
|
||||
|
||||
async getMapConfig() {
|
||||
// Return basic map configuration for clients
|
||||
// إرجاع إعدادات الخريطة للواجهة الأمامية
|
||||
return {
|
||||
center: [31.95, 35.91], // Amman, Jordan
|
||||
zoom: 12,
|
||||
tileServerUrl: this.configService.get('TILE_SERVER_URL', 'http://localhost:3001'),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Entity, Column, PrimaryColumn, Index } from 'typeorm';
|
||||
|
||||
@Entity('road_segment_stats')
|
||||
export class RoadSegmentStat {
|
||||
@PrimaryColumn()
|
||||
segmentId: string; // OSM Way ID or similar
|
||||
|
||||
@Column('float', { default: 0 })
|
||||
averageSpeed: number;
|
||||
|
||||
@Column('float', { default: 1.0 })
|
||||
congestionFactor: number; // 1.0 = Free flow, >1.0 = Congested
|
||||
|
||||
@Column('int', { default: 0 })
|
||||
sampleCount: number;
|
||||
|
||||
@Column({
|
||||
type: 'geography',
|
||||
spatialFeatureType: 'LineString',
|
||||
srid: 4326,
|
||||
nullable: true,
|
||||
})
|
||||
@Index({ spatial: true })
|
||||
geometry: any;
|
||||
|
||||
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
|
||||
lastUpdated: Date;
|
||||
}
|
||||
Reference in New Issue
Block a user