extract carpool informations from geography module

This commit is contained in:
sbriat
2023-09-07 14:30:07 +02:00
parent 57fe8d417f
commit d1a314f011
28 changed files with 586 additions and 611 deletions

View File

@@ -2,12 +2,7 @@ import { Inject, Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { GeorouterPort } from '../core/application/ports/georouter.port';
import { GeorouterSettings } from '../core/application/types/georouter-settings.type';
import {
Path,
PathType,
Route,
SpacetimePoint,
} from '../core/domain/route.types';
import { Route, Step, Waypoint } from '../core/domain/route.types';
import { DefaultParamsProviderPort } from '../core/application/ports/default-params-provider.port';
import { GEODESIC, PARAMS_PROVIDER } from '../geography.di-tokens';
import { catchError, lastValueFrom, map } from 'rxjs';
@@ -35,13 +30,13 @@ export class GraphhopperGeorouter implements GeorouterPort {
].join('');
}
routes = async (
paths: Path[],
route = async (
waypoints: Waypoint[],
settings: GeorouterSettings,
): Promise<Route[]> => {
): Promise<Route> => {
this._setDefaultUrlArgs();
this._setSettings(settings);
return this._getRoutes(paths);
return this._getRoute(waypoints);
};
private _setDefaultUrlArgs = (): void => {
@@ -62,46 +57,39 @@ export class GraphhopperGeorouter implements GeorouterPort {
}
};
private _getRoutes = async (paths: Path[]): Promise<Route[]> => {
const routes = Promise.all(
paths.map(async (path) => {
const url: string = [
this.getUrl(),
'&point=',
path.points
.map((point) => [point.lat, point.lon].join('%2C'))
.join('&point='),
].join('');
return await lastValueFrom(
this.httpService.get(url).pipe(
map((response) => {
if (response.data) return this.createRoute(response, path.type);
throw new Error();
}),
catchError((error: AxiosError) => {
if (error.code == AxiosError.ERR_BAD_REQUEST) {
throw new RouteNotFoundException(
error,
'No route found for given coordinates',
);
}
throw new GeorouterUnavailableException(error);
}),
),
);
}),
private _getRoute = async (waypoints: Waypoint[]): Promise<Route> => {
const url: string = [
this.getUrl(),
'&point=',
waypoints
.map((waypoint: Waypoint) => [waypoint.lat, waypoint.lon].join('%2C'))
.join('&point='),
].join('');
return await lastValueFrom(
this.httpService.get(url).pipe(
map((response) => {
if (response.data) return this.createRoute(response);
throw new Error();
}),
catchError((error: AxiosError) => {
if (error.code == AxiosError.ERR_BAD_REQUEST) {
throw new RouteNotFoundException(
error,
'No route found for given coordinates',
);
}
throw new GeorouterUnavailableException(error);
}),
),
);
return routes;
};
private getUrl = (): string => [this.url, this.urlArgs.join('&')].join('');
private createRoute = (
response: AxiosResponse<GraphhopperResponse>,
type: PathType,
): Route => {
const route = {} as Route;
route.type = type;
if (response.data.paths && response.data.paths[0]) {
const shortestPath = response.data.paths[0];
route.distance = shortestPath.distance ?? 0;
@@ -135,7 +123,7 @@ export class GraphhopperGeorouter implements GeorouterPort {
let instructions: GraphhopperInstruction[] = [];
if (shortestPath.instructions)
instructions = shortestPath.instructions;
route.spacetimeWaypoints = this.generateSpacetimePoints(
route.steps = this.generateSteps(
shortestPath.points.coordinates,
shortestPath.snapped_waypoints.coordinates,
shortestPath.details.time,
@@ -147,12 +135,12 @@ export class GraphhopperGeorouter implements GeorouterPort {
return route;
};
private generateSpacetimePoints = (
private generateSteps = (
points: [[number, number]],
snappedWaypoints: [[number, number]],
durations: [[number, number, number]],
instructions: GraphhopperInstruction[],
): SpacetimePoint[] => {
): Step[] => {
const indices = this.getIndices(points, snappedWaypoints);
const times = this.getTimes(durations, indices);
const distances = this.getDistances(instructions, indices);

View File

@@ -1,16 +1,16 @@
import { DirectionEncoderPort } from '../core/application/ports/direction-encoder.port';
import { Injectable } from '@nestjs/common';
import { Coordinates } from '../core/domain/route.types';
import { Point } from '../core/domain/route.types';
@Injectable()
export class PostgresDirectionEncoder implements DirectionEncoderPort {
encode = (coordinates: Coordinates[]): string =>
encode = (coordinates: Point[]): string =>
[
"'LINESTRING(",
coordinates.map((point) => [point.lon, point.lat].join(' ')).join(),
")'",
].join('');
decode = (direction: string): Coordinates[] =>
decode = (direction: string): Point[] =>
direction
.split('(')[1]
.split(')')[0]