@wemap/routers 12.7.1 → 12.7.3

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/dist/index.mjs CHANGED
@@ -5,7 +5,7 @@ var __publicField = (obj, key, value) => {
5
5
  return value;
6
6
  };
7
7
  import { Level, Coordinates, BoundingBox, UserPosition, Constants, Utils } from "@wemap/geo";
8
- import { diffAngleLines, deg2rad, diffAngle, positiveMod, rad2deg } from "@wemap/maths";
8
+ import { diffAngleLines, deg2rad, diffAngle, positiveMod, rad2deg, roundFactor } from "@wemap/maths";
9
9
  import Logger from "@wemap/logger";
10
10
  import salesman from "@wemap/salesman.js";
11
11
  import { OsmNode, OsmParser } from "@wemap/osm";
@@ -3885,14 +3885,28 @@ class RemoteRouterManager {
3885
3885
  }
3886
3886
  async getItinerariesWithFallback(routerRequest, fallbackStrategy) {
3887
3887
  let itineraries;
3888
+ const errors = [];
3888
3889
  for (const { name, endpointUrl } of fallbackStrategy) {
3889
- itineraries = await this.getItineraries(name, endpointUrl, routerRequest);
3890
- if (itineraries.length) {
3891
- return itineraries;
3890
+ try {
3891
+ itineraries = await this.getItineraries(name, endpointUrl, routerRequest);
3892
+ if (itineraries.length) {
3893
+ return itineraries;
3894
+ }
3895
+ } catch (error) {
3896
+ if (error instanceof RemoteRoutingError) {
3897
+ errors.push({
3898
+ name,
3899
+ endpointUrl,
3900
+ error
3901
+ });
3902
+ } else {
3903
+ throw error;
3904
+ }
3892
3905
  }
3893
3906
  }
3894
- const routersNames = fallbackStrategy.map(({ name }) => name).join(",");
3895
- throw RemoteRoutingError.notFound(routersNames, `Could not find an itinerary with any of following servers: ${routersNames}`);
3907
+ const routersNames = fallbackStrategy.map(({ name }) => name).join(", ");
3908
+ const errorsMessages = errors.map((routerError) => `(${routerError.name}) Could not find an itinerary on endpoint: ${routerError.endpointUrl}. Details: ${routerError.error.message}`).join("\n");
3909
+ throw RemoteRoutingError.notFound(routersNames, errorsMessages);
3896
3910
  }
3897
3911
  }
3898
3912
  const RemoteRouterManager$1 = new RemoteRouterManager();
@@ -4357,6 +4371,148 @@ class ItineraryInfoManager {
4357
4371
  return itineraryInfo;
4358
4372
  }
4359
4373
  }
