@wemap/routers 11.2.3 → 11.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,39 +11,14 @@ import { getDurationFromLength } from '../Utils.js';
11
11
  import OsmGraph, { OsmEdge, OsmEdgeData, OsmVertex, OsmVertexData } from './OsmGraph.js';
12
12
  import Leg from '../model/Leg.js';
13
13
  import { MinStepInfo } from '../model/Step.js';
14
+ import { OsmRouterOptions } from './OsmRouterOptions.js';
14
15
 
15
16
 
16
- const DEFAULT_OPTIONS = Object.assign({}, GeoGraphRouter.DEFAULT_OPTIONS, {
17
- weightEdgeFn: (edge: OsmEdge) => {
18
- if (edge.data.isElevator) {
19
- return 90;
20
- }
21
- let duration = getDurationFromLength(edge.length, 4);
22
- if (edge.data instanceof OsmWay && edge.data.areStairs) {
23
- duration *= 3;
24
- }
25
- return duration;
26
- },
27
- acceptEdgeFn: (edge: GeoGraphEdge) => {
28
- const accessTag = (edge as OsmEdge).data.tags.access;
29
- return typeof accessTag === 'undefined' || accessTag === 'yes';
30
- }
31
- });
32
-
33
- const WITHOUT_STAIRS_OPTIONS = Object.assign({}, DEFAULT_OPTIONS, {
34
- acceptEdgeFn: (edge: GeoGraphEdge) => (edge as OsmEdge).data.tags.highway !== 'steps'
35
- });
36
-
37
17
  export type GeoGraphTripRouterOptions<VertexData = unknown, EdgeData = unknown> =
38
18
  GeoGraphRouterOptions<VertexData, EdgeData> & {
39
19
  tspTempCoeff?: number; // https://github.com/wemap/salesman.js/blob/master/salesman.js#L164
40
20
  };
41
21
 
