@wemap/routers 7.5.1 → 7.5.2

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": "7.5.1",
14
+ "version": "7.5.2",
15
15
  "bugs": {
16
16
  "url": "https://github.com/wemap/wemap-modules-js/issues"
17
17
  },
@@ -34,5 +34,5 @@
34
34
  "@wemap/maths": "^7.5.0",
35
35
  "@wemap/osm": "^7.5.0"
36
36
  },
37
- "gitHead": "8bb7768dce76ad5aef46128c4761505725fb0105"
37
+ "gitHead": "86a6d0131a7a32586d4ca8ab79adb45eec7a76c2"
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
 
@@ -70,7 +70,7 @@ class RouterResponse {
70
70
 
71
71
  /**
72
72
  * @param {object} json
73
- * @returns {!RouterResponse}
73
+ * @returns {RouterResponse}
74
74
  */
75
75
  static fromJson(json) {
76
76
  const routerResponse = new RouterResponse();
@@ -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, waypoints);
103
+ return this.createRouterResponseFromJson(jsonResponse, waypoints[0], waypoints[1]);
104
104
  }
105
105
 
106
106
 
@@ -132,22 +132,22 @@ 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
- * @param {Array<Coordinates>} waypoints
135
+ * @param {Coordinates} from
136
+ * @param {Coordinates} to
136
137
  * @returns {!RouterResponse}
137
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
138
139
  */
