@wemap/routers 8.1.0 → 8.2.1

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.
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "directory": "packages/routers"
12
12
  },
13
13
  "name": "@wemap/routers",
14
- "version": "8.1.0",
14
+ "version": "8.2.1",
15
15
  "bugs": {
16
16
  "url": "https://github.com/wemap/wemap-modules-js/issues"
17
17
  },
@@ -34,5 +34,5 @@
34
34
  "@wemap/maths": "^8.1.0",
35
35
  "@wemap/osm": "^8.1.0"
36
36
  },
37
- "gitHead": "cadc0d73f0b0b5730c09b67836c31fbdb4fe94a3"
37
+ "gitHead": "6f0d3c90242a914f3daf7fcee165a4283c459fef"
38
38
  }
@@ -19,7 +19,9 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
19
19
  const filePath = path.resolve(__dirname, '../assets/itinerary-grenoble-otp-1.json');
20
20
  const fileString = fs.readFileSync(filePath, 'utf8');
21
21
  const json = JSON.parse(fileString);
22
- const routerResponse = OtpRemoteRouter.createRouterResponseFromJson(json);
22
+ const from = new Coordinates(45.187291, 5.723455);
23
+ const to = new Coordinates(45.163729, 5.74085);
24
+ const routerResponse = OtpRemoteRouter.createRouterResponseFromJson(json, from, to);
23
25
 
24
26
  let position, itineraryInfo;
25
27
 
@@ -100,7 +100,7 @@ class CitywayRemoteRouter extends RemoteRouter {
100
100
  const jsonResponse = await res.json().catch(() => {
101
101
  throw new RemoteRouterServerUnreachable(this.rname, url);
102
102
  });
103
- return this.createRouterResponseFromJson(jsonResponse);
103
+ return this.createRouterResponseFromJson(jsonResponse, waypoints[0], waypoints[1]);
104
104
  }
105
105
 
106
106
 
