@wemap/routers 12.0.0-alpha.9 → 12.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.
@@ -23,10 +23,10 @@
23
23
  },
24
24
  "distance": 13.06248469300833,
25
25
  "duration": 9.404988978965996,
26
- "mode": "WALK",
26
+ "transitMode": "WALK",
27
27
  "legs": [
28
28
  {
29
- "mode": "WALK",
29
+ "transitMode": "WALK",
30
30
  "start": {
31
31
  "coords": [
32
32
  48.8725992,
@@ -26,10 +26,10 @@
26
26
  },
27
27
  "distance": 1623.567209158486,
28
28
  "duration": 1188.9963905941102,
29
- "mode": "WALK",
29
+ "transitMode": "WALK",
30
30
  "legs": [
31
31
  {
32
- "mode": "WALK",
32
+ "transitMode": "WALK",
33
33
  "start": {
34
34
  "coords": [
35
35
  48.8725992,
@@ -93,7 +93,7 @@
93
93
  ]
94
94
  },
95
95
  {
96
- "mode": "WALK",
96
+ "transitMode": "WALK",
97
97
  "start": {
98
98
  "coords": [
99
99
  48.872668,
@@ -548,7 +548,7 @@
548
548
  ]
549
549
  },
550
550
  {
551
- "mode": "WALK",
551
+ "transitMode": "WALK",
552
552
  "start": {
553
553
  "coords": [
554
554
  48.8774156,
@@ -24,10 +24,10 @@
24
24
  },
25
25
  "distance": 24.658068219028713,
26
26
  "duration": 17.641809117700674,
27
- "mode": "WALK",
27
+ "transitMode": "WALK",
28
28
  "legs": [
29
29
  {
30
- "mode": "WALK",
30
+ "transitMode": "WALK",
31
31
  "start": {
32
32
  "coords": [
33
33
  48.8725992,
@@ -91,7 +91,7 @@
91
91
  ]
92
92
  },
93
93
  {
94
- "mode": "WALK",
94
+ "transitMode": "WALK",
95
95
  "start": {
96
96
  "coords": [
97
97
  48.872668,
@@ -24,10 +24,10 @@
24
24
  },
25
25
  "distance": 18.278520725857963,
26
26
  "duration": 13.108534922617736,
27
- "mode": "WALK",
27
+ "transitMode": "WALK",
28
28
  "legs": [
29
29
  {
30
- "mode": "WALK",
30
+ "transitMode": "WALK",
31
31
  "start": {
32
32
  "coords": [
33
33
  48.872645,
@@ -85,7 +85,7 @@
85
85
  ]
86
86
  },
87
87
  {
88
- "mode": "WALK",
88
+ "transitMode": "WALK",
89
89
  "start": {
90
90
  "coords": [
91
91
  48.87262168252,
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "distance": 29.8,
22
22
  "duration": 23.5,
23
- "mode": "WALK",
23
+ "transitMode": "WALK",
24
24
  "legs": [
25
25
  {
26
- "mode": "WALK",
26
+ "transitMode": "WALK",
27
27
  "start": {
28
28
  "coords": [
29
29
  48.872645,
package/dist/index.js CHANGED
@@ -14,6 +14,15 @@ const osm = require("@wemap/osm");
14
14
  const Polyline = require("@mapbox/polyline");
15
15
  const pointInPolygon = require("@turf/boolean-point-in-polygon");
16
16
  const convexHullFn = require("@turf/convex");
17
+ function routerRequestToJson(routerRequest) {
18
+ const { origin, destination, waypoints, ...rest } = routerRequest;
19
+ return {
20
+ origin: origin.toJson(),
21
+ destination: destination.toJson(),
22
+ ...waypoints && { waypoints: waypoints.map((w) => w.toJson()) },
23
+ ...rest
24
+ };
25
+ }
17
26
  const _Edge = class _Edge {
18
27
  constructor(vertex1, vertex2, properties = {}) {
19
28
  __publicField(this, "id", _Edge.currentUniqueId++);
@@ -510,7 +519,7 @@ class Leg {
510
519
  start,
511
520
  end,
512
521
  coords,
513
- mode,
522
+ transitMode,
514
523
  duration,
515
524
  startTime,
516
525
  endTime,
@@ -521,7 +530,7 @@ class Leg {
521
530
  __publicField(this, "end");
522
531
  __publicField(this, "coords");
523
532
  __publicField(this, "distance");
524
- __publicField(this, "mode");
533
+ __publicField(this, "transitMode");
525
534
  __publicField(this, "duration");
526
535
  __publicField(this, "startTime");
527
536
  __publicField(this, "endTime");
@@ -536,7 +545,7 @@ class Leg {
536
545
  coords: end.coords
537
546
  };
538
547
  this.coords = coords;
539
- this.mode = mode;
548
+ this.transitMode = transitMode;
540
549
  this.distance = geo.Utils.calcDistance(coords);
541
550
  this.duration = typeof duration === "number" ? duration : getDurationFromLength(this.distance);
542
551
  this.startTime = typeof startTime === "number" ? startTime : null;
@@ -545,14 +554,14 @@ class Leg {
545
554
  this.steps = Array.isArray(steps) ? steps : new StepsBuilder().setStart(start.coords).setEnd(end.coords).setPathCoords(coords).build();
546
555
  }
547
556
  isPublicTransport() {
548
- return isTransitModePublicTransport(this.mode);
557
+ return isTransitModePublicTransport(this.transitMode);
549
558
  }
550
559
  toGraph() {
551
560
  return Graph.fromCoordinatesSegments([this.coords]);
552
561
  }
553
562
  static equals(obj1, obj2) {
554
563
  var _a, _b;
555
- const intermediate = obj1.mode === obj2.mode && Math.abs(obj1.duration - obj2.duration) <= 0.05 && obj1.startTime === obj2.startTime && obj1.endTime === obj2.endTime && obj1.start.name === obj2.start.name && obj1.start.coords.equals(obj2.start.coords) && obj1.end.name === obj2.end.name && obj1.end.coords.equals(obj2.end.coords) && obj1.coords.length === obj2.coords.length && (obj1.steps === obj2.steps || ((_a = obj1.steps) == null ? void 0 : _a.length) === ((_b = obj2.steps) == null ? void 0 : _b.length));
564
+ const intermediate = obj1.transitMode === obj2.transitMode && Math.abs(obj1.duration - obj2.duration) <= 0.05 && obj1.startTime === obj2.startTime && obj1.endTime === obj2.endTime && obj1.start.name === obj2.start.name && obj1.start.coords.equals(obj2.start.coords) && obj1.end.name === obj2.end.name && obj1.end.coords.equals(obj2.end.coords) && obj1.coords.length === obj2.coords.length && (obj1.steps === obj2.steps || ((_a = obj1.steps) == null ? void 0 : _a.length) === ((_b = obj2.steps) == null ? void 0 : _b.length));
556
565
  if (!intermediate) {
557
566
  return false;
558
567
  }
@@ -582,7 +591,7 @@ class Leg {
582
591
  }
583
592
  toJson() {
584
593
  return {
585
- mode: this.mode,
594
+ transitMode: this.transitMode,
586
595
  start: {
587
596
  coords: this.start.coords.toCompressedJson(),
588
597
  ...this.start.name && { name: this.start.name }
@@ -591,6 +600,7 @@ class Leg {
591
600
  coords: this.end.coords.toCompressedJson(),
592
601
  ...this.end.name && { name: this.end.name }
593
602
  },
603
+ distance: Number(this.distance.toFixed(1)),
594
604
  duration: Number(this.duration.toFixed(1)),
595
605
  coords: this.coords.map((coords) => coords.toCompressedJson()),
596
606
  steps: this.steps.map(stepToJson),
@@ -615,13 +625,13 @@ class Leg {
615
625
  }));
616
626
  return leg;
617
627
  }
618
- static fromGraphRoute(graphRoute, mode = "WALK") {
628
+ static fromGraphRoute(graphRoute, transitMode = "WALK") {
619
629
  return new Leg({
620
630
  start: { coords: graphRoute.start },
621
631
  end: { coords: graphRoute.end },
622
632
  coords: graphRoute.vertices.map((vertex) => vertex.coords),
623
633
  duration: graphRoute.edgesWeights.reduce((acc, weight) => acc + weight, 0),
624
- mode,
634
+ transitMode,
625
635
  steps: new StepsBuilder().setGraphRoute(graphRoute).build()
626
636
  });
627
637
  }
@@ -651,7 +661,7 @@ class Itinerary {
651
661
  __publicField(this, "destination");
652
662
  __publicField(this, "duration");
653
663
  __publicField(this, "legs");
654
- __publicField(this, "_mode", null);
664
+ __publicField(this, "_transitMode", null);
655
665
  __publicField(this, "startTime");
656
666
  __publicField(this, "endTime");
657
667
  __publicField(this, "_coords", null);
@@ -679,30 +689,30 @@ class Itinerary {
679
689
  get steps() {
680
690
  return this.legs.map((leg) => leg.steps).flat();
681
691
  }
682
- set mode(_) {
692
+ set transitMode(_) {
683
693
  throw new Error("Itinerary.mode cannot be set. They are calculated from Itinerary.legs.");
684
694
  }
685
- get mode() {
686
- if (!this._mode) {
695
+ get transitMode() {
696
+ if (!this._transitMode) {
687
697
  let isPublicTransport = false;
688
698
  let isBicycle = false;
689
699
  let isDriving = false;
690
700
  this.legs.forEach((leg) => {
691
- isPublicTransport = isPublicTransport || isTransitModePublicTransport(leg.mode);
692
- isBicycle = isBicycle || leg.mode === "BIKE";
693
- isDriving = isDriving || leg.mode === "CAR";
701
+ isPublicTransport = isPublicTransport || isTransitModePublicTransport(leg.transitMode);
702
+ isBicycle = isBicycle || leg.transitMode === "BIKE";
703
+ isDriving = isDriving || leg.transitMode === "CAR";
694
704
  });
695
705
  if (isPublicTransport) {
696
- this._mode = "MULTI";
706
+ this._transitMode = "MULTI";
697
707
  } else if (isDriving) {
698
- this._mode = "CAR";
708
+ this._transitMode = "CAR";
699
709
  } else if (isBicycle) {
700
- this._mode = "BIKE";
710
+ this._transitMode = "BIKE";
701
711
  } else {
702
- this._mode = "WALK";
712
+ this._transitMode = "WALK";
703
713
  }
704
714
  }
705
- return this._mode;
715
+ return this._transitMode;
706
716
  }
707
717
  set distance(_) {
708
718
  throw new Error("Itinerary.distance cannot be set. They are calculated from Itinerary.legs.");
@@ -744,13 +754,13 @@ class Itinerary {
744
754
  /**
745
755
  * Convert ordered Coordinates to Itinerary
746
756
  */
747
- static fromOrderedCoordinates(coords, origin, destination, mode = "WALK") {
757
+ static fromOrderedCoordinates(coords, origin, destination, transitMode = "WALK") {
748
758
  const steps = new StepsBuilder().setPathCoords(coords).setStart(origin).setEnd(destination).build();
749
759
  const leg = new Leg({
750
760
  start: { coords: origin },
751
761
  end: { coords: destination },
752
762
  coords,
753
- mode,
763
+ transitMode,
754
764
  steps
755
765
  });
756
766
  return new Itinerary({ origin, destination, legs: [leg] });
@@ -774,8 +784,9 @@ class Itinerary {
774
784
  return {
775
785
  origin: this.origin.toJson(),
776
786
  destination: this.destination.toJson(),
787
+ distance: Number(this.distance.toFixed(1)),
777
788
  duration: Number(this.duration.toFixed(1)),
778
- mode: this.mode,
789
+ transitMode: this.transitMode,
779
790
  legs: this.legs.map((leg) => leg.toJson()),
780
791
  ...this.startTime !== null && { startTime: this.startTime },
781
792
  ...this.endTime !== null && { endTime: this.endTime }
@@ -791,8 +802,8 @@ class Itinerary {
791
802
  endTime: json.endTime
792
803
  });
793
804
  }
794
- static fromGraphRoute(graphRoute, mode = "WALK") {
795
- const leg = Leg.fromGraphRoute(graphRoute, mode);
805
+ static fromGraphRoute(graphRoute, transitMode = "WALK") {
806
+ const leg = Leg.fromGraphRoute(graphRoute, transitMode);
796
807
  return new Itinerary({
797
808
  origin: graphRoute.start,
798
809
  destination: graphRoute.end,
@@ -831,13 +842,34 @@ class Itinerary {
831
842
  }
832
843
  }
833
844
  toGeoJson() {
834
- return {
845
+ const transformToPoint = (point, name) => ({
835
846
  type: "Feature",
836
- properties: {},
847
+ properties: { name, level: point.level },
837
848
  geometry: {
838
- type: "MultiLineString",
839
- coordinates: this.legs.map((leg) => leg.coords.map(({ lat, lng }) => [lng, lat]))
849
+ type: "Point",
850
+ coordinates: [point.lng, point.lat]
840
851
  }
852
+ });
853
+ const transformToMultiLineStrings = (segments, level) => {
854
+ return {
855
+ type: "Feature",
856
+ properties: { level, name: level == null ? void 0 : level.toString() },
857
+ geometry: {
858
+ type: "MultiLineString",
859
+ coordinates: segments.map((s) => s.map(({ lat, lng }) => [lng, lat]))
860
+ }
861
+ };
862
+ };
863
+ const levelsOfItinerary = [...new Set(this.coords.map((c) => geo.Level.toString(c.level)))].map(geo.Level.fromString);
864
+ const segmentsSplitted = levelsOfItinerary.map((loi) => [loi, geo.Utils.createSegmentsAtLevel(this.coords, loi, true)]);
865
+ const multiLineStrings = segmentsSplitted.map(([loi, segments]) => transformToMultiLineStrings(segments, loi));
866
+ return {
867
+ type: "FeatureCollection",
868
+ features: [
869
+ transformToPoint(this.origin, "origin"),
870
+ transformToPoint(this.destination, "destination"),
871
+ ...multiLineStrings
872
+ ]
841
873
  };
842
874
  }
843
875
  /**
@@ -991,6 +1023,54 @@ class GraphRouterEngineResults {
991
1023
  }
992
1024
  return new GraphRoute(this.source.coords, to.coords, path, edges, edgesWeights);
993
1025
  }
1026
+ toGeoJson(graph) {
1027
+ return {
1028
+ type: "FeatureCollection",
1029
+ features: [
1030
+ ...graph.vertices.map((v) => {
1031
+ const prev = this.prev[v.id];
1032
+ const dist = this.dist[v.id];
1033
+ const isSource = v === this.source;
1034
+ const isTarget = this.targets.includes(v);
1035
+ const level = v.coords.level;
1036
+ return {
1037
+ type: "Feature",
1038
+ properties: {
1039
+ id: v.id,
1040
+ ...prev && { prev: prev.id },
1041
+ dist: dist === Number.MAX_VALUE ? "infinity" : dist,
1042
+ ...isSource && { name: "source" },
1043
+ ...isTarget && { name: "target" },
1044
+ ...level !== null && { level },
1045
+ ...v.properties
1046
+ },
1047
+ geometry: {
1048
+ type: "Point",
1049
+ coordinates: [v.coords.lng, v.coords.lat]
1050
+ }
1051
+ };
1052
+ }),
1053
+ ...graph.edges.map((e) => {
1054
+ const level = e.level;
1055
+ return {
1056
+ type: "Feature",
1057
+ properties: {
1058
+ id: e.id,
1059
+ ...level !== null && { level },
1060
+ ...e.properties
1061
+ },
1062
+ geometry: {
1063
+ type: "LineString",
1064
+ coordinates: [
1065
+ [e.vertex1.coords.lng, e.vertex1.coords.lat],
1066
+ [e.vertex2.coords.lng, e.vertex2.coords.lat]
1067
+ ]
1068
+ }
1069
+ };
1070
+ })
1071
+ ]
1072
+ };
1073
+ }
994
1074
  }
995
1075
  class GraphRouterEngine {
996
1076
  constructor(graph) {
@@ -1253,7 +1333,7 @@ class NoRouteFoundError extends Error {
1253
1333
  const _OsmGraphUtils = class _OsmGraphUtils {
1254
1334
  static parseNodeProperties(osmNode) {
1255
1335
  return {
1256
- ...osmNode.name !== null && { name: osmNode.name },
1336
+ ...osmNode.name && { name: osmNode.name },
1257
1337
  ...osmNode.isGate && { isGate: osmNode.isGate },
1258
1338
  ...osmNode.isSubwayEntrance && { isSubwayEntrance: osmNode.isSubwayEntrance },
1259
1339
  ...osmNode.subwayEntranceRef && { subwayEntrsanceRef: osmNode.subwayEntranceRef }
@@ -1261,7 +1341,7 @@ const _OsmGraphUtils = class _OsmGraphUtils {
1261
1341
  }
1262
1342
  static parseWayProperties(osmWay) {
1263
1343
  return {
1264
- ...osmWay.name !== null && { name: osmWay.name },
1344
+ ...osmWay.name && { name: osmWay.name },
1265
1345
  ...osmWay.isOneway && { isOneway: osmWay.isOneway },
1266
1346
  ...osmWay.areStairs && { areStairs: osmWay.areStairs },
1267
1347
  ...osmWay.isElevator && { isElevator: osmWay.isElevator },
@@ -1350,10 +1430,12 @@ const _OsmGraphUtils = class _OsmGraphUtils {
1350
1430
  return new Graph(verticesMapping.map((v) => v[1]), edges);
1351
1431
  }
1352
1432
  };
1353
- __publicField(_OsmGraphUtils, "HIGHWAYS_PEDESTRIANS", ["footway", "steps", "pedestrian", "living_street", "path", "track", "sidewalk", "elevator"]);
1433
+ __publicField(_OsmGraphUtils, "RESTRICTED_PEDESTRIANS_HIGHWAYS", ["motorway", "motorway_link"]);
1354
1434
  __publicField(_OsmGraphUtils, "DEFAULT_WAY_SELECTOR", (way) => {
1355
- const isElevatorArea = way.tags.highway === "elevator" && way.isArea;
1356
- return _OsmGraphUtils.HIGHWAYS_PEDESTRIANS.includes(way.tags.highway) && !isElevatorArea && !["no", "private"].includes(way.tags.access) || way.tags.footway === "sidewalk" || way.tags.public_transport === "platform" || way.tags.railway === "platform";
1435
+ if (way.isArea)
1436
+ return false;
1437
+ const isElevatorArea = way.tags.highway === "elevator" && way.isGeometryClosed;
1438
+ return way.tags.highway && !_OsmGraphUtils.RESTRICTED_PEDESTRIANS_HIGHWAYS.includes(way.tags.highway) && !isElevatorArea && !["no", "private"].includes(way.tags.access) || way.tags.footway === "sidewalk" || way.tags.public_transport === "platform" || way.tags.railway === "platform";
1357
1439
  });
1358
1440
  let OsmGraphUtils = _OsmGraphUtils;
1359
1441
  class RemoteRouter {
@@ -1624,7 +1706,7 @@ class CitywayRemoteRouter extends RemoteRouter {
1624
1706
  stepsBuilder.setEnd(legEnd.coords);
1625
1707
  stepsBuilder.setPathCoords(legCoords);
1626
1708
  const leg = new Leg({
1627
- mode: legMode,
1709
+ transitMode: legMode,
1628
1710
  duration: this.parseDuration(jsonLeg.Duration),
1629
1711
  startTime: jsonDateToTimestamp(jsonLeg.Departure.Time),
1630
1712
  endTime: jsonDateToTimestamp(jsonLeg.Arrival.Time),
@@ -1709,7 +1791,7 @@ class DeutscheBahnRemoteRouter extends RemoteRouter {
1709
1791
  ({ lon, lat }) => new geo.Coordinates(lat, lon, null, level)
1710
1792
  );
1711
1793
  return new Leg({
1712
- mode: "WALK",
1794
+ transitMode: "WALK",
1713
1795
  coords,
1714
1796
  start: { coords: coords[0] },
1715
1797
  end: { coords: coords[coords.length - 1] }
@@ -1806,7 +1888,7 @@ class IdfmRemoteRouter extends RemoteRouter {
1806
1888
  throw RemoteRoutingError.notFound(this.rname, jsonResponse.error.message);
1807
1889
  }
1808
1890
  const itineraries = this.parseResponse(jsonResponse);
1809
- const sameModeFound = itineraries.some((itinerary) => itinerary.mode === routerRequest.travelMode);
1891
+ const sameModeFound = itineraries.some((itinerary) => itinerary.transitMode === routerRequest.travelMode);
1810
1892
  if (!sameModeFound) {
1811
1893
  throw RemoteRoutingError.notFound(
1812
1894
  this.rname,
@@ -1949,7 +2031,7 @@ class IdfmRemoteRouter extends RemoteRouter {
1949
2031
  }, []);
1950
2032
  const stepsBuilder = new StepsBuilder().setStart(startSection).setEnd(endSection).setPathCoords(legCoords);
1951
2033
  let transportInfo;
1952
- let mode = transitModeCorrespondance.get(jsonSection.mode);
2034
+ let transitMode = transitModeCorrespondance.get(jsonSection.mode);
1953
2035
  if (jsonSection.path) {
1954
2036
  const idfmIntermediateSteps = [];
1955
2037
  for (const jsonPathLink of jsonSection.path) {
@@ -1967,7 +2049,7 @@ class IdfmRemoteRouter extends RemoteRouter {
1967
2049
  routeTextColor: jsonSection.display_informations.text_color,
1968
2050
  directionName: jsonSection.display_informations.direction
1969
2051
  };
1970
- mode = transitModeCorrespondance.get(jsonSection.display_informations.physical_mode);
2052
+ transitMode = transitModeCorrespondance.get(jsonSection.display_informations.physical_mode);
1971
2053
  const legStep = {
1972
2054
  coords: legCoords[0],
1973
2055
  name: transportInfo.directionName,
@@ -1976,7 +2058,7 @@ class IdfmRemoteRouter extends RemoteRouter {
1976
2058
  stepsBuilder.setStepsInfo([legStep]);
1977
2059
  }
1978
2060
  const leg = new Leg({
1979
- mode,
2061
+ transitMode,
1980
2062
  duration: jsonSection.duration,
1981
2063
  startTime: dateStringToTimestamp(jsonSection.departure_date_time, timeZone),
1982
2064
  endTime: dateStringToTimestamp(jsonSection.arrival_date_time, timeZone),
@@ -2198,7 +2280,7 @@ class OsrmRemoteRouter extends RemoteRouter {
2198
2280
  });
2199
2281
  });
2200
2282
  return new Leg({
2201
- mode: transitMode,
2283
+ transitMode,
2202
2284
  duration: jsonLeg.duration,
2203
2285
  coords: legCoords,
2204
2286
  start: {
@@ -2306,7 +2388,7 @@ class OtpRemoteRouter extends RemoteRouter {
2306
2388
  });
2307
2389
  }
2308
2390
  const leg = new Leg({
2309
- mode: jsonLeg.mode,
2391
+ transitMode: jsonLeg.mode,
2310
2392
  duration: jsonLeg.duration,
2311
2393
  startTime: jsonLeg.startTime,
2312
2394
  endTime: jsonLeg.endTime,
@@ -2493,8 +2575,8 @@ class WemapMultiRouter {
2493
2575
  }
2494
2576
  const newRouterRequest = {
2495
2577
  ...routerRequest,
2496
- origin: ioMapRoute.end,
2497
- destination,
2578
+ origin,
2579
+ destination: ioMapRoute.start,
2498
2580
  waypoints: []
2499
2581
  };
2500
2582
  try {
@@ -2607,6 +2689,7 @@ class CustomNetworkMap {
2607
2689
  return vertex || null;
2608
2690
  });
2609
2691
  const entryPoints = entryPointsToCheck.filter((it) => it !== null);
2692
+ entryPoints.forEach((v) => graph.exitVertices.add(v));
2610
2693
  const bounds = {
2611
2694
  type: "MultiPolygon",
2612
2695
  coordinates: []
@@ -2838,4 +2921,5 @@ exports.WemapMultiRemoteRouter = WemapMultiRemoteRouter$1;
2838
2921
  exports.WemapMultiRouter = WemapMultiRouter;
2839
2922
  exports.WemapMultiRoutingError = WemapMultiRoutingError;
2840
2923
  exports.getDurationFromLength = getDurationFromLength;
2924
+ exports.routerRequestToJson = routerRequestToJson;
2841
2925
  //# sourceMappingURL=index.js.map