139
- createRouterResponseFromJson(json, waypoints) {
140
+ createRouterResponseFromJson(json, from, to) {
140
141
 
141
142
  const routerResponse = new RouterResponse();
142
143
  routerResponse.routerName = this.rname;
143
- routerResponse.from = waypoints[0];
144
- routerResponse.to = waypoints[1];
144
+ routerResponse.from = from;
145
+ routerResponse.to = to;
145
146
 
146
147
  if (json.StatusCode !== 200 || !json.Data || !json.Data.length) {
147
148
  return routerResponse;
148
149
  }
149
150
 
150
-
151
151
  // Do not know if it the best approach, but it works...
152
152
  const allJsonTrips = json.Data.reduce((acc, dataObj) => {
153
153
  acc.push(...dataObj.response.trips.Trip.map(trip => ({
@@ -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,23 +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[]} waypoints
67
+ * @param {Coordinates} from
68
+ * @param {Coordinates} to
68
69
  * @returns {!RouterResponse}
69
70
  */
70
- createRouterResponseFromJson(json, waypoints) {
71
+ createRouterResponseFromJson(json, from, to) {
71
72
 
72
73
  const routerResponse = new RouterResponse();
73
74
  routerResponse.routerName = this.rname;
74
- routerResponse.from = waypoints[0];
75
- routerResponse.to = waypoints[1];
75
+ routerResponse.from = from;
76
+ routerResponse.to = to;
76
77
 
77
78
  const { segments: jsonSegments } = json;
78
-
79
79
  if (!jsonSegments) {
80
80
  return routerResponse;
81
81
  }
82
82
 
83
-
84
83
  /** @type {GraphEdge<OsmElement>[]} */
85
84
  const edges = [];
86
85
 
@@ -128,7 +127,7 @@ class DeutscheBahnRemoteRouter extends RemoteRouter {
128
127
  graphItinerary.end = nodes[nodes.length - 1].coords;
129
128
 
130
129
  const points = nodes.map(node => node.coords);
131
- const itinerary = Itinerary.fromOrderedCoordinates(points, waypoints[0], waypoints[1]);
130
+ const itinerary = Itinerary.fromOrderedCoordinates(points, from, to);
132
131
  itinerary.legs[0].steps = StepsGeneration.fromGraphItinerary(graphItinerary);
133
132
  itinerary.legs[0].steps.map((step, idx) => (step._idCoordsInLeg = idx));
134
133
 
@@ -127,7 +127,7 @@ class IdfmRemoteRouter extends RemoteRouter {
127
127
  return routerResponse;
128
128
  }
129
129
 
130
- return this.createRouterResponseFromJson(jsonResponse, waypoints);
130
+ return this.createRouterResponseFromJson(jsonResponse, waypoints[0], waypoints[1]);
131
131
  }
132
132
 
133
133
  /**
@@ -327,21 +327,21 @@ 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
- * @param {Coordinates[]} waypoints
331
- * @returns {?RouterResponse}
330
+ * @param {Coordinates} from
331
+ * @param {Coordinates} to
332
+ * @returns {!RouterResponse}
332
333
  */
333
- createRouterResponseFromJson(json, waypoints) {
334
+ createRouterResponseFromJson(json, from, to) {
334
335
 
335
336
  const routerResponse = new RouterResponse();
336
337
  routerResponse.routerName = this.rname;
337
- routerResponse.from = waypoints[0];
338
- routerResponse.to = waypoints[1];
338
+ routerResponse.from = from;
339
+ routerResponse.to = to;
339
340
 
340
341
  if (!json || !json.journeys) {
341
342
  return routerResponse;
342
343
  }
343
344
 
344
-
345
345
  const timeZone = json.context.timezone;
346
346
 
347
347
  for (const jsonItinerary of json.journeys) {
@@ -351,8 +351,8 @@ class IdfmRemoteRouter extends RemoteRouter {
351
351
  itinerary.duration = jsonItinerary.duration;
352
352
  itinerary.startTime = this.dateStringToTimestamp(jsonItinerary.departure_date_time, timeZone);
353
353
  itinerary.endTime = this.dateStringToTimestamp(jsonItinerary.arrival_date_time, timeZone);
354
- itinerary.from = routerResponse.from;
355
- itinerary.to = routerResponse.to;
354
+ itinerary.from = this.getSectionCoords(jsonItinerary.sections[0]).from;
355
+ itinerary.to = this.getSectionCoords(last(jsonItinerary.sections)).to;
356
356
  itinerary.distance = 0;
357
357
 
358
358
  routerResponse.itineraries.push(itinerary);
@@ -365,7 +365,7 @@ class IdfmRemoteRouter extends RemoteRouter {
365
365
 
366
366
  const leg = new Leg();
367
367
  let existingCoords = [];
368
- const { from, to } = this.getSectionCoords(jsonSection);
368
+ const { from: fromSection, to: toSection } = this.getSectionCoords(jsonSection);
369
369
 
370
370
  leg.distance = 0;
371
371
  leg.mode = routingModeCorrespondance.get(jsonSection.mode);
@@ -375,12 +375,12 @@ class IdfmRemoteRouter extends RemoteRouter {
375
375
 
376
376
  leg.from = {
377
377
  name: jsonSection.from.name,
378
- coords: from
378
+ coords: fromSection
379
379
  };
380
380
 
381
381
  leg.to = {
382
382
  name: jsonSection.to.name,
383
- coords: to
383
+ coords: toSection
384
384
  };
385
385
 
386
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);
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
  /**
@@ -259,16 +261,17 @@ class OsrmRemoteRouter extends RemoteRouter {
259
261
  /**
260
262
  * Generate multi itineraries from OSRM JSON
261
263
  * @param {object} json JSON file provided by OSRM.
262
- * @param {Coordinates[]} waypoints
264
+ * @param {Coordinates} from itinerary start
265
+ * @param {Coordinates} to itinerary end
263
266
  * @param {?string} routingMode [walking|driving|bicycle]
264
- * @returns {!RouterResponse}
267
+ * @returns {?RouterResponse}
265
268
  */
266
- createRouterResponseFromJson(json, waypoints, routingMode = 'walking') {
269
+ createRouterResponseFromJson(json, from, to, routingMode = 'walking') {
267
270
 
268
271
  const routerResponse = new RouterResponse();
269
272
  routerResponse.routerName = this.rname;
270
- routerResponse.from = waypoints[0];
271
- routerResponse.to = waypoints[1];
273
+ routerResponse.from = from;
274
+ routerResponse.to = to;
272
275
 
273
276
  const { routes: jsonRoutes } = json;
274
277
  if (!jsonRoutes) {
@@ -288,8 +291,8 @@ class OsrmRemoteRouter extends RemoteRouter {
288
291
  // itinerary.coords = jsonItinerary.geometry.coordinates.map(jsonToCoordinates);
289
292
  itinerary.distance = jsonItinerary.distance;
290
293
  itinerary.duration = jsonItinerary.duration;
291
- itinerary.from = waypoints[0];
292
- itinerary.to = waypoints[1];
294
+ itinerary.from = from;
295
+ itinerary.to = to;
293
296
 
294
297
  routerResponse.itineraries.push(itinerary);
295
298
 
@@ -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,25 @@ 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
- * @param {Coordinates[]} waypoints
131
- * @returns {?RouterResponse}
130
+ * @param {Coordinates} from
131
+ * @param {Coordinates} to
132
+ * @returns {!RouterResponse}
132
133
  */
133
- createRouterResponseFromJson(json, waypoints) {
134
+ createRouterResponseFromJson(json, from, to) {
134
135
 
135
136
  const routerResponse = new RouterResponse();
136
137
  routerResponse.routerName = this.rname;
137
- routerResponse.from = waypoints[0];
138
- routerResponse.to = waypoints[1];
138
+ routerResponse.from = from;
139
+ routerResponse.to = to;
139
140
 
140
141
  const { plan: jsonPlan } = json;
141
142
  if (!jsonPlan) {
142
143
  return routerResponse;
143
144
  }
144
145
 
146
+ const itinerariesFrom = this.jsonToCoordinates(jsonPlan.from);
147
+ const itinerariesTo = this.jsonToCoordinates(jsonPlan.to);
148
+
145
149
  for (const jsonItinerary of jsonPlan.itineraries) {
146
150
 
147
151
  const itinerary = new Itinerary();
@@ -149,8 +153,8 @@ class OtpRemoteRouter extends RemoteRouter {
149
153
  itinerary.duration = jsonItinerary.duration;
150
154
  itinerary.startTime = jsonItinerary.startTime;
151
155
  itinerary.endTime = jsonItinerary.endTime;
152
- itinerary.from = routerResponse.from;
153
- itinerary.to = routerResponse.to;
156
+ itinerary.from = itinerariesFrom;
157
+ itinerary.to = itinerariesTo;
154
158
 
155
159
  routerResponse.itineraries.push(itinerary);
156
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');
@@ -44,7 +44,7 @@ class WemapMetaRouter {
44
44
  * @param {string} mode see Constants.ROUTING_MODE
45
45
  * @param {Coordinates[]} waypoints
46
46
  * @param {WemapMetaRouterOptions} options
47
- * @returns {!RouterResponse}
47
+ * @returns {RouterResponse}
48
48
  */
49
49
  async getItineraries(mode, waypoints, options) {
50
50