4374
+ const _InstructionManager = class _InstructionManager {
4375
+ static getTurnInfoFromAngle(_angle) {
4376
+ let direction, directionExtra;
4377
+ const directionAngle = rad2deg(diffAngle(_angle, Math.PI));
4378
+ const directionAngleAbs = Math.abs(directionAngle);
4379
+ if (directionAngleAbs <= 20) {
4380
+ direction = "straight";
4381
+ } else {
4382
+ direction = directionAngle > 0 ? "left" : "right";
4383
+ if (directionAngleAbs < 55) {
4384
+ directionExtra = "slight";
4385
+ } else if (directionAngleAbs > 120) {
4386
+ directionExtra = "sharp";
4387
+ }
4388
+ }
4389
+ return { direction, directionExtra };
4390
+ }
4391
+ static getInfoFromStep(step) {
4392
+ let type, direction, directionExtra, levelChange;
4393
+ if (step.levelChange) {
4394
+ type = "level-change";
4395
+ levelChange = step.levelChange;
4396
+ } else {
4397
+ type = "turn";
4398
+ const turnInfo = _InstructionManager.getTurnInfoFromAngle(step.angle);
4399
+ direction = turnInfo.direction;
4400
+ directionExtra = turnInfo.directionExtra;
4401
+ }
4402
+ return {
4403
+ type,
4404
+ direction,
4405
+ directionExtra,
4406
+ levelChange,
4407
+ name: step.name,
4408
+ indoor: step.coords.level !== null
4409
+ };
4410
+ }
4411
+ // eslint-disable-next-line max-statements, complexity
4412
+ static getInstructionFromStep(step) {
4413
+ var _a, _b;
4414
+ const { direction, directionExtra } = _InstructionManager.getTurnInfoFromAngle(step.angle);
4415
+ const isTurn = direction !== "straight";
4416
+ if (step.lastStep) {
4417
+ if (isTurn && direction === "left") {
4418
+ return "Your destination is on your left";
4419
+ } else if (isTurn && direction === "right") {
4420
+ return "Your destination is on your right";
4421
+ }
4422
+ }
4423
+ let suffix = "";
4424
+ if ((_a = step.extras) == null ? void 0 : _a.isGate) {
4425
+ suffix = ` on gate ${step.name}`;
4426
+ } else if (step.name) {
4427
+ suffix = ` on ${step.name}`;
4428
+ }
4429
+ if (step.levelChange) {
4430
+ if (step.levelChange.direction === "up") {
4431
+ if (step.levelChange.type === "stairs") {
4432
+ return "Go up the stairs";
4433
+ }
4434
+ if (step.levelChange.type === "escalator") {
4435
+ return "Go up the escalator";
4436
+ }
4437
+ if (step.levelChange.type === "elevator") {
4438
+ return "Go up the elevator";
4439
+ }
4440
+ if (step.levelChange.type === "moving walkway") {
4441
+ return "Go up the moving walkway";
4442
+ }
4443
+ return "Go up" + suffix;
4444
+ }
4445
+ if (step.levelChange.direction === "down") {
4446
+ if (step.levelChange.type === "stairs") {
4447
+ return "Go down the stairs";
4448
+ }
4449
+ if (step.levelChange.type === "escalator") {
4450
+ return "Go down the escalator";
4451
+ }
4452
+ if (step.levelChange.type === "elevator") {
4453
+ return "Go down the elevator";
4454
+ }
4455
+ if (step.levelChange.type === "moving walkway") {
4456
+ return "Go down the moving walkway";
4457
+ }
4458
+ return "Go down" + suffix;
4459
+ }
4460
+ if ((_b = step.extras) == null ? void 0 : _b.subwayEntrance) {
4461
+ return `Take exit ${step.extras.subwayEntranceRef}`;
4462
+ }
4463
+ }
4464
+ if (isTurn) {
4465
+ if (direction === "left") {
4466
+ if (directionExtra === "slight") {
4467
+ return "Turn slightly left" + suffix;
4468
+ }
4469
+ return "Turn left" + suffix;
4470
+ }
4471
+ if (direction === "right") {
4472
+ if (directionExtra === "slight") {
4473
+ return "Turn slightly right" + suffix;
4474
+ }
4475
+ return "Turn right" + suffix;
4476
+ }
4477
+ }
4478
+ return "Continue straight";
4479
+ }
4480
+ static getInstructionFromPosition(itineraryInfoManager, position) {
4481
+ const itineraryInfo = itineraryInfoManager.getInfo(position);
4482
+ if (!itineraryInfo) {
4483
+ return null;
4484
+ }
4485
+ if (this.useProposals && itineraryInfo.projection.distanceFromNearestElement > 15) {
4486
+ return "It seems that we are a little bit lost, please start again the localization process";
4487
+ }
4488
+ const { nextStep } = itineraryInfo;
4489
+ if (!nextStep) {
4490
+ return "You are arrived";
4491
+ }
4492
+ const distNextStep = position.distanceTo(nextStep.coords);
4493
+ const distRounded = roundFactor(distNextStep, 5);
4494
+ if (this.useProposals && distNextStep > 10) {
4495
+ return `Continue straight for ${distRounded}m`;
4496
+ }
4497
+ let instruction = _InstructionManager.getInstructionFromStep(nextStep);
4498
+ const stepWithImportantInfo = itineraryInfoManager._steps.find(
4499
+ (step) => step.levelChange && step.number > nextStep.number && step.coords.distanceTo(nextStep.coords) < 10
4500
+ ) || null;
4501
+ if (stepWithImportantInfo && stepWithImportantInfo.levelChange) {
4502
+ const nextBearing = nextStep.coords.bearingTo(stepWithImportantInfo.coords);
4503
+ const { direction } = _InstructionManager.getTurnInfoFromAngle(nextBearing - nextStep.previousBearing);
4504
+ instruction = direction === "straight" ? "Continue straight" : `Turn ${direction}`;
4505
+ const { direction: levelDirection, type: levelType } = stepWithImportantInfo.levelChange;
4506
+ instruction += ` and take the ${levelType} going ${levelDirection}`;
4507
+ }
4508
+ if (distNextStep >= 5) {
4509
+ instruction += ` in ${distRounded}m`;
4510
+ }
4511
+ return instruction;
4512
+ }
4513
+ };
4514
+ __publicField(_InstructionManager, "useProposals", false);
4515
+ let InstructionManager = _InstructionManager;
4360
4516
  export {
4361
4517
  CitywayRemoteRouter$1 as CitywayRemoteRouter,
4362
4518
  CustomGraphMap,
@@ -4369,6 +4525,7 @@ export {
4369
4525
  GraphRouter,
4370
4526
  GraphRouterOptionsBuilder,
4371
4527
  IdfmRemoteRouter$1 as IdfmRemoteRouter,
4528
+ InstructionManager,
4372
4529
  Itinerary,
4373
4530
  ItineraryInfoManager,
4374
4531
  Leg,