Skip to content

Commit

Permalink
feat: add waypoint support, mainly for valhalla
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstnbwnkl committed Jul 23, 2024
1 parent 88c3618 commit c84fd8f
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 84 deletions.
15 changes: 10 additions & 5 deletions packages/core/BaseRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,42 @@ export interface ClientConstructorArgs {
readonly axiosOpts?: AxiosRequestConfig
}

export interface BaseRouter {
export interface Waypoint {
lat: number
lon: number
}

export interface BaseRouter<T extends Waypoint> {
client: Client<
Record<string | number, any>,
Record<string | number, any> | undefined,
Record<string | number, any> | undefined
>

directions: (
locations: number[][],
locations: ([number, number] | T)[],
profile: string,
directionsOpts?: Record<string | number | symbol, JSONValue>,
dryRun?: boolean
) => Promise<Directions<any, any> | string>

matrix: (
locations: [number, number][],
locations: ([number, number] | T)[],
profile: string,
matrixOpts?: JSONObject,
dryRun?: boolean
) => Promise<Matrix<any> | string>

reachability?: (
location: [number, number],
location: [number, number] | T,
profile: string,
intervals: number[],
isochronesOpts?: JSONObject,
dryRun?: boolean
) => Promise<Isochrones<any, any> | string>

mapMatch?: (
locations: [number, number][],
locations: ([number, number] | T)[],
profile: string,
mapMatchOpts?: JSONObject,
dryRun?: boolean
Expand Down
2 changes: 1 addition & 1 deletion packages/core/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Client<
? (error) =>
isNetworkOrIdempotentRequestError(error) ||
error.response?.status == 429
: undefined,
: () => false,
retryDelay: axiosRetry.exponentialDelay,
onRetry: (number, error) =>
console.log(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { BaseRouter, ClientConstructorArgs } from "./BaseRouter"
export { BaseRouter, ClientConstructorArgs, Waypoint } from "./BaseRouter"
export { default as Client } from "./Client"
export { Direction, Directions, DirectionFeat } from "./Direction"
export { default as Matrix } from "./Matrix"
Expand Down
38 changes: 22 additions & 16 deletions packages/graphhopper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Isochrones,
Matrix,
Client,
Waypoint,
} from "@routingjs/core"
import { LineString } from "geojson"
import {
Expand Down Expand Up @@ -119,7 +120,7 @@ export type GraphHopperClient = Client<
*
* For the full documentation, see {@link https://docs.graphhopper.com}.
*/
export class GraphHopper implements BaseRouter {
export class GraphHopper implements BaseRouter<Waypoint> {
client: GraphHopperClient
apiKey?: string

Expand Down Expand Up @@ -161,26 +162,28 @@ export class GraphHopper implements BaseRouter {
* @param dryRun - if true, will not make the request and instead return an info string containing the URL and request parameters; for debugging
*/
directions(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: GraphHopperProfile,
directionsOpts?: GraphHopperDirectionsOpts,
dryRun?: false
): Promise<GraphHopperDirections>
directions(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: GraphHopperProfile,
directionsOpts: GraphHopperDirectionsOpts,
dryRun: true
): Promise<string>
public async directions(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: GraphHopperProfile,
directionsOpts?: GraphHopperDirectionsOpts,
dryRun?: boolean | undefined
): Promise<string | GraphHopperDirections> {
const params: GraphHopperRouteParams = {
profile,
points: locations.map(([lat, lon]) => [lon, lat]),
points: locations.map((c) =>
Array.isArray(c) ? [c[1], c[0]] : [c.lon, c.lat]
),
...directionsOpts,
}

Expand Down Expand Up @@ -263,28 +266,30 @@ export class GraphHopper implements BaseRouter {
* @see {@link https://docs.graphhopper.com/#tag/Isochrone-API} for the full documentation.
*/
reachability(
location: [number, number],
location: [number, number] | Waypoint,
profile: GraphHopperProfile,
intervals: [number],
isochronesOpts?: GraphHopperIsochroneOpts,
dryRun?: false
): Promise<GraphHopperIsochrones>
reachability(
location: [number, number],
location: [number, number] | Waypoint,
profile: GraphHopperProfile,
intervals: [number],
isochronesOpts: GraphHopperIsochroneOpts,
dryRun: true
): Promise<string>
public async reachability(
location: [number, number],
location: [number, number] | Waypoint,
profile: GraphHopperProfile,
intervals: [number],
isochronesOpts?: GraphHopperIsochroneOpts,
dryRun?: boolean | undefined
): Promise<string | GraphHopperIsochrones> {
const params: GraphHopperIsochroneGetParams = {
point: location.join(","),
point: Array.isArray(location)
? `${location[0]}, ${location[1]}`
: `${location.lat},${location.lon}`,
profile,
}

Expand Down Expand Up @@ -333,13 +338,13 @@ export class GraphHopper implements BaseRouter {
*/
public static parseIsochroneResponse(
response: GraphHopperIsochroneResponse,
center: [number, number],
center: [number, number] | Waypoint,
intervalType: "time" | "distance"
): GraphHopperIsochrones {
const isochrones: Isochrone<GraphHopperIsochroneProps>[] =
response.polygons.map((poly) => {
return new Isochrone(
center,
Array.isArray(center) ? center : [center.lat, center.lon],
poly.properties.bucket,
intervalType,
poly
Expand Down Expand Up @@ -367,19 +372,19 @@ export class GraphHopper implements BaseRouter {
*
*/
matrix(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: GraphHopperProfile,
matrixOpts?: GraphHopperMatrixOpts,
dryRun?: false
): Promise<GraphHopperMatrix>
matrix(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: GraphHopperProfile,
matrixOpts: GraphHopperMatrixOpts,
dryRun: true
): Promise<string>
public async matrix(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: GraphHopperProfile,
matrixOpts?: GraphHopperMatrixOpts,
dryRun?: boolean | undefined
Expand All @@ -392,15 +397,16 @@ export class GraphHopper implements BaseRouter {
return matrixOpts.sources.includes(i)
} else return true
})
.map(([lat, lon]) => [lon, lat]), // reverse order for POST requests
.map((c) => (Array.isArray(c) ? [c[1], c[0]] : [c.lon, c.lat])), // reverse order for POST requests

to_points: locations
.filter((coords, i) => {
if (matrixOpts?.destinations !== undefined) {
return matrixOpts.destinations.includes(i)
} else return true
})
.map(([lat, lon]) => [lon, lat]),
.map((c) => (Array.isArray(c) ? [c[1], c[0]] : [c.lon, c.lat])), // reverse order for POST requests

...matrixOpts,
}

Expand Down
33 changes: 20 additions & 13 deletions packages/ors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Matrix,
Isochrone,
Isochrones,
Waypoint,
} from "@routingjs/core"

import {
Expand Down Expand Up @@ -85,7 +86,7 @@ export type ORSClient = Client<
ORSRouteParams | ORSMatrixParams | ORSIsochroneParams
>

export class ORS implements BaseRouter {
export class ORS implements BaseRouter<Waypoint> {
client: ORSClient
apiKey?: string
constructor(clientArgs?: ClientConstructorArgs) {
Expand Down Expand Up @@ -118,21 +119,21 @@ export class ORS implements BaseRouter {
)
}
directions(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: ORSProfile,
directionsOpts?: ORSDirectionsOpts,
dryRun?: false,
format?: ORSFormat
): Promise<ORSDirections>
directions(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: ORSProfile,
directionsOpts: ORSDirectionsOpts,
dryRun: true,
format?: ORSFormat
): Promise<string>
public async directions(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: ORSProfile,
directionsOpts: ORSDirectionsOpts = {},
dryRun?: boolean,
Expand All @@ -156,7 +157,9 @@ export class ORS implements BaseRouter {
}

const params: ORSRouteParams = {
coordinates: locations.map(([lat, lon]) => [lon, lat]),
coordinates: locations.map((c) =>
Array.isArray(c) ? [c[1], c[0]] : [c.lon, c.lat]
),
...directionsOpts,
}

Expand Down Expand Up @@ -245,29 +248,31 @@ export class ORS implements BaseRouter {
}

reachability(
location: [number, number],
location: [number, number] | Waypoint,
profile: string,
intervals: number[],
isochronesOpts?: ORSIsochroneOpts,
dryRun?: false
): Promise<ORSIsochrones>
reachability(
location: [number, number],
location: [number, number] | Waypoint,
profile: string,
intervals: number[],
isochronesOpts: ORSIsochroneOpts,
dryRun: true
): Promise<string>
async reachability(
location: [number, number],
location: [number, number] | Waypoint,
profile: string,
intervals: number[],
isochronesOpts: ORSIsochroneOpts = {},
dryRun?: boolean
): Promise<ORSIsochrones | string> {
const { range_type, ...rest } = isochronesOpts
const params: ORSIsochroneParams = {
locations: [[location[1], location[0]]], // format must be lon/lat
locations: Array.isArray(location)
? [[location[1], location[0]]]
: [[location.lon, location.lat]], // format must be lon/lat
range: intervals,
...rest,
}
Expand Down Expand Up @@ -313,25 +318,27 @@ export class ORS implements BaseRouter {
}

matrix(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: ORSProfile,
matrixOpts?: ORSMatrixOpts,
dryRun?: false
): Promise<ORSMatrix>
matrix(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: ORSProfile,
matrixOpts: ORSMatrixOpts,
dryRun: true
): Promise<string>
async matrix(
locations: [number, number][],
locations: ([number, number] | Waypoint)[],
profile: ORSProfile,
matrixOpts: ORSMatrixOpts = {},
dryRun?: boolean
): Promise<ORSMatrix | string> {
const params: ORSMatrixParams = {
locations: locations.map(([lat, lon]) => [lon, lat]),
locations: locations.map((c) =>
Array.isArray(c) ? [c[1], c[0]] : [c.lon, c.lat]
),
...matrixOpts,
}

Expand Down
Loading

0 comments on commit c84fd8f

Please sign in to comment.