@wemap/routers 9.0.0-alpha.1 → 9.0.0-alpha.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.
@@ -1751,7 +1751,7 @@ class CitywayRemoteRouter extends RemoteRouter {
1751
1751
  const jsonResponse = await res.json().catch(() => {
1752
1752
  throw new RemoteRouterServerUnreachable(this.rname, url);
1753
1753
  });
1754
- return this.createRouterResponseFromJson(jsonResponse);
1754
+ return this.createRouterResponseFromJson(jsonResponse, waypoints[0], waypoints[1]);
1755
1755
  }
1756
1756
 
1757
1757
 
@@ -1783,18 +1783,21 @@ class CitywayRemoteRouter extends RemoteRouter {
1783
1783
  /**
1784
1784
  * Generate multi itineraries from Cityway JSON
1785
1785
  * @param {object} json JSON file provided by Cityway.
1786
- * @returns {?RouterResponse}
1786
+ * @param {Coordinates} from
1787
+ * @param {Coordinates} to
1788
+ * @returns {!RouterResponse}
1787
1789
  * @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
1788
1790
  */
1789
- createRouterResponseFromJson(json) {
1790
-
1791
- if (json.StatusCode !== 200 || !json.Data || !json.Data.length) {
1792
- return null;
1793
- }
1791
+ createRouterResponseFromJson(json, from, to) {
1794
1792
 
1795
1793
  const routerResponse = new RouterResponse();
1796
1794
  routerResponse.routerName = this.rname;
1795
+ routerResponse.from = from;
1796
+ routerResponse.to = to;
1797
1797
 
1798
+ if (json.StatusCode !== 200 || !json.Data || !json.Data.length) {
1799
+ return routerResponse;
1800
+ }
1798
1801
 
1799
1802
  // Do not know if it the best approach, but it works...
1800
1803
  const allJsonTrips = json.Data.reduce((acc, dataObj) => {
@@ -1953,9 +1956,6 @@ class CitywayRemoteRouter extends RemoteRouter {
1953
1956
  generateStepsMetadata(itinerary);
1954
1957
  }
1955
1958
 
1956
- routerResponse.from = routerResponse.itineraries[0].from;
1957
- routerResponse.to = routerResponse.itineraries[routerResponse.itineraries.length - 1].to;
1958
-
1959
1959
  return routerResponse;
1960
1960
  }
1961
1961
 
@@ -2094,24 +2094,22 @@ class DeutscheBahnRemoteRouter extends RemoteRouter {
2094
2094
  /**
2095
2095
  * Generate multi itineraries from the DB JSON
2096
2096
  * @param {object} json JSON file provided by the DB.
2097
- * @param {Coordinates} from itinerary start
2098
- * @param {Coordinates} to itinerary end
2099
- * @returns {?RouterResponse}
2097
+ * @param {Coordinates} from
2098
+ * @param {Coordinates} to
2099
+ * @returns {!RouterResponse}
2100
2100
  */
2101
2101
  createRouterResponseFromJson(json, from, to) {
2102
2102
 
2103
- const { segments: jsonSegments } = json;
2104
-
2105
- if (!jsonSegments) {
2106
- return null;
2107
- }
2108
-
2109
2103
  const routerResponse = new RouterResponse();
2110
2104
  routerResponse.routerName = this.rname;
2111
-
2112
2105
  routerResponse.from = from;
2113
2106
  routerResponse.to = to;
2114
2107
 
2108
+ const { segments: jsonSegments } = json;
2109
+ if (!jsonSegments) {
2110
+ return routerResponse;
2111
+ }
2112
+
2115
2113
  /** @type {GraphEdge<OsmElement>[]} */
2116
2114
  const edges = [];
2117
2115
 
@@ -2291,7 +2289,7 @@ class IdfmRemoteRouter extends RemoteRouter {
2291
2289
  return routerResponse;
2292
2290
  }
2293
2291
 
2294
- return this.createRouterResponseFromJson(jsonResponse);
2292
+ return this.createRouterResponseFromJson(jsonResponse, waypoints[0], waypoints[1]);
2295
2293
  }
2296
2294
 
2297
2295
  /**
@@ -2489,19 +2487,20 @@ class IdfmRemoteRouter extends RemoteRouter {
2489
2487
  /**
2490
2488
  * Generate multi itineraries from OTP JSON
2491
2489
  * @param {object} json JSON file provided by OTP.
2492
- * @returns {?RouterResponse}
2490
+ * @param {Coordinates} from
2491
+ * @param {Coordinates} to
2492
+ * @returns {!RouterResponse}
2493
2493
  */
2494
- createRouterResponseFromJson(json) {
2495
-
2496
- if (!json || !json.journeys) {
2497
- return null;
2498
- }
2494
+ createRouterResponseFromJson(json, from, to) {
2499
2495
 
2500
2496
  const routerResponse = new RouterResponse();
2501
2497
  routerResponse.routerName = this.rname;
2498
+ routerResponse.from = from;
2499
+ routerResponse.to = to;
2502
2500
 
2503
- routerResponse.from = this.getSectionCoords(json.journeys[0].sections[0]).from;
2504
- routerResponse.to = this.getSectionCoords(last(json.journeys[0].sections)).to;
2501
+ if (!json || !json.journeys) {
2502
+ return routerResponse;
2503
+ }
2505
2504
 
2506
2505
  const timeZone = json.context.timezone;
2507
2506
 
@@ -2512,8 +2511,8 @@ class IdfmRemoteRouter extends RemoteRouter {
2512
2511
  itinerary.duration = jsonItinerary.duration;
2513
2512
  itinerary.startTime = this.dateStringToTimestamp(jsonItinerary.departure_date_time, timeZone);
2514
2513
  itinerary.endTime = this.dateStringToTimestamp(jsonItinerary.arrival_date_time, timeZone);
2515
- itinerary.from = routerResponse.from;
2516
- itinerary.to = routerResponse.to;
2514
+ itinerary.from = this.getSectionCoords(jsonItinerary.sections[0]).from;
2515
+ itinerary.to = this.getSectionCoords(last(jsonItinerary.sections)).to;
2517
2516
  itinerary.distance = 0;
2518
2517
 
2519
2518
  routerResponse.itineraries.push(itinerary);
@@ -2526,7 +2525,7 @@ class IdfmRemoteRouter extends RemoteRouter {
2526
2525
 
2527
2526
  const leg = new Leg();
2528
2527
  let existingCoords = [];
2529
- const { from, to } = this.getSectionCoords(jsonSection);
2528
+ const { from: fromSection, to: toSection } = this.getSectionCoords(jsonSection);
2530
2529
 
2531
2530
  leg.distance = 0;
2532
2531
  leg.mode = routingModeCorrespondance.get(jsonSection.mode);
@@ -2536,12 +2535,12 @@ class IdfmRemoteRouter extends RemoteRouter {
2536
2535
 
2537
2536
  leg.from = {
2538
2537
  name: jsonSection.from.name,
2539
- coords: from
2538
+ coords: fromSection
2540
2539
  };
2541
2540
 
2542
2541
  leg.to = {
2543
2542
  name: jsonSection.to.name,
2544
- coords: to
2543
+ coords: toSection
2545
2544
  };
2546
2545
 
2547
2546
  // A section can have multiple same coordinates, we need to remove them
@@ -2649,7 +2648,9 @@ class OsrmRemoteRouter extends RemoteRouter {
2649
2648
  throw new RemoteRouterServerUnreachable(this.rname, url);
2650
2649
  });
2651
2650
 
2652
- return this.createRouterResponseFromJson(jsonResponse, waypoints[0], waypoints[1]);
2651
+ const from = waypoints[0];
2652
+ const to = waypoints[waypoints.length - 1];
2653
+ return this.createRouterResponseFromJson(jsonResponse, from, to);
2653
2654
  }
2654
2655
 
2655
2656
  /**
@@ -2861,10 +2862,15 @@ class OsrmRemoteRouter extends RemoteRouter {
2861
2862
  * @returns {?RouterResponse}
2862
2863
  */
2863
2864
  createRouterResponseFromJson(json, from, to, routingMode = 'walking') {
2864
- const { routes: jsonRoutes } = json;
2865
2865
 
2866
+ const routerResponse = new RouterResponse();
2867
+ routerResponse.routerName = this.rname;
2868
+ routerResponse.from = from;
2869
+ routerResponse.to = to;
2870
+
2871
+ const { routes: jsonRoutes } = json;
2866
2872
  if (!jsonRoutes) {
2867
- return null;
2873
+ return routerResponse;
2868
2874
  }
2869
2875
 
2870
2876
  const routingModeCorrespondance = new Map();
@@ -2873,12 +2879,6 @@ class OsrmRemoteRouter extends RemoteRouter {
2873
2879
  routingModeCorrespondance.set('bicycle', 'BIKE');
2874
2880
  const mode = routingModeCorrespondance.get(routingMode) || null;
2875
2881
 
2876
- const routerResponse = new RouterResponse();
2877
- routerResponse.routerName = this.rname;
2878
-
2879
- routerResponse.from = from;
2880
- routerResponse.to = to;
2881
-
2882
2882
  for (const jsonItinerary of jsonRoutes) {
2883
2883
 
2884
2884
  const itinerary = new Itinerary();
@@ -2968,7 +2968,7 @@ class OtpRemoteRouter extends RemoteRouter {
2968
2968
  const jsonResponse = await res.json().catch(() => {
2969
2969
  throw new RemoteRouterServerUnreachable(this.rname, url);
2970
2970
  });
2971
- return this.createRouterResponseFromJson(jsonResponse);
2971
+ return this.createRouterResponseFromJson(jsonResponse, waypoints[0], waypoints[1]);
2972
2972
  }
2973
2973
 
2974
2974
  /**
@@ -3045,21 +3045,24 @@ class OtpRemoteRouter extends RemoteRouter {
3045
3045
  /**
3046
3046
  * Generate multi itineraries from OTP JSON
3047
3047
  * @param {object} json JSON file provided by OTP.
3048
- * @returns {?RouterResponse}
3048
+ * @param {Coordinates} from
3049
+ * @param {Coordinates} to
3050
+ * @returns {!RouterResponse}
3049
3051
  */
3050
- createRouterResponseFromJson(json) {
3052
+ createRouterResponseFromJson(json, from, to) {
3051
3053
 
3052
- const { plan: jsonPlan } = json;
3054
+ const routerResponse = new RouterResponse();
3055
+ routerResponse.routerName = this.rname;
3056
+ routerResponse.from = from;
3057
+ routerResponse.to = to;
3053
3058
 
3059
+ const { plan: jsonPlan } = json;
3054
3060
  if (!jsonPlan) {
3055
- return null;
3061
+ return routerResponse;
3056
3062
  }
3057
3063
 
3058
- const routerResponse = new RouterResponse();
3059
- routerResponse.routerName = this.rname;
3060
-
3061
- routerResponse.from = this.jsonToCoordinates(jsonPlan.from);
3062
- routerResponse.to = this.jsonToCoordinates(jsonPlan.to);
3064
+ const itinerariesFrom = this.jsonToCoordinates(jsonPlan.from);
3065
+ const itinerariesTo = this.jsonToCoordinates(jsonPlan.to);
3063
3066
 
3064
3067
  for (const jsonItinerary of jsonPlan.itineraries) {
3065
3068
 
@@ -3068,8 +3071,8 @@ class OtpRemoteRouter extends RemoteRouter {
3068
3071
  itinerary.duration = jsonItinerary.duration;
3069
3072
  itinerary.startTime = jsonItinerary.startTime;
3070
3073
  itinerary.endTime = jsonItinerary.endTime;
3071
- itinerary.from = routerResponse.from;
3072
- itinerary.to = routerResponse.to;
3074
+ itinerary.from = itinerariesFrom;
3075
+ itinerary.to = itinerariesTo;
3073
3076
 
3074
3077
  routerResponse.itineraries.push(itinerary);
3075
3078