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