add graphhopper georouter

This commit is contained in:
sbriat
2023-08-23 11:35:48 +02:00
parent a6836b168c
commit 158b12b150
19 changed files with 253 additions and 71 deletions

View File

@@ -1,4 +1,4 @@
import { Coordinates } from '../types/coordinates.type';
import { Coordinates } from '../../domain/route.types';
export interface DirectionEncoderPort {
encode(coordinates: Coordinates[]): string;

View File

@@ -1,5 +0,0 @@
import { GeorouterPort } from './georouter.port';
export interface GeorouterCreatorPort {
create(type: string, url: string): GeorouterPort;
}

View File

@@ -1,6 +1,5 @@
import { QueryBase } from '@mobicoop/ddd-library';
import { Role } from '@modules/geography/core/domain/route.types';
import { Waypoint } from '../../types/waypoint.type';
import { Role, Waypoint } from '@modules/geography/core/domain/route.types';
import { GeorouterSettings } from '../../types/georouter-settings.type';
export class GetRouteQuery extends QueryBase {

View File

@@ -1,4 +0,0 @@
export type Coordinates = {
lon: number;
lat: number;
};

View File

@@ -1,7 +0,0 @@
import { PathType } from '../../domain/route.types';
import { Coordinates } from './coordinates.type';
export type Path = {
type: PathType;
points: Coordinates[];
};

View File

@@ -1,13 +0,0 @@
import { PathType } from '../../domain/route.types';
import { Coordinates } from './coordinates.type';
import { SpacetimePoint } from './spacetime-point.type';
export type Route = {
type: PathType;
distance: number;
duration: number;
fwdAzimuth: number;
backAzimuth: number;
distanceAzimuth: number;
points: Coordinates[] | SpacetimePoint[];
};

View File

@@ -1,6 +0,0 @@
import { Coordinates } from './coordinates.type';
export type SpacetimePoint = Coordinates & {
duration: number;
distance: number;
};

View File

@@ -1,5 +0,0 @@
import { Coordinates } from './coordinates.type';
export type Waypoint = Coordinates & {
position: number;
};

View File

@@ -5,10 +5,9 @@ import {
Role,
RouteProps,
PathType,
Direction,
Route,
} from './route.types';
import { WaypointProps } from './value-objects/waypoint.value-object';
import { Route } from '../application/types/route.type';
import { v4 } from 'uuid';
import { RouteNotFoundException } from './route.errors';
@@ -16,33 +15,30 @@ export class RouteEntity extends AggregateRoot<RouteProps> {
protected readonly _id: AggregateID;
static create = async (create: CreateRouteProps): Promise<RouteEntity> => {
let directions: Direction[];
let routes: Route[];
try {
directions = await create.georouter.routes(
routes = await create.georouter.routes(
this.getPaths(create.roles, create.waypoints),
create.georouterSettings,
);
if (!directions || directions.length == 0)
throw new RouteNotFoundException();
if (!routes || routes.length == 0) throw new RouteNotFoundException();
} catch (e: any) {
throw e;
}
let driverRoute: Route;
let passengerRoute: Route;
if (directions.some((route: Route) => route.type == PathType.GENERIC)) {
driverRoute = passengerRoute = directions.find(
if (routes.some((route: Route) => route.type == PathType.GENERIC)) {
driverRoute = passengerRoute = routes.find(
(route: Route) => route.type == PathType.GENERIC,
);
} else {
driverRoute = directions.some(
(route: Route) => route.type == PathType.DRIVER,
)
? directions.find((route: Route) => route.type == PathType.DRIVER)
driverRoute = routes.some((route: Route) => route.type == PathType.DRIVER)
? routes.find((route: Route) => route.type == PathType.DRIVER)
: undefined;
passengerRoute = directions.some(
passengerRoute = routes.some(
(route: Route) => route.type == PathType.PASSENGER,
)
? directions.find((route: Route) => route.type == PathType.PASSENGER)
? routes.find((route: Route) => route.type == PathType.PASSENGER)
: undefined;
}
const routeProps: RouteProps = {

View File

@@ -9,3 +9,13 @@ export class RouteNotFoundException extends ExceptionBase {
super(RouteNotFoundException.message, cause, metadata);
}
}
export class GeorouterUnavailableException extends ExceptionBase {
static readonly message = 'Georouter unavailable';
public readonly code = 'GEOROUTER.UNAVAILABLE';
constructor(cause?: Error, metadata?: unknown) {
super(GeorouterUnavailableException.message, cause, metadata);
}
}

View File

@@ -25,27 +25,31 @@ export interface CreateRouteProps {
georouterSettings: GeorouterSettings;
}
export type Direction = {
export type Route = {
type: PathType;
distance: number;
duration: number;
fwdAzimuth: number;
backAzimuth: number;
distanceAzimuth: number;
points: SpacetimePoint[] | Point[];
points: SpacetimePoint[] | Coordinates[];
};
export type Path = {
type: PathType;
points: Point[];
points: Coordinates[];
};
export type Point = {
export type Coordinates = {
lon: number;
lat: number;
};
export type SpacetimePoint = Point & {
export type Waypoint = Coordinates & {
position: number;
};
export type SpacetimePoint = Coordinates & {
duration: number;
distance: number;
};