@wemap/routers 6.2.2 → 7.0.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.
Files changed (57) hide show
  1. package/assets/biocbon-bergere-rdc-network.osm +163 -0
  2. package/assets/gare-de-lest-network-pp-bounds.osm +1615 -0
  3. package/dist/wemap-routers.es.js +1811 -695
  4. package/dist/wemap-routers.es.js.map +1 -1
  5. package/index.js +13 -5
  6. package/package.json +9 -6
  7. package/src/Constants.js +4 -2
  8. package/src/ItineraryInfoManager.spec.js +2 -2
  9. package/src/Utils.js +0 -77
  10. package/src/model/Itinerary.js +41 -5
  11. package/src/model/Itinerary.spec.js +91 -0
  12. package/src/model/Itinerary.type.spec.js +3 -78
  13. package/src/model/Leg.js +89 -19
  14. package/src/model/Leg.spec.js +110 -0
  15. package/src/model/Leg.type.spec.js +48 -0
  16. package/src/model/LevelChange.js +14 -24
  17. package/src/model/LevelChange.spec.js +78 -0
  18. package/src/model/LevelChange.type.spec.js +26 -0
  19. package/src/model/RouterResponse.js +70 -1
  20. package/src/model/RouterResponse.spec.js +85 -0
  21. package/src/model/RouterResponse.type.spec.js +7 -4
  22. package/src/model/Step.js +45 -6
  23. package/src/model/Step.spec.js +100 -0
  24. package/src/model/Step.type.spec.js +52 -0
  25. package/src/remote/RemoteRouter.js +31 -0
  26. package/src/remote/RemoteRouterManager.js +84 -0
  27. package/src/remote/RemoteRouterOptions.js +25 -0
  28. package/src/remote/RemoteRouterServerUnreachable.js +10 -0
  29. package/src/remote/RemoteRouterUtils.js +78 -0
  30. package/src/remote/RoutingModeCorrespondanceNotFound.js +18 -0
  31. package/src/remote/cityway/CitywayRemoteRouter.js +386 -0
  32. package/src/{cityway/CitywayUtils.spec.js → remote/cityway/CitywayRemoteRouter.spec.js} +19 -18
  33. package/src/remote/deutsche-bahn/DeutscheBahnRemoteRouter.js +143 -0
  34. package/src/{deutsche-bahn/DeutscheBahnRouterUtils.spec.js → remote/deutsche-bahn/DeutscheBahnRemoteRouter.spec.js} +7 -6
  35. package/src/remote/idfm/IdfmRemoteRouter.js +432 -0
  36. package/src/{idfm/IdfmUtils.spec.js → remote/idfm/IdfmRemoteRouter.spec.js} +7 -6
  37. package/src/remote/idfm/IdfmRemoteRouterTokenError.js +6 -0
  38. package/src/remote/osrm/OsrmRemoteRouter.js +331 -0
  39. package/src/{osrm/OsrmUtils.spec.js → remote/osrm/OsrmRemoteRouter.spec.js} +9 -15
  40. package/src/remote/otp/OtpRemoteRouter.js +222 -0
  41. package/src/{otp/OtpUtils.spec.js → remote/otp/OtpRemoteRouter.spec.js} +10 -9
  42. package/src/remote/wemap-meta/WemapMetaRemoteRouter.js +57 -0
  43. package/src/remote/wemap-meta/WemapMetaRemoteRouter.spec.js +22 -0
  44. package/src/remote/wemap-meta/WemapMetaRemoteRouterOptions.js +36 -0
  45. package/src/remote/wemap-meta/WemapMetaRemoteRouterPayload.js +44 -0
  46. package/src/wemap/WemapRouter.js +6 -0
  47. package/src/wemap/WemapRouterUtils.js +10 -4
  48. package/src/wemap/WemapStepsGeneration.js +36 -9
  49. package/src/wemap-meta/IOMap.js +191 -0
  50. package/src/wemap-meta/WemapMetaRouter.js +314 -0
  51. package/src/wemap-meta/WemapMetaRouter.spec.js +119 -0
  52. package/src/wemap-meta/WemapMetaRouterOptions.js +20 -0
  53. package/src/cityway/CitywayUtils.js +0 -252
  54. package/src/deutsche-bahn/DeutscheBahnRouterUtils.js +0 -91
  55. package/src/idfm/IdfmUtils.js +0 -247
  56. package/src/osrm/OsrmUtils.js +0 -269
  57. package/src/otp/OtpUtils.js +0 -150