42
- const DEFAULT_TRIP_OPTIONS = Object.assign({}, {
43
- tspTempCoeff: 0.99
44
- }, GeoGraphRouter.DEFAULT_OPTIONS);
45
-
46
-
47
22
  const buildStepsRules = (graphItinerary: OsmItinerary): StepsGenerationRules => (
48
23
  currentCoords: Coordinates,
49
24
  nextCoords: Coordinates,
@@ -64,10 +39,14 @@ const buildStepsRules = (graphItinerary: OsmItinerary): StepsGenerationRules =>
64
39
  let levelChangeType: LevelChangeType | null = null;
65
40
  if (edge.data.isElevator) {
66
41
  levelChangeType = 'elevator';
67
- } else if (edge.data.isConveying) {
42
+ } else if (edge.data instanceof OsmWay && edge.data.isConveying && edge.data.areStairs) {
43
+ // TODO: change to escalator
68
44
  levelChangeType = 'conveyor';
45
+ // levelChangeType = 'escalator';
69
46
  } else if (edge.data instanceof OsmWay && edge.data.areStairs) {
70
47
  levelChangeType = 'stairs';
48
+ } else if (edge.data instanceof OsmWay && edge.data.isConveying) {
49
+ levelChangeType = 'moving walkway';
71
50
  }
72
51
 
73
52
  // Handle stairs/elevator/conveyors without change in level coordinates
@@ -124,8 +103,9 @@ export class OsmItinerary extends GeoGraphItinerary<OsmVertexData, OsmEdgeData>
124
103
 
125
104
  class WemapOsmRouter extends GeoGraphRouter<OsmVertexData, OsmEdgeData> {
126
105
 
127
- static DEFAULT_OPTIONS = DEFAULT_OPTIONS;
128
- static WITHOUT_STAIRS_OPTIONS = WITHOUT_STAIRS_OPTIONS;
106
+ static DEFAULT_OPTIONS = WemapOsmRouter.buildOptions();
107
+ static WITHOUT_STAIRS_OPTIONS = WemapOsmRouter.buildOptions({ useStairs: false });
108
+ static DEFAULT_TRIP_OPTIONS = WemapOsmRouter.buildTripOptions();
129
109
 
130
110
  constructor(graph: OsmGraph) {
131
111
  super(graph);
@@ -138,7 +118,7 @@ class WemapOsmRouter extends GeoGraphRouter<OsmVertexData, OsmEdgeData> {
138
118
  getShortestPath(
139
119
  start: OsmVertex | Coordinates,
140
120
  end: OsmVertex | Coordinates,
141
- options: GeoGraphRouterOptions<OsmVertexData, OsmEdgeData> = DEFAULT_OPTIONS
121
+ options: GeoGraphRouterOptions<OsmVertexData, OsmEdgeData> = WemapOsmRouter.DEFAULT_OPTIONS
142
122
  ) {
143
123
  return super.getShortestPath(start, end, options) as OsmItinerary;
144
124
  }
@@ -146,7 +126,7 @@ class WemapOsmRouter extends GeoGraphRouter<OsmVertexData, OsmEdgeData> {
146
126
  getItinerary(
147
127
  start: OsmVertex | Coordinates,
148
128
  end: OsmVertex | Coordinates,
149
- options: GeoGraphRouterOptions<OsmVertexData, OsmEdgeData> = DEFAULT_OPTIONS
129
+ options: GeoGraphRouterOptions<OsmVertexData, OsmEdgeData> = WemapOsmRouter.DEFAULT_OPTIONS
150
130
  ) {
151
131
  const graphItinerary = this.getShortestPath(start, end, options);
152
132
 
@@ -177,7 +157,7 @@ class WemapOsmRouter extends GeoGraphRouter<OsmVertexData, OsmEdgeData> {
177
157
 
178
158
  getShortestTrip(
179
159
  waypoints: Coordinates[],
180
- options: GeoGraphTripRouterOptions<OsmVertexData, OsmEdgeData> = DEFAULT_TRIP_OPTIONS
160
+ options: GeoGraphTripRouterOptions<OsmVertexData, OsmEdgeData> = WemapOsmRouter.DEFAULT_TRIP_OPTIONS
181
161
  ) {
182
162
  type CustomPoint = salesman.Point & { coords: Coordinates };
183
163
 
@@ -221,7 +201,7 @@ class WemapOsmRouter extends GeoGraphRouter<OsmVertexData, OsmEdgeData> {
221
201
 
222
202
  getTripItinerary(
223
203
  waypoints: Coordinates[],
224
- options: GeoGraphTripRouterOptions<OsmVertexData, OsmEdgeData> = DEFAULT_TRIP_OPTIONS
204
+ options: GeoGraphTripRouterOptions<OsmVertexData, OsmEdgeData> = WemapOsmRouter.DEFAULT_TRIP_OPTIONS
225
205
  ) {
226
206
  const shortestTrip = this.getShortestTrip(waypoints, options);
227
207
  return new Itinerary({
@@ -230,6 +210,54 @@ class WemapOsmRouter extends GeoGraphRouter<OsmVertexData, OsmEdgeData> {
230
210
  legs: shortestTrip.map(graphItinerary => Leg.fromGraphItinerary(graphItinerary))
231
211
  });
232
212
  }
213
+
214
+ static buildOptions(options?: OsmRouterOptions): GeoGraphRouterOptions<OsmVertexData, OsmEdgeData> {
215
+
216
+ const weightEdgeFn = (edge: GeoGraphEdge<OsmVertexData, OsmEdgeData>) => {
217
+ if (edge.data?.isElevator) {
218
+ return 90;
219
+ }
220
+ let duration = getDurationFromLength(edge.length, 4);
221
+ if (edge.data instanceof OsmWay && edge.data.areStairs) {
222
+ duration *= 3;
223
+ }
224
+ return duration;
225
+ }
226
+
227
+ const acceptEdgeFn = (edge: GeoGraphEdge<OsmVertexData, OsmEdgeData>) => {
228
+
229
+ // Verify access
230
+ const accessTag = edge.data?.tags?.access;
231
+ if (typeof accessTag !== 'undefined' && accessTag !== 'yes') return false;
232
+
233
+ // Verify stairs
234
+ if (typeof options?.useStairs !== 'undefined' && !options?.useStairs
235
+ && edge.data instanceof OsmWay && edge.data.areStairs && !edge.data.isConveying) return false;
236
+
237
+ // Verify escalators
238
+ if (typeof options?.useEscalator !== 'undefined' && !options?.useEscalator
239
+ && edge.data instanceof OsmWay && edge.data.areStairs && edge.data.isConveying) return false;
240
+
241
+ // Verify elevator
242
+ if (typeof options?.useElevator !== 'undefined' && !options?.useElevator && edge.data?.isElevator) return false;
243
+
244
+ return true;
245
+ };
246
+
247
+ return {
248
+ weightEdgeFn,
249
+ acceptEdgeFn,
250
+ projectionMaxDistance: GeoGraphRouter.DEFAULT_OPTIONS.projectionMaxDistance
251
+ };
252
+ }
253
+
254
+ static buildTripOptions(options?: OsmRouterOptions): GeoGraphTripRouterOptions<OsmVertexData, OsmEdgeData> {
255
+ return Object.assign({},
256
+ WemapOsmRouter.buildOptions(options),
257
+ { tspTempCoeff: 0.99 }
258
+ );
259
+ }
260
+
233
261
  }
234
262
 
235
263
  export default WemapOsmRouter;
@@ -0,0 +1,12 @@
1
+ export type OsmRouterOptions = {
2
+
3
+ useStairs?: boolean;
4
+
5
+ useEscalator?: boolean;
6
+
7
+ useElevator?: boolean;
8
+
9
+ isTrip?: boolean;
10
+
11
+ tspTempCoeff?: number;
12
+ }