@wemap/routers 11.2.4 → 11.3.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/dist/index.js CHANGED
@@ -590,53 +590,91 @@ const _OsmGraph = class extends geo.GeoGraph {
590
590
  return super.getEdgeByName(name);
591
591
  }
592
592
  static fromOsmModel(osmModel, waySelectionFilter = DEFAULT_WAY_SELECTOR) {
593
- const nodes = [];
593
+ const vertices = [];
594
594
  const edges = [];
595
- const nodesCreated = {};
596
- const elevatorNodes = [];
597
- const getOrCreateNode = (osmNode) => {
598
- let node = nodesCreated[osmNode.id];
599
- if (!node) {
600
- node = new geo.GeoGraphVertex(osmNode.coords, { data: osmNode, name: osmNode.tags.name });
601
- nodesCreated[osmNode.id] = node;
602
- nodes.push(node);
603
- if (osmNode.tags.highway === "elevator") {
604
- elevatorNodes.push(node);
595
+ const elevatorVertices = [];
596
+ const getOrCreateVertex = (osmNode, forceLevel = null) => {
597
+ let vertex = vertices.find(
598
+ (vertex2) => vertex2.data.id === osmNode.id && (forceLevel !== null ? geo.Level.intersect(vertex2.coords.level, forceLevel) : true)
599
+ ) || null;
600
+ if (vertex) {
601
+ return vertex;
602
+ }
603
+ if (vertices.find((v) => v.data.id === osmNode.id)) {
604
+ throw `An error occured because repeat_on as not been set on node ${osmNode.coords} for level ${forceLevel}`;
605
+ }
606
+ let levelsToGenerate = [null];
607
+ if ("level" in osmNode.tags) {
608
+ levelsToGenerate = [osmNode.coords.level];
609
+ if ("repeat_on" in osmNode.tags) {
610
+ levelsToGenerate.push(...osmNode.tags.repeat_on.split(";").map(geo.Level.fromString));
611
+ }
612
+ } else if (forceLevel !== null) {
613
+ levelsToGenerate = [forceLevel];
614
+ }
615
+ levelsToGenerate.forEach((level) => {
616
+ const newVertex = new geo.GeoGraphVertex(osmNode.coords.clone(), { data: osmNode, name: osmNode.tags.name });
617
+ newVertex.coords.level = level;
618
+ vertices.push(newVertex);
619
+ if (geo.Level.intersect(newVertex.coords.level, forceLevel) || forceLevel === null && "level" in osmNode.tags) {
620
+ vertex = newVertex;
605
621
  }
622
+ });
623
+ if (osmNode.tags.highway === "elevator" && vertex) {
624
+ elevatorVertices.push(vertex);
606
625
  }
607
- return node;
626
+ return vertex;
608
627
  };
609
628
  osmModel.ways.forEach((way) => {
610
629
  if (!waySelectionFilter(way)) {
611
630
  return;
612
631
  }
613
- let firstNode = getOrCreateNode(way.nodes[0]);
614
- for (let i = 1; i < way.nodes.length; i++) {
615
- const secondNode = getOrCreateNode(way.nodes[i]);
616
- const edge = new geo.GeoGraphEdge(
617
- firstNode,
618
- secondNode,
619
- { data: way, name: way.tags.name, level: way.level }
620
- );
621
- _OsmGraph.manageOneWay(edge, way);
622
- edges.push(edge);
623
- firstNode = secondNode;
632
+ let levelsToGenerate = [null];
633
+ if ("level" in way.tags) {
634
+ levelsToGenerate = [way.level];
635
+ if ("repeat_on" in way.tags) {
636
+ levelsToGenerate.push(...way.tags.repeat_on.split(";").map(geo.Level.fromString));
637
+ }
624
638
  }
639
+ levelsToGenerate.forEach((level) => {
640
+ let firstVertex = getOrCreateVertex(way.nodes[0], level);
641
+ for (let i = 1; i < way.nodes.length; i++) {
642
+ const secondVertex = getOrCreateVertex(way.nodes[i], level);
643
+ const edge = new geo.GeoGraphEdge(
644
+ firstVertex,
645
+ secondVertex,
646
+ { data: way, name: way.tags.name, level }
647
+ );
648
+ _OsmGraph.manageOneWay(edge, way);
649
+ edges.push(edge);
650
+ firstVertex = secondVertex;
651
+ }
652
+ });
625
653
  });
626
654
  osmModel.ways.filter((way) => way.isElevator).forEach((way) => {
627
- way.nodes.forEach((node) => {
628
- const connectedWays = node.ways.filter((otherWay) => otherWay != way);
629
- if (connectedWays.length) {
630
- const graphVertex = getOrCreateNode(node);
631
- graphVertex.data.tags.highway = "elevator";
632
- elevatorNodes.push(graphVertex);
633
- }
655
+ const entryVertices = way.nodes.map((node) => vertices.filter((v) => v.data.id === node.id)).flat();
656
+ const elevatorLevel = entryVertices.reduce((acc, v) => geo.Level.union(acc, v.coords.level), null);
657
+ const elevatorCenter = way.nodes.reduce((acc, node) => [acc[0] + node.coords.lat, acc[1] + node.coords.lng], [0, 0]).map((val) => val / way.nodes.length);
658
+ const elevatorCenterCoords = new geo.Coordinates(elevatorCenter[0], elevatorCenter[1], null, elevatorLevel);
659
+ const elevatorCenterVertex = new geo.GeoGraphVertex(elevatorCenterCoords, { data: way, name: way.tags.name });
660
+ vertices.push(elevatorCenterVertex);
661
+ entryVertices.forEach((entryVertex) => {
662
+ const newEdge = new geo.GeoGraphEdge(
663
+ elevatorCenterVertex,
664
+ entryVertex,
665
+ {
666
+ level: geo.Level.intersection(elevatorCenterVertex.coords.level, entryVertex.coords.level),
667
+ data: way
668
+ }
669
+ );
670
+ edges.push(newEdge);
634
671
  });
672
+ elevatorVertices.push(elevatorCenterVertex);
635
673
  });
636
- elevatorNodes.forEach((node) => {
637
- _OsmGraph.createNodesAndEdgesFromElevator(nodes, edges, node);
674
+ elevatorVertices.forEach((node) => {
675
+ _OsmGraph.createNodesAndEdgesFromElevator(vertices, edges, node);
638
676
  });
639
- return new _OsmGraph(nodes, edges, true);
677
+ return new _OsmGraph(vertices, edges, true);
640
678
  }
641
679
  static manageOneWay(edge, way) {
642
680
  const { highway, oneway, conveying } = way.tags;
@@ -647,81 +685,58 @@ const _OsmGraph = class extends geo.GeoGraph {
647
685
  edge.vertex2 = tmpNode;
648
686
  }
649
687
  }
650
- static createNodesAndEdgesFromElevator(nodes, edges, elevatorNode) {
651
- const createdNodes = [];
688
+ static createNodesAndEdgesFromElevator(vertices, edges, elevatorVertex) {
689
+ const createdVertices = [];
652
690
  const getOrCreateLevelVertex = (level) => {
653
- let levelVertex = createdNodes.find(({ coords }) => geo.Level.equals(level, coords.level));
691
+ let levelVertex = createdVertices.find(({ coords }) => geo.Level.equals(level, coords.level));
654
692
  if (!levelVertex) {
655
- levelVertex = new geo.GeoGraphVertex(elevatorNode.coords.clone(), {
656
- data: elevatorNode.data,
657
- name: `${elevatorNode.name} (elevator lvl: ${level})`
693
+ levelVertex = new geo.GeoGraphVertex(elevatorVertex.coords.clone(), {
694
+ data: elevatorVertex.data,
695
+ name: `${elevatorVertex.name} (elevator lvl: ${level})`
658
696
  });
659
697
  levelVertex.coords.level = level;
660
- createdNodes.push(levelVertex);
661
- nodes.push(levelVertex);
698
+ createdVertices.push(levelVertex);
699
+ vertices.push(levelVertex);
662
700
  }
663
701
  return levelVertex;
664
702
  };
665
- elevatorNode.edges.forEach((edge) => {
703
+ elevatorVertex.edges.forEach((edge) => {
666
704
  if (geo.Level.isRange(edge.level)) {
667
705
  throw new Error("Cannot handle this elevator edge due to ambiguity");
668
706
  }
669
707
  const levelVertex = getOrCreateLevelVertex(edge.level);
670
- if (edge.vertex1 === elevatorNode) {
708
+ if (edge.vertex1 === elevatorVertex) {
671
709
  edge.vertex1 = levelVertex;
672
710
  } else {
673
711
  edge.vertex2 = levelVertex;
674
712
  }
675
- levelVertex.edges.push(edge);
676
713
  });
677
- for (let i = 0; i < createdNodes.length; i++) {
678
- for (let j = i + 1; j < createdNodes.length; j++) {
679
- const createdNode1 = createdNodes[i];
680
- const createdNode2 = createdNodes[j];
681
- if (createdNode1.coords.level === null || createdNode2.coords.level === null) {
714
+ for (let i = 0; i < createdVertices.length; i++) {
715
+ for (let j = i + 1; j < createdVertices.length; j++) {
716
+ const createdVertex1 = createdVertices[i];
717
+ const createdVertex2 = createdVertices[j];
718
+ if (createdVertex1.coords.level === null || createdVertex2.coords.level === null) {
682
719
  continue;
683
720
  }
684
- const minLevel = Math.min(createdNode1.coords.level, createdNode2.coords.level);
685
- const maxLevel = Math.max(createdNode1.coords.level, createdNode2.coords.level);
721
+ const minLevel = Math.min(createdVertex1.coords.level, createdVertex2.coords.level);
722
+ const maxLevel = Math.max(createdVertex1.coords.level, createdVertex2.coords.level);
686
723
  const newEdge = new geo.GeoGraphEdge(
687
- createdNode1,
688
- createdNode2,
689
- { data: elevatorNode.data, name: elevatorNode.name, level: [minLevel, maxLevel] }
724
+ createdVertex1,
725
+ createdVertex2,
726
+ { data: elevatorVertex.data, name: elevatorVertex.name, level: [minLevel, maxLevel] }
690
727
  );
691
728
  edges.push(newEdge);
692
729
  }
693
730
  }
694
- const elevatorNodeIndex = nodes.indexOf(elevatorNode);
731
+ const elevatorNodeIndex = vertices.indexOf(elevatorVertex);
695
732
  if (elevatorNodeIndex > -1) {
696
- nodes.splice(elevatorNodeIndex, 1);
733
+ vertices.splice(elevatorNodeIndex, 1);
697
734
  }
698
735
  }
699
736
  };
700
737
  let OsmGraph = _OsmGraph;
701
738
  __publicField(OsmGraph, "HIGHWAYS_PEDESTRIANS", HIGHWAYS_PEDESTRIANS);
702
739
  __publicField(OsmGraph, "DEFAULT_WAY_SELECTOR", DEFAULT_WAY_SELECTOR);
703
- const DEFAULT_OPTIONS = Object.assign({}, geo.GeoGraphRouter.DEFAULT_OPTIONS, {
704
- weightEdgeFn: (edge) => {
705
- if (edge.data.isElevator) {
706
- return 90;
707
- }
708
- let duration = getDurationFromLength(edge.length, 4);
709
- if (edge.data instanceof osm.OsmWay && edge.data.areStairs) {
710
- duration *= 3;
711
- }
712
- return duration;
713
- },
714
- acceptEdgeFn: (edge) => {
715
- const accessTag = edge.data.tags.access;
716
- return typeof accessTag === "undefined" || accessTag === "yes";
717
- }
718
- });
719
- const WITHOUT_STAIRS_OPTIONS = Object.assign({}, DEFAULT_OPTIONS, {
720
- acceptEdgeFn: (edge) => edge.data.tags.highway !== "steps"
721
- });
722
- const DEFAULT_TRIP_OPTIONS = Object.assign({}, {
723
- tspTempCoeff: 0.99
724
- }, geo.GeoGraphRouter.DEFAULT_OPTIONS);
725
740
  const buildStepsRules = (graphItinerary) => (currentCoords, nextCoords, previousStep) => {
726
741
  var _a, _b, _c, _d, _e;
727
742
  const edges = graphItinerary.edges;
@@ -739,10 +754,12 @@ const buildStepsRules = (graphItinerary) => (currentCoords, nextCoords, previous
739
754
  let levelChangeType = null;
740
755
  if (edge.data.isElevator) {
741
756
  levelChangeType = "elevator";
742
- } else if (edge.data.isConveying) {
757
+ } else if (edge.data instanceof osm.OsmWay && edge.data.isConveying && edge.data.areStairs) {
743
758
  levelChangeType = "conveyor";
744
759
  } else if (edge.data instanceof osm.OsmWay && edge.data.areStairs) {
745
760
  levelChangeType = "stairs";
761
+ } else if (edge.data instanceof osm.OsmWay && edge.data.isConveying) {
762
+ levelChangeType = "moving walkway";
746
763
  }
747
764
  let forceLevelChange;
748
765
  const edgeTags = edge.data.tags || {};
@@ -782,17 +799,17 @@ const buildStepsRules = (graphItinerary) => (currentCoords, nextCoords, previous
782
799
  ...forceEndOfLevelChange && { forceEndOfLevelChange }
783
800
  };
784
801
  };
785
- class WemapOsmRouter extends geo.GeoGraphRouter {
802
+ const _WemapOsmRouter = class extends geo.GeoGraphRouter {
786
803
  constructor(graph) {
787
804
  super(graph);
788
805
  }
789
806
  static get rname() {
790
807
  return "wemap-osm";
791
808
  }
792
- getShortestPath(start, end, options = DEFAULT_OPTIONS) {
809
+ getShortestPath(start, end, options = _WemapOsmRouter.DEFAULT_OPTIONS) {
793
810
  return super.getShortestPath(start, end, options);
794
811
  }
795
- getItinerary(start, end, options = DEFAULT_OPTIONS) {
812
+ getItinerary(start, end, options = _WemapOsmRouter.DEFAULT_OPTIONS) {
796
813
  const graphItinerary = this.getShortestPath(start, end, options);
797
814
  return Itinerary.fromGraphItinerary(graphItinerary, "WALK", buildStepsRules(graphItinerary));
798
815
  }
@@ -812,7 +829,7 @@ class WemapOsmRouter extends geo.GeoGraphRouter {
812
829
  }
813
830
  return { direction, directionExtra };
814
831
  }
815
- getShortestTrip(waypoints, options = DEFAULT_TRIP_OPTIONS) {
832
+ getShortestTrip(waypoints, options = _WemapOsmRouter.DEFAULT_TRIP_OPTIONS) {
816
833
  const points = waypoints.map((waypoint) => {
817
834
  const point = new salesman__default.default.Point(0, 0);
818
835
  point.coords = waypoint;
@@ -846,7 +863,7 @@ class WemapOsmRouter extends geo.GeoGraphRouter {
846
863
  }
847
864
  return orderedItineraries;
848
865
  }
849
- getTripItinerary(waypoints, options = DEFAULT_TRIP_OPTIONS) {
866
+ getTripItinerary(waypoints, options = _WemapOsmRouter.DEFAULT_TRIP_OPTIONS) {
850
867
  const shortestTrip = this.getShortestTrip(waypoints, options);
851
868
  return new Itinerary({
852
869
  from: shortestTrip[0].start,
@@ -854,9 +871,49 @@ class WemapOsmRouter extends geo.GeoGraphRouter {
854
871
  legs: shortestTrip.map((graphItinerary) => Leg.fromGraphItinerary(graphItinerary))
855
872
  });
856
873
  }
857
- }
858
- __publicField(WemapOsmRouter, "DEFAULT_OPTIONS", DEFAULT_OPTIONS);
859
- __publicField(WemapOsmRouter, "WITHOUT_STAIRS_OPTIONS", WITHOUT_STAIRS_OPTIONS);
874
+ static buildOptions(options) {
875
+ const weightEdgeFn = (edge) => {
876
+ var _a;
877
+ if ((_a = edge.data) == null ? void 0 : _a.isElevator) {
878
+ return 90;
879
+ }
880
+ let duration = getDurationFromLength(edge.length, 4);
881
+ if (edge.data instanceof osm.OsmWay && edge.data.areStairs) {
882
+ duration *= 3;
883
+ }
884
+ return duration;
885
+ };
886
+ const acceptEdgeFn = (edge) => {
887
+ var _a, _b, _c;
888
+ const accessTag = (_b = (_a = edge.data) == null ? void 0 : _a.tags) == null ? void 0 : _b.access;
889
+ if (typeof accessTag !== "undefined" && accessTag !== "yes")
890
+ return false;
891
+ if (typeof (options == null ? void 0 : options.useStairs) !== "undefined" && !(options == null ? void 0 : options.useStairs) && edge.data instanceof osm.OsmWay && edge.data.areStairs && !edge.data.isConveying)
892
+ return false;
893
+ if (typeof (options == null ? void 0 : options.useEscalator) !== "undefined" && !(options == null ? void 0 : options.useEscalator) && edge.data instanceof osm.OsmWay && edge.data.areStairs && edge.data.isConveying)
894
+ return false;
895
+ if (typeof (options == null ? void 0 : options.useElevator) !== "undefined" && !(options == null ? void 0 : options.useElevator) && ((_c = edge.data) == null ? void 0 : _c.isElevator))
896
+ return false;
897
+ return true;
898
+ };
899
+ return {
900
+ weightEdgeFn,
901
+ acceptEdgeFn,
902
+ projectionMaxDistance: geo.GeoGraphRouter.DEFAULT_OPTIONS.projectionMaxDistance
903
+ };
904
+ }
905
+ static buildTripOptions(options) {
906
+ return Object.assign(
907
+ {},
908
+ _WemapOsmRouter.buildOptions(options),
909
+ { tspTempCoeff: 0.99 }
910
+ );
911
+ }
912
+ };
913
+ let WemapOsmRouter = _WemapOsmRouter;
914
+ __publicField(WemapOsmRouter, "DEFAULT_OPTIONS", _WemapOsmRouter.buildOptions());
915
+ __publicField(WemapOsmRouter, "WITHOUT_STAIRS_OPTIONS", _WemapOsmRouter.buildOptions({ useStairs: false }));
916
+ __publicField(WemapOsmRouter, "DEFAULT_TRIP_OPTIONS", _WemapOsmRouter.buildTripOptions());
860
917
  class RemoteRouter {
861
918
  }
862
919
  class RemoteRouterServerUnreachable extends Error {
@@ -1858,8 +1915,8 @@ class WemapMultiRouter {
1858
1915
  }
1859
1916
  return ioMapsToTest;
1860
1917
  }
1861
- convertOptionsToWemapOsmOptions(options) {
1862
- const outputOptions = !options || !("useStairs" in options) || options.useStairs ? WemapOsmRouter.DEFAULT_OPTIONS : WemapOsmRouter.WITHOUT_STAIRS_OPTIONS;
1918
+ convertOptionsToWemapOsmOptions(options = null) {
1919
+ const outputOptions = WemapOsmRouter.buildOptions(options ? options : {});
1863
1920
  if (typeof (options == null ? void 0 : options.tspTempCoeff) !== "undefined") {
1864
1921
  outputOptions.tspTempCoeff = options.tspTempCoeff;
1865
1922
  }