@@ -132,18 +132,21 @@ class CitywayRemoteRouter extends RemoteRouter {
132
132
  /**
133
133
  * Generate multi itineraries from Cityway JSON
134
134
  * @param {object} json JSON file provided by Cityway.
135
- * @returns {?RouterResponse}
135
+ * @param {Coordinates} from
136
+ * @param {Coordinates} to
137
+ * @returns {!RouterResponse}
136
138
  * @example https://preprod.api.lia2.cityway.fr/journeyplanner/api/opt/PlanTrips/json?DepartureLatitude=49.51509388236216&DepartureLongitude=0.09341749619366316&ArrivalLatitude=49.5067090188444&ArrivalLongitude=0.1694842115417831&DepartureType=COORDINATES&ArrivalType=COORDINATES
137
139
  */
138
- createRouterResponseFromJson(json) {
139
-
140
- if (json.StatusCode !== 200 || !json.Data || !json.Data.length) {
141
- return null;
142
- }
140
+ createRouterResponseFromJson(json, from, to) {
143
141
 
144
142
  const routerResponse = new RouterResponse();
145
143
  routerResponse.routerName = this.rname;
144
+ routerResponse.from = from;
145
+ routerResponse.to = to;
146
146
 
147
+ if (json.StatusCode !== 200 || !json.Data || !json.Data.length) {
148
+ return routerResponse;
149
+ }
147
150
 
148
151
  // Do not know if it the best approach, but it works...
149
152
  const allJsonTrips = json.Data.reduce((acc, dataObj) => {
@@ -302,9 +305,6 @@ class CitywayRemoteRouter extends RemoteRouter {
302
305
  generateStepsMetadata(itinerary);
303
306
  }
304
307
 
305
- routerResponse.from = routerResponse.itineraries[0].from;
306
- routerResponse.to = routerResponse.itineraries[routerResponse.itineraries.length - 1].to;
307
-
308
308
  return routerResponse;
309
309
  }
310
310
 
@@ -24,7 +24,9 @@ describe('CitywayRemoteRouter - createRouterResponseFromJson', () => {
24
24
  const fileString = fs.readFileSync(filePath, 'utf8');
25
25
  const json = JSON.parse(fileString);
26
26
 
27
- const routerResponse = CitywayRemoteRouter.createRouterResponseFromJson(json);
27
+ const from = new Coordinates(49.515093882362159, 0.093417496193663158);
28
+ const to = new Coordinates(49.5067090188444, 0.16948421154178309);
29
+ const routerResponse = CitywayRemoteRouter.createRouterResponseFromJson(json, from, to);
28
30
  checkRouterResponseType(routerResponse);
29
31
 
30
32
  expect(routerResponse.routerName).equal('cityway');
@@ -62,12 +64,14 @@ describe('CitywayRemoteRouter - createRouterResponseFromJson', () => {
62
64
  expect(itinerary1leg2.transportInfo.directionName).equal('Graville');
63
65
  });
64
66
 
67
+ const fakeCoords = new Coordinates(0, 0);
68
+
65
69
  it('RouterResponse - 2', () => {
66
70
  const filePath = path.resolve(assetsPath, 'itinerary-lehavre-cityway-2.json');
67
71
  const fileString = fs.readFileSync(filePath, 'utf8');
68
72
  const json = JSON.parse(fileString);
69
73
 
70
- const routerResponse = CitywayRemoteRouter.createRouterResponseFromJson(json);
74
+ const routerResponse = CitywayRemoteRouter.createRouterResponseFromJson(json, fakeCoords, fakeCoords);
71
75
  checkRouterResponseType(routerResponse);
72
76
 
73
77
  expect(routerResponse.itineraries.length).equal(1);
@@ -79,7 +83,7 @@ describe('CitywayRemoteRouter - createRouterResponseFromJson', () => {
79
83
  const fileString = fs.readFileSync(filePath, 'utf8');
80
84
  const json = JSON.parse(fileString);
81
85
 
82
- const routerResponse = CitywayRemoteRouter.createRouterResponseFromJson(json);
86
+ const routerResponse = CitywayRemoteRouter.createRouterResponseFromJson(json, fakeCoords, fakeCoords);
83
87
  checkRouterResponseType(routerResponse);
84
88
 
85
89
  expect(routerResponse.itineraries[0].mode).equal('PT');
@@ -91,7 +95,7 @@ describe('CitywayRemoteRouter - createRouterResponseFromJson', () => {
91
95
  const fileString = fs.readFileSync(filePath, 'utf8');
92
96
  const json = JSON.parse(fileString);
93
97
 
94
- const routerResponse = CitywayRemoteRouter.createRouterResponseFromJson(json);
98
+ const routerResponse = CitywayRemoteRouter.createRouterResponseFromJson(json, fakeCoords, fakeCoords);
95
99
  checkRouterResponseType(routerResponse);
96
100
 
97
101
  expect(routerResponse.itineraries[0].mode).equal('BIKE');
@@ -103,7 +107,7 @@ describe('CitywayRemoteRouter - createRouterResponseFromJson', () => {
103
107
  const fileString = fs.readFileSync(filePath, 'utf8');
104
108
  const json = JSON.parse(fileString);
105
109
 
106
- const routerResponse = CitywayRemoteRouter.createRouterResponseFromJson(json);
110
+ const routerResponse = CitywayRemoteRouter.createRouterResponseFromJson(json, fakeCoords, fakeCoords);
107
111
  checkRouterResponseType(routerResponse);
108
112
 
109
113
  expect(routerResponse.itineraries[1].mode).equal('PT');
@@ -64,24 +64,22 @@ class DeutscheBahnRemoteRouter extends RemoteRouter {
64
64
  /**
65
65
  * Generate multi itineraries from the DB JSON
66
66
  * @param {object} json JSON file provided by the DB.
67
- * @param {Coordinates} from itinerary start
68
- * @param {Coordinates} to itinerary end
69
- * @returns {?RouterResponse}
67
+ * @param {Coordinates} from
68
+ * @param {Coordinates} to
69
+ * @returns {!RouterResponse}
70
70
  */
71
71
  createRouterResponseFromJson(json, from, to) {
72
72
 
73
- const { segments: jsonSegments } = json;
74
-
75
- if (!jsonSegments) {
76
- return null;
77
- }
78
-
79
73
  const routerResponse = new RouterResponse();
80
74
  routerResponse.routerName = this.rname;
81
-
82
75
  routerResponse.from = from;
83
76
  routerResponse.to = to;
84
77
 
78
+ const { segments: jsonSegments } = json;
79
+ if (!jsonSegments) {
80
+ return routerResponse;
81
+ }
82
+
85
83
  /** @type {GraphEdge<OsmElement>[]} */
86
84
  const edges = [];
87
85
 
@@ -127,7 +127,7 @@ class IdfmRemoteRouter extends RemoteRouter {
127
127
  return routerResponse;
128
128
  }
129
129
 
130
- return this.createRouterResponseFromJson(jsonResponse);
130
+ return this.createRouterResponseFromJson(jsonResponse, waypoints[0], waypoints[1]);
131
131
  }
132
132
 
133
133
  /**
@@ -327,19 +327,20 @@ class IdfmRemoteRouter extends RemoteRouter {
327
327
  /**
328
328
  * Generate multi itineraries from OTP JSON
329
329
  * @param {object} json JSON file provided by OTP.
330
- * @returns {?RouterResponse}
330
+ * @param {Coordinates} from
331
+ * @param {Coordinates} to
332
+ * @returns {!RouterResponse}
331
333
  */
332
- createRouterResponseFromJson(json) {
333
-
334
- if (!json || !json.journeys) {
335
- return null;
336
- }
334
+ createRouterResponseFromJson(json, from, to) {
337
335
 
338
336
  const routerResponse = new RouterResponse();
339
337
  routerResponse.routerName = this.rname;
338
+ routerResponse.from = from;
339
+ routerResponse.to = to;
340
340
 
341
- routerResponse.from = this.getSectionCoords(json.journeys[0].sections[0]).from;
342
- routerResponse.to = this.getSectionCoords(last(json.journeys[0].sections)).to;
341
+ if (!json || !json.journeys) {
342
+ return routerResponse;
343
+ }
343
344
 
344
345
  const timeZone = json.context.timezone;
345
346
 
@@ -350,8 +351,8 @@ class IdfmRemoteRouter extends RemoteRouter {
350
351
  itinerary.duration = jsonItinerary.duration;
351
352
  itinerary.startTime = this.dateStringToTimestamp(jsonItinerary.departure_date_time, timeZone);
352
353
  itinerary.endTime = this.dateStringToTimestamp(jsonItinerary.arrival_date_time, timeZone);
353
- itinerary.from = routerResponse.from;
354
- itinerary.to = routerResponse.to;
354
+ itinerary.from = this.getSectionCoords(jsonItinerary.sections[0]).from;
355
+ itinerary.to = this.getSectionCoords(last(jsonItinerary.sections)).to;
355
356
  itinerary.distance = 0;
356
357
 
357
358
  routerResponse.itineraries.push(itinerary);
@@ -364,7 +365,7 @@ class IdfmRemoteRouter extends RemoteRouter {
364
365
 
365
366
  const leg = new Leg();
366
367
  let existingCoords = [];
367
- const { from, to } = this.getSectionCoords(jsonSection);
368
+ const { from: fromSection, to: toSection } = this.getSectionCoords(jsonSection);
368
369
 
369
370
  leg.distance = 0;
370
371
  leg.mode = routingModeCorrespondance.get(jsonSection.mode);
@@ -374,12 +375,12 @@ class IdfmRemoteRouter extends RemoteRouter {
374
375
 
375
376
  leg.from = {
376
377
  name: jsonSection.from.name,
377
- coords: from
378
+ coords: fromSection
378
379
  };
379
380
 
380
381
  leg.to = {
381
382
  name: jsonSection.to.name,
382
- coords: to
383
+ coords: toSection
383
384
  };
384
385
 
385
386
  // A section can have multiple same coordinates, we need to remove them
@@ -24,15 +24,17 @@ describe('IdfmRouter - createRouterResponseFromJson', () => {
24
24
  const fileString = fs.readFileSync(filePath, 'utf8');
25
25
  const json = JSON.parse(fileString);
26
26
 
27
- const routerResponse = IdfmRemoteRouter.createRouterResponseFromJson(json);
27
+ const from = new Coordinates(48.875877, 2.301357);
28
+ const to = new Coordinates(48.877877, 2.351929);
29
+ const routerResponse = IdfmRemoteRouter.createRouterResponseFromJson(json, from, to);
28
30
  checkRouterResponseType(routerResponse);
29
31
 
30
32
  expect(routerResponse.routerName).equal('idfm');
31
33
  expect(routerResponse.itineraries.length).equal(5);
32
- expect(routerResponse.from.equalsTo(new Coordinates(48.875877, 2.301357))).true;
33
- expect(routerResponse.to.equalsTo(new Coordinates(48.877877, 2.351929))).true;
34
34
 
35
35
  const itinerary1 = routerResponse.itineraries[0];
36
+ expect(itinerary1.from.equalsTo(new Coordinates(48.875877, 2.301357))).true;
37
+ expect(itinerary1.to.equalsTo(new Coordinates(48.877877, 2.351929))).true;
36
38
  expect(itinerary1.distance).to.be.closeTo(1242, 1);
37
39
  expect(itinerary1.duration).equal(1842);
38
40
  expect(itinerary1.mode).equal('PT');
@@ -53,7 +53,9 @@ class OsrmRemoteRouter extends RemoteRouter {
53
53
  throw new RemoteRouterServerUnreachable(this.rname, url);
54
54
  });
55
55
 
56
- return this.createRouterResponseFromJson(jsonResponse, waypoints[0], waypoints[1]);
56
+ const from = waypoints[0];
57
+ const to = waypoints[waypoints.length - 1];
58
+ return this.createRouterResponseFromJson(jsonResponse, from, to);
57
59
  }
58
60
 
59
61
  /**
@@ -265,10 +267,15 @@ class OsrmRemoteRouter extends RemoteRouter {
265
267
  * @returns {?RouterResponse}
266
268
  */
267
269
  createRouterResponseFromJson(json, from, to, routingMode = 'walking') {
268
- const { routes: jsonRoutes } = json;
269
270
 
271
+ const routerResponse = new RouterResponse();
272
+ routerResponse.routerName = this.rname;
273
+ routerResponse.from = from;
274
+ routerResponse.to = to;
275
+
276
+ const { routes: jsonRoutes } = json;
270
277
  if (!jsonRoutes) {
271
- return null;
278
+ return routerResponse;
272
279
  }
273
280
 
274
281
  const routingModeCorrespondance = new Map();
@@ -277,12 +284,6 @@ class OsrmRemoteRouter extends RemoteRouter {
277
284
  routingModeCorrespondance.set('bicycle', 'BIKE');
278
285
  const mode = routingModeCorrespondance.get(routingMode) || null;
279
286
 
280
- const routerResponse = new RouterResponse();
281
- routerResponse.routerName = this.rname;
282
-
283
- routerResponse.from = from;
284
- routerResponse.to = to;
285
-
286
287
  for (const jsonItinerary of jsonRoutes) {
287
288
 
288
289
  const itinerary = new Itinerary();
@@ -50,7 +50,7 @@ class OtpRemoteRouter extends RemoteRouter {
50
50
  const jsonResponse = await res.json().catch(() => {
51
51
  throw new RemoteRouterServerUnreachable(this.rname, url);
52
52
  });
53
- return this.createRouterResponseFromJson(jsonResponse);
53
+ return this.createRouterResponseFromJson(jsonResponse, waypoints[0], waypoints[1]);
54
54
  }
55
55
 
56
56
  /**
@@ -127,21 +127,24 @@ class OtpRemoteRouter extends RemoteRouter {
127
127
  /**
128
128
  * Generate multi itineraries from OTP JSON
129
129
  * @param {object} json JSON file provided by OTP.
130
- * @returns {?RouterResponse}
130
+ * @param {Coordinates} from
131
+ * @param {Coordinates} to
132
+ * @returns {!RouterResponse}
131
133
  */
132
- createRouterResponseFromJson(json) {
134
+ createRouterResponseFromJson(json, from, to) {
133
135
 
134
- const { plan: jsonPlan } = json;
136
+ const routerResponse = new RouterResponse();
137
+ routerResponse.routerName = this.rname;
138
+ routerResponse.from = from;
139
+ routerResponse.to = to;
135
140
 
141
+ const { plan: jsonPlan } = json;
136
142
  if (!jsonPlan) {
137
- return null;
143
+ return routerResponse;
138
144
  }
139
145
 
140
- const routerResponse = new RouterResponse();
141
- routerResponse.routerName = this.rname;
142
-
143
- routerResponse.from = this.jsonToCoordinates(jsonPlan.from);
144
- routerResponse.to = this.jsonToCoordinates(jsonPlan.to);
146
+ const itinerariesFrom = this.jsonToCoordinates(jsonPlan.from);
147
+ const itinerariesTo = this.jsonToCoordinates(jsonPlan.to);
145
148
 
146
149
  for (const jsonItinerary of jsonPlan.itineraries) {
147
150
 
@@ -150,8 +153,8 @@ class OtpRemoteRouter extends RemoteRouter {
150
153
  itinerary.duration = jsonItinerary.duration;
151
154
  itinerary.startTime = jsonItinerary.startTime;
152
155
  itinerary.endTime = jsonItinerary.endTime;
153
- itinerary.from = routerResponse.from;
154
- itinerary.to = routerResponse.to;
156
+ itinerary.from = itinerariesFrom;
157
+ itinerary.to = itinerariesTo;
155
158
 
156
159
  routerResponse.itineraries.push(itinerary);
157
160
 
@@ -26,15 +26,17 @@ describe('OtpRouter - createRouterResponseFromJson', () => {
26
26
  const fileString = fs.readFileSync(filePath, 'utf8');
27
27
  const json = JSON.parse(fileString);
28
28
 
29
- const routerResponse = OtpRemoteRouter.createRouterResponseFromJson(json);
29
+ const from = new Coordinates(45.187291, 5.723455);
30
+ const to = new Coordinates(45.163729, 5.74085);
31
+ const routerResponse = OtpRemoteRouter.createRouterResponseFromJson(json, from, to);
30
32
  checkRouterResponseType(routerResponse);
31
33
 
32
34
  expect(routerResponse.routerName).equal('otp');
33
35
  expect(routerResponse.itineraries.length).equal(2);
34
- expect(routerResponse.from.equalsTo(new Coordinates(45.187291, 5.723455))).true;
35
- expect(routerResponse.to.equalsTo(new Coordinates(45.163729, 5.74085))).true;
36
36
 
37
37
  const itinerary1 = routerResponse.itineraries[0];
38
+ expect(itinerary1.from.equalsTo(new Coordinates(45.187291, 5.723455))).true;
39
+ expect(itinerary1.to.equalsTo(new Coordinates(45.163729, 5.74085))).true;
38
40
  expect(itinerary1.coords.length).equal(162);
39
41
  expect(itinerary1.duration).equal(1206);
40
42
  expect(itinerary1.startTime).equal(1614607210000);
@@ -70,14 +72,16 @@ describe('OtpRouter - createRouterResponseFromJson', () => {
70
72
  const fileString = fs.readFileSync(filePath, 'utf8');
71
73
  const json = JSON.parse(fileString);
72
74
 
73
- const routerResponse = OtpRemoteRouter.createRouterResponseFromJson(json);
75
+ const from = new Coordinates(45.192514856662456, 5.731452541397871);
76
+ const to = new Coordinates(45.18544, 5.73123);
77
+ const routerResponse = OtpRemoteRouter.createRouterResponseFromJson(json, from, to);
74
78
  checkRouterResponseType(routerResponse);
75
79
 
76
80
  expect(routerResponse.itineraries.length).equal(2);
77
- expect(routerResponse.from.equalsTo(new Coordinates(45.192514856662456, 5.731452541397871))).true;
78
- expect(routerResponse.to.equalsTo(new Coordinates(45.18544, 5.73123))).true;
79
81
 
80
82
  const itinerary1 = routerResponse.itineraries[0];
83
+ expect(itinerary1.from.equalsTo(new Coordinates(45.192514856662456, 5.731452541397871))).true;
84
+ expect(itinerary1.to.equalsTo(new Coordinates(45.18544, 5.73123))).true;
81
85
  expect(itinerary1.legs.length).equal(5);
82
86
 
83
87
  expect(itinerary1.mode).equal('PT');