@@ -1,269 +0,0 @@
1
- /* eslint-disable max-statements */
2
-
3
- import { Level, Coordinates } from '@wemap/geo';
4
- import { rad2deg, positiveMod } from '@wemap/maths';
5
-
6
- import Itinerary from '../model/Itinerary.js';
7
- import Leg from '../model/Leg.js';
8
- import LevelChange from '../model/LevelChange.js';
9
- import RouterResponse from '../model/RouterResponse.js';
10
- import Step from '../model/Step.js';
11
- import { generateStepsMetadata } from '../Utils.js';
12
-
13
- /**
14
- * @param {Coordinates} coordinates
15
- * @returns {object}
16
- */
17
- export function coordinatesToJson(coordinates) {
18
- const output = [coordinates.lng, coordinates.lat];
19
- if (coordinates.level) {
20
- output.push(coordinates.level.toString());
21
- }
22
- return output;
23
- }
24
-
25
- /**
26
- * @param {object} json
27
- * @returns {Coordinates}
28
- */
29
- export function jsonToCoordinates(json) {
30
- const output = new Coordinates(json[1], json[0]);
31
- if (json.length > 2) {
32
- output.level = Level.fromString(json[2]);
33
- }
34
- return output;
35
- }
36
-
37
- export function nodesToJsonCoords(nodes) {
38
- return nodes.map(node => coordinatesToJson(node.coords));
39
- }
40
-
41
-
42
- export function getModifierFromAngle(_angle) {
43
-
44
- const angle = positiveMod(rad2deg(_angle), 360);
45
-
46
- if (angle > 0 && angle < 60) {
47
- return 'sharp right';
48
- }
49
- if (angle >= 60 && angle < 140) {
50
- return 'right';
51
- }
52
- if (angle >= 140 && angle < 160) {
53
- return 'slight right';
54
- }
55
- if (angle >= 160 && angle <= 200) {
56
- return 'straight';
57
- }
58
- if (angle > 200 && angle <= 220) {
59
- return 'slight left';
60
- }
61
- if (angle > 220 && angle <= 300) {
62
- return 'left';
63
- }
64
- if (angle > 300 && angle < 360) {
65
- return 'sharp left';
66
- }
67
- return 'u turn';
68
- }
69
-
70
-
71
- export function noRouteFoundJson(message) {
72
- return {
73
- 'code': 'NoRoute',
74
- message
75
- };
76
- }
77
-
78
- /**
79
- * @param {Itinerary} itinerary
80
- * @returns {object}
81
- */
82
- export function itineraryToOsrmJson(itinerary) {
83
-
84
- const lastLegId = itinerary.legs.length - 1;
85
-
86
- const jsonLegs = itinerary.legs.map(({ distance, duration, coords, steps }, idLeg) => {
87
-
88
- const lastStepId = steps.length - 1;
89
-
90
- return {
91
- distance,
92
- duration,
93
- steps: steps.map((step, idStep, arr) => {
94
-
95
- let type = idStep === 0 && idLeg === 0 ? 'depart' : 'turn';
96
- type = idStep === lastStepId && idLeg === lastLegId ? 'arrive' : type;
97
-
98
- const stepCoordsIdx = coords.findIndex(p => p.equalsTo(step.coords));
99
- const nextStepCoordsIdx = idStep === lastStepId
100
- ? stepCoordsIdx
101
- : coords.findIndex(p => p.equalsTo(arr[idStep + 1].coords));
102
-
103
- const jsonStep = {
104
- geometry: {
105
- type: 'LineString',
106
- coordinates: coords.slice(stepCoordsIdx, nextStepCoordsIdx + 1).map(coordinatesToJson)
107
- },
108
- distance: step.distance,
109
- duration: step.duration,
110
- name: step.name,
111
- maneuver: {
112
- bearing_before: rad2deg(step.previousBearing),
113
- bearing_after: rad2deg(step.nextBearing),
114
- location: coordinatesToJson(step.coords),
115
- modifier: getModifierFromAngle(step.angle),
116
- type
117
- }
118
- };
119
- if (step.levelChange !== null) {
120
- jsonStep.levelChange = step.levelChange.toJson();
121
- }
122
- if (typeof step.extras === 'object' && Object.keys(step.extras).length !== 0) {
123
- jsonStep.extras = step.extras;
124
- }
125
-
126
- return jsonStep;
127
- })
128
- };
129
- });
130
-
131
- return {
132
- 'code': 'Ok',
133
- 'routes': [
134
- {
135
- 'geometry': {
136
- 'type': 'LineString',
137
- 'coordinates': itinerary.coords.map(coordinatesToJson)
138
- },
139
- 'legs': jsonLegs,
140
- 'distance': itinerary.distance,
141
- 'duration': itinerary.duration,
142
- 'weight_name': 'routability',
143
- 'weight': 0
144
- }
145
- ],
146
- 'waypoints': []
147
- };
148
- }
149
-
150
-
151
- /**
152
- * @param {object} jsonSteps
153
- * @param {Coordinates[]} legCoords
154
- * @returns {Step[]}
155
- */
156
- function parseJsonSteps(jsonSteps, legCoords) {
157
-
158
- if (!jsonSteps) {
159
- return [];
160
- }
161
-
162
- return jsonSteps.map(jsonStep => {
163
-
164
- const step = new Step();
165
- step.coords = jsonToCoordinates(jsonStep.maneuver.location);
166
-
167
- // Sometimes, OSRM step does not have the same coordinates than a point in legCoords.
168
- // ex: first step of https://routing.getwemap.com/route/v1/walking/2.33222164147,48.87084765712;2.3320734,48.8730212?geometries=geojson&overview=full&steps=true
169
- // That is why we look for the closest point.
170
- const distances = legCoords.map(coords => coords.distanceTo(step.coords));
171
- const idStepCoordsInLeg = distances.indexOf(Math.min(...distances));
172
- if (idStepCoordsInLeg < 0) {
173
- throw new Error('Osrm Parser: Cannot find step coords in leg coordinates');
174
- }
175
- step._idCoordsInLeg = idStepCoordsInLeg;
176
-
177
- step.name = jsonStep.name;
178
- step.levelChange = jsonStep.levelChange ? LevelChange.fromJson(jsonStep.levelChange) : null;
179
-
180
- step.distance = jsonStep.distance;
181
- step.duration = jsonStep.duration;
182
-
183
- if (jsonStep.extras && jsonStep.extras.subwayEntrance) {
184
- step.extras.subwayEntrance = true;
185
- if (jsonStep.extras.subwayEntranceRef) {
186
- step.extras.subwayEntranceRef = jsonStep.extras.subwayEntranceRef;
187
- }
188
- }
189
-
190
- return step;
191
- });
192
- }
193
-
194
-
195
- /**
196
- * Generate multi itineraries from OSRM JSON
197
- * @param {object} json JSON file provided by OSRM.
198
- * @param {Coordinates} from itinerary start
199
- * @param {Coordinates} to itinerary end
200
- * @param {?string} routingMode [walking|driving|bicycle]
201
- * @returns {?RouterResponse}
202
- */
203
- export function createRouterResponseFromJson(json, from, to, routingMode = 'walking') {
204
-
205
- const { routes: jsonRoutes } = json;
206
-
207
- if (!jsonRoutes) {
208
- return null;
209
- }
210
-
211
- const routingModeCorrespondance = new Map();
212
- routingModeCorrespondance.set('walking', 'WALK');
213
- routingModeCorrespondance.set('driving', 'CAR');
214
- routingModeCorrespondance.set('bicycle', 'BIKE');
215
- const mode = routingModeCorrespondance.get(routingMode) || null;
216
-
217
- const routerResponse = new RouterResponse();
218
- routerResponse.routerName = 'osrm';
219
-
220
- routerResponse.from = from;
221
- routerResponse.to = to;
222
-
223
- for (const jsonItinerary of jsonRoutes) {
224
-
225
- const itinerary = new Itinerary();
226
-
227
- // itinerary.coords = jsonItinerary.geometry.coordinates.map(jsonToCoordinates);
228
- itinerary.distance = jsonItinerary.distance;
229
- itinerary.duration = jsonItinerary.duration;
230
- itinerary.from = from;
231
- itinerary.to = to;
232
-
233
- routerResponse.itineraries.push(itinerary);
234
-
235
- for (const jsonLeg of jsonItinerary.legs) {
236
-
237
- const leg = new Leg();
238
-
239
- leg.mode = mode;
240
- leg.distance = jsonLeg.distance;
241
- leg.duration = jsonLeg.duration;
242
-
243
- leg.coords = jsonLeg.steps
244
- .map(step => step.geometry.coordinates.map(jsonToCoordinates))
245
- .flat()
246
- // Remove duplicates
247
- .filter((coords, idx, arr) => idx === 0 || !arr[idx - 1].equalsTo(coords));
248
-
249
- leg.from = {
250
- name: null,
251
- coords: leg.coords[0]
252
- };
253
- leg.to = {
254
- name: null,
255
- coords: leg.coords[leg.coords.length - 1]
256
- };
257
-
258
- leg.steps = parseJsonSteps(jsonLeg.steps, leg.coords);
259
-
260
- itinerary.legs.push(leg);
261
- }
262
-
263
- // All legs have to be parsed before computing steps metadata
264
- generateStepsMetadata(itinerary);
265
-
266
- }
267
-
268
- return routerResponse;
269
- }
@@ -1,150 +0,0 @@
1
- /* eslint-disable max-statements */
2
- import Polyline from '@mapbox/polyline';
3
-
4
- import { Coordinates } from '@wemap/geo';
5
-
6
- import Itinerary from '../model/Itinerary.js';
7
- import Leg from '../model/Leg.js';
8
- import RouterResponse from '../model/RouterResponse.js';
9
- import Step from '../model/Step.js';
10
- import { generateStepsMetadata } from '../Utils.js';
11
-
12
- /**
13
- * @param {object} json
14
- * @returns {Coordinates}
15
- */
16
- export function jsonToCoordinates(json) {
17
- return new Coordinates(json.lat, json.lon);
18
- }
19
-
20
- /**
21
- * @param {object} jsonSteps
22
- * @param {Coordinates[]} legCoords
23
- * @returns {Step[]}
24
- */
25
- function parseJsonSteps(jsonSteps, legCoords) {
26
-
27
- if (!jsonSteps) {
28
- return [];
29
- }
30
-
31
- return jsonSteps.map(jsonStep => {
32
-
33
- const step = new Step();
34
- const stepCoords = jsonToCoordinates(jsonStep);
35
-
36
- // OTP step does not have the same coordinates than a point in legCoords.
37
- // That is why we look for the closest point.
38
- const distances = legCoords.map(coords => coords.distanceTo(stepCoords));
39
- const idStepCoordsInLeg = distances.indexOf(Math.min(...distances));
40
- if (idStepCoordsInLeg < 0) {
41
- throw new Error('OTP Parser: Cannot find closest step');
42
- }
43
- step.coords = legCoords[idStepCoordsInLeg];
44
- step._idCoordsInLeg = idStepCoordsInLeg;
45
-
46
- step.name = jsonStep.streetName;
47
- step.levelChange = null;
48
-
49
- step.distance = jsonStep.distance;
50
-
51
- return step;
52
- });
53
- }
54
-
55
- /**
56
- * Generate multi itineraries from OTP JSON
57
- * @param {object} json JSON file provided by OTP.
58
- * @returns {?RouterResponse}
59
- */
60
- export function createRouterResponseFromJson(json) {
61
-
62
- const { plan: jsonPlan } = json;
63
-
64
- if (!jsonPlan) {
65
- return null;
66
- }
67
-
68
- const routerResponse = new RouterResponse();
69
- routerResponse.routerName = 'otp';
70
-
71
- routerResponse.from = jsonToCoordinates(jsonPlan.from);
72
- routerResponse.to = jsonToCoordinates(jsonPlan.to);
73
-
74
- for (const jsonItinerary of jsonPlan.itineraries) {
75
-
76
- const itinerary = new Itinerary();
77
-
78
- itinerary.duration = jsonItinerary.duration;
79
- itinerary.startTime = jsonItinerary.startTime;
80
- itinerary.endTime = jsonItinerary.endTime;
81
- itinerary.from = routerResponse.from;
82
- itinerary.to = routerResponse.to;
83
-
84
- routerResponse.itineraries.push(itinerary);
85
-
86
- for (const jsonLeg of jsonItinerary.legs) {
87
-
88
- const leg = new Leg();
89
-
90
- leg.mode = jsonLeg.mode;
91
- leg.duration = jsonLeg.duration;
92
- leg.startTime = jsonLeg.startTime;
93
- leg.endTime = jsonLeg.endTime;
94
- leg.from = {
95
- name: jsonLeg.from.name,
96
- coords: jsonToCoordinates(jsonLeg.from)
97
- };
98
- leg.to = {
99
- name: jsonLeg.to.name,
100
- coords: jsonToCoordinates(jsonLeg.to)
101
- };
102
- leg.coords = Polyline.decode(jsonLeg.legGeometry.points).map(([lat, lon]) => new Coordinates(lat, lon));
103
-
104
- leg.steps = parseJsonSteps(jsonLeg.steps, leg.coords);
105
-
106
- if (leg.mode === 'BUS' || leg.mode === 'TRAM') {
107
- leg.transportInfo = {
108
- name: jsonLeg.route,
109
- routeColor: jsonLeg.routeColor,
110
- routeTextColor: jsonLeg.routeTextColor,
111
- directionName: jsonLeg.headsign
112
- };
113
-
114
- const legStep = new Step();
115
- legStep.coords = leg.coords[0];
116
- legStep._idCoordsInLeg = 0;
117
- legStep.name = jsonLeg.headsign;
118
- legStep.levelChange = null;
119
- legStep.distance = jsonLeg.distance;
120
- leg.steps = [legStep];
121
- }
122
-
123
- // jsonLeg.distance is not reliable when compared to the array of leg coords.
124
- // leg.distance = jsonLeg.distance;
125
- leg.distance = leg.coords.reduce((acc, coords, idx, arr) => {
126
- if (idx === 0) {
127
- return acc;
128
- }
129
- return acc + arr[idx - 1].distanceTo(coords);
130
- }, 0);
131
-
132
- itinerary.legs.push(leg);
133
-
134
- }
135
-
136
- itinerary.distance = itinerary.coords.reduce((acc, coords, idx, arr) => {
137
- if (idx === 0) {
138
- return acc;
139
- }
140
- return acc + arr[idx - 1].distanceTo(coords);
141
- }, 0);
142
-
143
- // All legs have to be parsed before computing steps metadata
144
- generateStepsMetadata(itinerary);
145
- }
146
-
147
- return routerResponse;
148
- }
149
-
150
-