@wemap/routers 12.10.8 → 12.11.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.
Files changed (170) hide show
  1. package/dist/helpers/InstructionManager.d.ts +20 -0
  2. package/dist/helpers/InstructionManagerV1.d.ts +7 -0
  3. package/{index.ts → dist/index.d.ts} +1 -11
  4. package/dist/index.js +1 -5099
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +100 -5
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/src/ItineraryInfoManager.d.ts +31 -0
  9. package/dist/src/RoutingError.d.ts +24 -0
  10. package/{src/StatusCode.ts → dist/src/StatusCode.d.ts} +2 -3
  11. package/dist/src/Utils.d.ts +6 -0
  12. package/dist/src/graph/Edge.d.ts +38 -0
  13. package/dist/src/graph/Graph.d.ts +40 -0
  14. package/dist/src/graph/GraphProjection.d.ts +11 -0
  15. package/dist/src/graph/GraphProjectionOptions.d.ts +7 -0
  16. package/dist/src/graph/GraphRoute.d.ts +19 -0
  17. package/dist/src/graph/GraphRouter.d.ts +20 -0
  18. package/dist/src/graph/GraphRouterEngine.d.ts +80 -0
  19. package/{src/graph/GraphRouterOptions.ts → dist/src/graph/GraphRouterOptions.d.ts} +6 -7
  20. package/dist/src/graph/GraphRouterOptionsBuilder.d.ts +23 -0
  21. package/dist/src/graph/GraphRouterOptionsFactors.d.ts +8 -0
  22. package/dist/src/graph/NoRouteFoundError.d.ts +12 -0
  23. package/dist/src/graph/Vertex.d.ts +19 -0
  24. package/dist/src/model/Itinerary.d.ts +81 -0
  25. package/dist/src/model/Leg.d.ts +66 -0
  26. package/dist/src/model/LevelChange.d.ts +7 -0
  27. package/dist/src/model/RouterRequest.d.ts +42 -0
  28. package/dist/src/model/Step.d.ts +42 -0
  29. package/dist/src/model/StepExtra.d.ts +3 -0
  30. package/dist/src/model/StepsBuilder.d.ts +16 -0
  31. package/dist/src/model/TransitMode.d.ts +5 -0
  32. package/{src/model/TravelMode.ts → dist/src/model/TravelMode.d.ts} +1 -2
  33. package/dist/src/model/generateSteps.d.ts +0 -0
  34. package/{src/remote/RemoteRouter.ts → dist/src/remote/RemoteRouter.d.ts} +2 -7
  35. package/dist/src/remote/RemoteRouterManager.d.ts +731 -0
  36. package/dist/src/remote/RemoteRouterUtils.d.ts +6 -0
  37. package/dist/src/remote/cityway/CitywayRemoteRouter.d.ts +109 -0
  38. package/dist/src/remote/deutsche-bahn/DeutscheBahnRemoteRouter.d.ts +31 -0
  39. package/dist/src/remote/geovelo/GeoveloRemoteRouter.d.ts +106 -0
  40. package/dist/src/remote/idfm/IdfmRemoteRouter.d.ts +123 -0
  41. package/dist/src/remote/navitia/NavitiaRemoteRouter.d.ts +34 -0
  42. package/dist/src/remote/navitia/types.d.ts +87 -0
  43. package/dist/src/remote/osrm/OsrmRemoteRouter.d.ts +70 -0
  44. package/dist/src/remote/otp/OtpRemoteRouter.d.ts +69 -0
  45. package/dist/src/remote/wemap-multi/WemapMultiRemoteRouter.d.ts +19 -0
  46. package/dist/src/types.d.ts +31 -0
  47. package/dist/src/wemap-multi/CustomGraphMap.d.ts +56 -0
  48. package/dist/src/wemap-multi/CustomGraphMapTester.d.ts +39 -0
  49. package/dist/src/wemap-multi/WemapMultiRouter.d.ts +14 -0
  50. package/dist/src/wemap-osm/OsmGraphUtils.d.ts +11 -0
  51. package/dist/tests/CommonTest.d.ts +8 -0
  52. package/package.json +11 -8
  53. package/assets/biocbon-bergere-rdc-network.osm +0 -163
  54. package/assets/bureaux-wemap-montpellier-network.osm +0 -174
  55. package/assets/components.osm +0 -146
  56. package/assets/elevator-models-4.osm +0 -89
  57. package/assets/elevator-models.osm +0 -354
  58. package/assets/exit-graph.osm +0 -25
  59. package/assets/gare-de-lest-network-pp-bounds.osm +0 -1613
  60. package/assets/gare-de-lyon-extract.osm +0 -174
  61. package/assets/geovelo-montpellier.json +0 -1144
  62. package/assets/horizontal-elevator.osm +0 -12
  63. package/assets/itinerary-deutsche-bahn-1.json +0 -368
  64. package/assets/itinerary-grenoble-otp-1.json +0 -1536
  65. package/assets/itinerary-grenoble-otp-2.json +0 -1092
  66. package/assets/itinerary-info-two-points-proj.osm +0 -39
  67. package/assets/itinerary-info-two-points.osm +0 -24
  68. package/assets/itinerary-lehavre-cityway-1.json +0 -6799
  69. package/assets/itinerary-lehavre-cityway-2.json +0 -2133
  70. package/assets/itinerary-lehavre-cityway-3.json +0 -12577
  71. package/assets/itinerary-lehavre-cityway-4.json +0 -1451
  72. package/assets/itinerary-lehavre-cityway-5.json +0 -5925
  73. package/assets/itinerary-lemans-navitia.json +0 -7768
  74. package/assets/itinerary-montpellier-osrm-3.json +0 -185
  75. package/assets/itinerary-montpellier-outdoor-without-steps.json +0 -110
  76. package/assets/itinerary-montpellier-outdoor.json +0 -513
  77. package/assets/itinerary-paris-idfm-2.json +0 -1838
  78. package/assets/itinerary-paris-idfm.json +0 -27727
  79. package/assets/itinerary-step-not-on-path-osrm.json +0 -457
  80. package/assets/itinerary-with-duplicate-nodes.json +0 -110
  81. package/assets/network-conveying-backward.osm +0 -74
  82. package/assets/network-elevator.osm +0 -48
  83. package/assets/network-escalators.osm +0 -50
  84. package/assets/network-simple.osm +0 -27
  85. package/assets/network-steps-same-level.osm +0 -283
  86. package/assets/network-with-modifiers.osm +0 -39
  87. package/assets/one-way.osm +0 -46
  88. package/assets/report-map-1.osm +0 -36
  89. package/assets/report-map-2.osm +0 -29
  90. package/assets/report-map-3.osm +0 -15
  91. package/assets/rr-wemap-multi-indoor-outdoor-indoor.json +0 -1352
  92. package/assets/rr-wemap-multi-indoor-outdoor.json +0 -145
  93. package/assets/rr-wemap-multi-multi-level.json +0 -262
  94. package/assets/rr-wemap-multi-outdoor-indoor.json +0 -145
  95. package/assets/rr-wemap-multi-outdoor-outdoor.json +0 -207
  96. package/assets/rr-wemap-multi-remote-indoor-indoor.json +0 -155
  97. package/assets/rr-wemap-multi-remote-indoor-outdoor-indoor.json +0 -668
  98. package/assets/rr-wemap-multi-remote-indoor-outdoor.json +0 -154
  99. package/assets/rr-wemap-multi-remote-outdoor-indoor.json +0 -179
  100. package/assets/rr-wemap-multi-remote-outdoor-outdoor.json +0 -109
  101. package/assets/stairs-and-exit.osm +0 -47
  102. package/helpers/InstructionManager.ts +0 -184
  103. package/helpers/InstructionManagerV1.ts +0 -95
  104. package/src/ItineraryInfoManager.spec.ts +0 -183
  105. package/src/ItineraryInfoManager.ts +0 -181
  106. package/src/RoutingError.ts +0 -60
  107. package/src/Utils.ts +0 -8
  108. package/src/graph/Edge.spec.ts +0 -32
  109. package/src/graph/Edge.ts +0 -64
  110. package/src/graph/Graph.spec.ts +0 -509
  111. package/src/graph/Graph.ts +0 -272
  112. package/src/graph/GraphProjection.ts +0 -15
  113. package/src/graph/GraphProjectionOptions.ts +0 -8
  114. package/src/graph/GraphRoute.spec.ts +0 -15
  115. package/src/graph/GraphRoute.ts +0 -43
  116. package/src/graph/GraphRouter.spec.ts +0 -317
  117. package/src/graph/GraphRouter.ts +0 -229
  118. package/src/graph/GraphRouterEngine.ts +0 -248
  119. package/src/graph/GraphRouterOptionsBuilder.ts +0 -98
  120. package/src/graph/NoRouteFoundError.ts +0 -39
  121. package/src/graph/Vertex.spec.ts +0 -42
  122. package/src/graph/Vertex.ts +0 -45
  123. package/src/model/Itinerary.spec.ts +0 -134
  124. package/src/model/Itinerary.ts +0 -370
  125. package/src/model/Leg.spec.ts +0 -107
  126. package/src/model/Leg.ts +0 -224
  127. package/src/model/LevelChange.spec.ts +0 -50
  128. package/src/model/LevelChange.ts +0 -14
  129. package/src/model/RouterRequest.ts +0 -33
  130. package/src/model/Step.spec.ts +0 -99
  131. package/src/model/Step.ts +0 -90
  132. package/src/model/StepExtra.ts +0 -1
  133. package/src/model/StepsBuilder.ts +0 -242
  134. package/src/model/TransitMode.spec.ts +0 -31
  135. package/src/model/TransitMode.ts +0 -28
  136. package/src/model/generateSteps.ts +0 -102
  137. package/src/remote/RemoteRouterManager.spec.ts +0 -178
  138. package/src/remote/RemoteRouterManager.ts +0 -72
  139. package/src/remote/RemoteRouterUtils.ts +0 -25
  140. package/src/remote/cityway/CitywayRemoteRouter.spec.ts +0 -122
  141. package/src/remote/cityway/CitywayRemoteRouter.ts +0 -435
  142. package/src/remote/deutsche-bahn/DeutscheBahnRemoteRouter.spec.ts +0 -52
  143. package/src/remote/deutsche-bahn/DeutscheBahnRemoteRouter.ts +0 -85
  144. package/src/remote/geovelo/GeoveloRemoteRouter.spec.ts +0 -54
  145. package/src/remote/geovelo/GeoveloRemoteRouter.ts +0 -293
  146. package/src/remote/idfm/IdfmRemoteRouter.spec.ts +0 -102
  147. package/src/remote/idfm/IdfmRemoteRouter.ts +0 -523
  148. package/src/remote/navitia/NavitiaRemoteRouter.spec.ts +0 -116
  149. package/src/remote/navitia/NavitiaRemoteRouter.ts +0 -445
  150. package/src/remote/navitia/types.ts +0 -73
  151. package/src/remote/osrm/OsrmRemoteRouter.spec.ts +0 -127
  152. package/src/remote/osrm/OsrmRemoteRouter.ts +0 -303
  153. package/src/remote/otp/OtpRemoteRouter.spec.ts +0 -103
  154. package/src/remote/otp/OtpRemoteRouter.ts +0 -223
  155. package/src/remote/wemap-multi/WemapMultiRemoteRouter.spec.ts +0 -103
  156. package/src/remote/wemap-multi/WemapMultiRemoteRouter.ts +0 -56
  157. package/src/types.ts +0 -32
  158. package/src/wemap-multi/CustomGraphMap.spec.ts +0 -40
  159. package/src/wemap-multi/CustomGraphMap.ts +0 -213
  160. package/src/wemap-multi/CustomGraphMapTester.spec.ts +0 -48
  161. package/src/wemap-multi/CustomGraphMapTester.ts +0 -90
  162. package/src/wemap-multi/WemapMultiRouter.spec.ts +0 -138
  163. package/src/wemap-multi/WemapMultiRouter.ts +0 -329
  164. package/src/wemap-osm/OsmGraphUtils.spec.ts +0 -165
  165. package/src/wemap-osm/OsmGraphUtils.ts +0 -173
  166. package/src/wemap-osm/OsmRouter.elevators.spec.ts +0 -106
  167. package/src/wemap-osm/OsmRouter.spec.ts +0 -292
  168. package/tests/CommonTest.ts +0 -78
  169. package/tsconfig.json +0 -3
  170. package/vite.config.ts +0 -4
@@ -1,45 +0,0 @@
1
- import { Coordinates } from '@wemap/geo';
2
-
3
- import { GraphVertexJson } from '../types.js';
4
-
5
- export type VertexProperties = {
6
-
7
- name?: string;
8
- externalId?: string | number;
9
-
10
- isGate?: boolean;
11
- isSubwayEntrance?: boolean;
12
- subwayEntranceRef?: string;
13
- }
14
-
15
- export default class Vertex {
16
-
17
- public id = -1;
18
-
19
- constructor(public coords: Coordinates, public properties: VertexProperties = {}) { }
20
-
21
- distanceTo(other: Vertex) {
22
- return this.coords.distanceTo(other.coords);
23
- }
24
-
25
- bearingTo(other: Vertex) {
26
- return this.coords.bearingTo(other.coords);
27
- }
28
-
29
- toJson(): GraphVertexJson {
30
- return {
31
- id: this.id,
32
- coords: this.coords.toCompressedJson(),
33
- ...(Object.keys(this.properties).length > 0 && { properties: this.properties })
34
- }
35
- }
36
-
37
- static fromJson(json: GraphVertexJson) {
38
- const v = new Vertex(
39
- Coordinates.fromCompressedJson(json.coords),
40
- json.properties
41
- );
42
- v.id = json.id;
43
- return v;
44
- }
45
- }
@@ -1,134 +0,0 @@
1
- import chai from 'chai';
2
- import chaiAlmost from 'chai-almost';
3
-
4
- import { Coordinates } from '@wemap/geo';
5
- import { diffAngle } from '@wemap/maths';
6
-
7
- import Itinerary from './Itinerary.js';
8
- import { leg1, leg2, leg3, leg4 } from './Leg.spec.js';
9
- import Leg from './Leg.js';
10
-
11
- const expect = chai.expect;
12
-
13
- const itinerary1 = new Itinerary({
14
- origin: new Coordinates(0, 0),
15
- destination: new Coordinates(1, 0),
16
- legs: [leg1]
17
- });
18
-
19
- const itinerary2 = new Itinerary({
20
- origin: new Coordinates(0, 0),
21
- destination: new Coordinates(1, 0),
22
- duration: 200,
23
- legs: [Leg.fromJson(leg1.toJson()), Leg.fromJson(leg2.toJson())],
24
- startTime: 0,
25
- endTime: 10
26
- });
27
-
28
- const itinerary3 = new Itinerary({
29
- origin: new Coordinates(0, 0),
30
- destination: new Coordinates(3, 0),
31
- duration: 2000,
32
- legs: [
33
- Leg.fromJson(leg1.toJson()),
34
- Leg.fromJson(leg2.toJson()),
35
- Leg.fromJson(leg3.toJson()),
36
- Leg.fromJson(leg4.toJson())
37
- ],
38
- });
39
-
40
- const itinerary4 = new Itinerary({
41
- origin: new Coordinates(1, 0),
42
- destination: new Coordinates(4, 0),
43
- duration: 20000,
44
- legs: [Leg.fromJson(leg1.toJson())],
45
- });
46
-
47
- const itinerary5 = new Itinerary({
48
- origin: new Coordinates(1, 0),
49
- destination: new Coordinates(4, 0),
50
- duration: 20000,
51
- legs: [Leg.fromJson(leg1.toJson())],
52
- });
53
-
54
- export const verifyStepsCoherence = (itinerary: Itinerary) => {
55
-
56
- chai.use(chaiAlmost(1e-3));
57
-
58
- const itineraryCoords = itinerary.coords
59
- // Remove duplicates
60
- .filter((coords, idx, arr) => idx === 0 || !arr[idx - 1].equals(coords));
61
- const steps = itinerary.steps;
62
-
63
-
64
- steps.forEach((step, stepId) => {
65
- const coordsId = itineraryCoords.findIndex(coords => coords.equals(step.coords));
66
- expect(coordsId).not.equal(-1);
67
-
68
- const coordsBeforeStep = coordsId === 0 ? itinerary.origin : itineraryCoords[coordsId - 1];
69
- const coordsAfterStep = coordsId === itineraryCoords.length - 1
70
- ? itinerary.destination
71
- : itineraryCoords[coordsId + 1];
72
-
73
- expect(step.number).equals(stepId + 1);
74
- expect(step.previousBearing).equals(coordsBeforeStep.bearingTo(step.coords));
75
- expect(step.nextBearing).equals(step.coords.bearingTo(coordsAfterStep));
76
- expect(step.angle).almost.equals(diffAngle(step.previousBearing, step.nextBearing + Math.PI));
77
- expect(step.firstStep).equals(stepId === 0);
78
- expect(step.lastStep).equals(stepId === steps.length - 1);
79
- });
80
- itinerary.legs.forEach(leg => {
81
- const legSteps = leg.steps;
82
- expect(legSteps.length).not.equals(0);
83
- legSteps.forEach(step => expect(step).is.not.undefined);
84
- });
85
- }
86
-
87
- describe('Itinerary', () => {
88
-
89
- it('equals', () => {
90
- expect(itinerary1.equals(itinerary1)).is.true;
91
- expect(itinerary2.equals(itinerary2)).is.true;
92
- expect(itinerary3.equals(itinerary3)).is.true;
93
- expect(itinerary4.equals(itinerary4)).is.true;
94
- expect(itinerary4.equals(itinerary5)).is.true;
95
- expect(itinerary1.equals(itinerary2)).is.false;
96
- expect(itinerary1.equals(itinerary3)).is.false;
97
- expect(itinerary1.equals(itinerary4)).is.false;
98
- });
99
-
100
- it('steps coherence', () => {
101
- verifyStepsCoherence(itinerary1);
102
- verifyStepsCoherence(itinerary2);
103
- verifyStepsCoherence(itinerary3);
104
- verifyStepsCoherence(itinerary4);
105
- verifyStepsCoherence(itinerary5);
106
- });
107
-
108
- it('from / to JSON', () => {
109
-
110
- const itinerary1Json = itinerary1.toJson();
111
- expect(itinerary1Json).be.an('object');
112
- const itinerary1Bis = Itinerary.fromJson(itinerary1Json);
113
- expect(itinerary1.equals(itinerary1Bis)).is.true;
114
-
115
- const itinerary2Json = itinerary2.toJson();
116
- expect(itinerary2Json).be.an('object');
117
- const itinerary2Bis = Itinerary.fromJson(itinerary2Json);
118
- expect(itinerary2.equals(itinerary2Bis)).is.true;
119
-
120
- const itinerary3Json = itinerary3.toJson();
121
- expect(itinerary3Json).be.an('object');
122
- const itinerary3Bis = Itinerary.fromJson(itinerary3Json);
123
- expect(itinerary3.equals(itinerary3Bis)).is.true;
124
-
125
- const itinerary4Json = itinerary4.toJson();
126
- expect(itinerary4Json).be.an('object');
127
- const itinerary4Bis = Itinerary.fromJson(itinerary4Json);
128
- expect(itinerary4.equals(itinerary4Bis)).is.true;
129
-
130
- });
131
-
132
- });
133
-
134
- export { itinerary1, itinerary2, itinerary3, itinerary4 };
@@ -1,370 +0,0 @@
1
- import { Feature, FeatureCollection, MultiLineString, Point } from 'geojson';
2
- import { Coordinates, Level, Utils as GeoUtils, CoordinatesJson, Level_t } from '@wemap/geo';
3
- import { diffAngle, Point2_t, Vector3_t } from '@wemap/maths';
4
-
5
- import Graph from '../graph/Graph.js';
6
- import Leg, { type LegJson } from './Leg.js';
7
- import { isTransitModePublicTransport, type TransitMode } from './TransitMode.js';
8
- import { Step } from './Step.js';
9
- import { getDurationFromLength } from '../Utils.js';
10
- import StepsBuilder from './StepsBuilder.js';
11
- import GraphRoute from '../graph/GraphRoute.js';
12
-
13
-
14
- export type ItineraryCommon = {
15
- startTime?: number,
16
- endTime?: number
17
- };
18
-
19
- export type ItineraryJson = {
20
- transitMode: TransitMode,
21
- origin: CoordinatesJson,
22
- destination: CoordinatesJson,
23
- distance: number,
24
- duration: number,
25
- legs: LegJson[]
26
- } & ItineraryCommon;
27
-
28
-
29
- export type ItineraryConstructor = {
30
- origin: Coordinates,
31
- destination: Coordinates,
32
- duration?: number
33
- legs: Leg[],
34
- } & ItineraryCommon;
35
-
36
- /**
37
- * Main attributes are:
38
- * nodes: the ordered list of Node
39
- * edges: the ordered list of Edge
40
- * start: the start point (Coordinates)
41
- * end: the end point (Coordinates)
42
- * length: the route length
43
- */
44
- export default class Itinerary {
45
-
46
- origin: Coordinates;
47
- destination: Coordinates;
48
- duration: number;
49
- readonly legs: Leg[];
50
-
51
- private _transitMode: TransitMode | null = null;
52
- startTime: number | null;
53
- endTime: number | null;
54
-
55
- private _coords: Coordinates[] | null = null;
56
- private _distance: number | null = null;
57
-
58
- constructor({
59
- origin, destination, duration, legs, startTime, endTime
60
- }: ItineraryConstructor) {
61
- this.origin = origin;
62
- this.destination = destination;
63
- this.legs = legs;
64
- if (typeof duration === 'number') {
65
- this.duration = duration;
66
- } else {
67
- this.duration = this.legs.reduce((dur, leg) => dur + leg.duration, 0);
68
- }
69
- this.startTime = typeof startTime === 'number' ? startTime : null;
70
- this.endTime = typeof endTime === 'number' ? endTime : null;
71
-
72
- this.updateStepsFromLegs();
73
- }
74
-
75
- set coords(_) {
76
- throw new Error('Itinerary.coords cannot be set. They are calculated from Itinerary.legs.');
77
- }
78
-
79
- public get coords() {
80
- if (!this._coords) {
81
- this._coords = this.legs.map(leg => leg.coords)
82
- .flat()
83
- // Remove duplicates
84
- .filter((coords, idx, arr) => idx === 0 || !arr[idx - 1].equals(coords));
85
- }
86
- return this._coords;
87
- }
88
-
89
- set steps(_) {
90
- throw new Error('Itinerary.step cannot be set. They are calculated from Itinerary.legs.');
91
- }
92
-
93
- get steps(): Step[] {
94
- return this.legs.map(leg => leg.steps).flat();
95
- }
96
-
97
- set transitMode(_) {
98
- throw new Error('Itinerary.transitMode cannot be set. They are calculated from Itinerary.legs.');
99
- }
100
-
101
- // Transit mode will return MULTI if there are several transit modes except WALK
102
- // Else it will return the only transit mode
103
- // fallback to WALK if no transit mode
104
- get transitMode() {
105
- if (!this._transitMode) {
106
- const legTransitModes = new Set(this.legs.map(leg => leg.transitMode));
107
-
108
- // Remove WALK from transit modes
109
- legTransitModes.delete('WALK');
110
-
111
- if (legTransitModes.size > 1) {
112
- this._transitMode = 'MULTI';
113
-
114
- return this._transitMode;
115
- }
116
-
117
- if (legTransitModes.size === 1) {
118
- this._transitMode = legTransitModes.values().next().value as TransitMode;
119
-
120
- return this._transitMode;
121
- }
122
-
123
- this._transitMode = 'WALK';
124
- }
125
-
126
- return this._transitMode;
127
-
128
- }
129
-
130
- set distance(_) {
131
- throw new Error('Itinerary.distance cannot be set. They are calculated from Itinerary.legs.');
132
- }
133
-
134
- get distance() {
135
- if (this._distance === null) {
136
- this._distance = GeoUtils.calcDistance(this.coords);
137
- }
138
- return this._distance;
139
-
140
- // Does not work if legs does not intersect
141
- // return this.legs.reduce((dist, leg) => dist + leg.distance, 0);
142
- }
143
-
144
- toGraph() {
145
- return Graph.fromCoordinatesSegments([this.coords]);
146
- }
147
-
148
- static fromItineraries(...itineraries: Itinerary[]) {
149
- return new Itinerary({
150
- origin: itineraries[0].origin,
151
- destination: itineraries[itineraries.length - 1].destination,
152
- legs: itineraries.map(itinerary => itinerary.legs).flat(),
153
- });
154
- }
155
-
156
- /**
157
- * Convert lat/lng/level? points to Itinerary
158
- */
159
- static fromOrderedPointsArray(
160
- points: (Point2_t | Vector3_t)[],
161
- start: (Point2_t | Vector3_t),
162
- end: (Point2_t | Vector3_t)) {
163
-
164
- const pointToCoordinates = (point: Point2_t | Vector3_t) =>
165
- new Coordinates(point[0], point[1], null, point[2]);
166
-
167
- return this.fromOrderedCoordinates(
168
- points.map(pointToCoordinates),
169
- pointToCoordinates(start),
170
- pointToCoordinates(end)
171
- );
172
- }
173
-
174
- /**
175
- * Convert ordered Coordinates to Itinerary
176
- */
177
- static fromOrderedCoordinates(
178
- coords: Coordinates[],
179
- origin: Coordinates,
180
- destination: Coordinates,
181
- transitMode: TransitMode = 'WALK'
182
- ) {
183
-
184
- const steps = new StepsBuilder().setPathCoords(coords).setStart(origin).setEnd(destination).build();
185
-
186
- const leg = new Leg({
187
- start: { coords: origin },
188
- end: { coords: destination },
189
- coords,
190
- transitMode,
191
- steps
192
- });
193
-
194
- return new Itinerary({ origin, destination, legs: [leg] });
195
- }
196
-
197
-
198
- static equals(obj1: Itinerary, obj2: Itinerary) {
199
- const intermediate = obj1.origin.equals(obj2.origin)
200
- && obj1.destination.equals(obj2.destination)
201
- && Math.abs(obj1.distance - obj2.distance) <= 0.05
202
- && Math.abs(obj1.duration - obj2.duration) <= 0.05
203
- && obj1.startTime === obj2.startTime
204
- && obj1.endTime === obj2.endTime
205
- && obj1.legs.length === obj2.legs.length;
206
-
207
- if (!intermediate) {
208
- return false;
209
- }
210
-
211
- for (let i = 0; i < obj1.legs.length; i++) {
212
- if (!obj1.legs[i].equals(obj2.legs[i])) {
213
- return false;
214
- }
215
- }
216
-
217
- return true;
218
- }
219
-
220
- equals(obj: Itinerary) {
221
- return Itinerary.equals(this, obj);
222
- }
223
-
224
- toJson(): ItineraryJson {
225
- return {
226
- origin: this.origin.toJson(),
227
- destination: this.destination.toJson(),
228
- distance: Number(this.distance.toFixed(1)),
229
- duration: Number(this.duration.toFixed(1)),
230
- transitMode: this.transitMode,
231
- legs: this.legs.map(leg => leg.toJson()),
232
- ...(this.startTime !== null && { startTime: this.startTime }),
233
- ...(this.endTime !== null && { endTime: this.endTime })
234
- };
235
- }
236
-
237
- static fromJson(json: ItineraryJson) {
238
- return new Itinerary({
239
- origin: Coordinates.fromJson(json.origin),
240
- destination: Coordinates.fromJson(json.destination),
241
- duration: json.duration,
242
- legs: json.legs.map(Leg.fromJson),
243
- startTime: json.startTime,
244
- endTime: json.endTime
245
- });
246
- }
247
-
248
- static fromGraphRoute(graphRoute: GraphRoute, transitMode: TransitMode = 'WALK') {
249
- const leg = Leg.fromGraphRoute(graphRoute, transitMode);
250
- return new Itinerary({
251
- origin: graphRoute.start,
252
- destination: graphRoute.end,
253
- legs: [leg]
254
- });
255
- }
256
-
257
- // TODO: Remove when possible...
258
- // Livemap specific
259
- multiplyLevel(levelFactor: number) {
260
-
261
- this.origin.level = Level.multiplyBy(this.origin.level, levelFactor);
262
- this.destination.level = Level.multiplyBy(this.destination.level, levelFactor);
263
-
264
- this.legs.forEach(leg => leg.multiplyLevel(levelFactor));
265
-
266
- // It is not necessary to multiply this._coords because
267
- // coords are not cloned between legs and itinerary
268
- }
269
-
270
- // TODO: Remove when possible...
271
- // Livemap specific
272
- forceUnknownLevelTo0() {
273
-
274
- this.origin.level = this.origin.level || 0;
275
- this.destination.level = this.destination.level || 0;
276
-
277
- for (const leg of this.legs) {
278
- leg.start.coords.level = leg.start.coords.level || 0;
279
- leg.end.coords.level = leg.end.coords.level || 0;
280
- for (const coords of leg.coords) {
281
- coords.level = coords.level || 0;
282
- }
283
- if (leg.steps) {
284
- for (const step of leg.steps) {
285
- step.coords.level = step.coords.level || 0;
286
- }
287
- }
288
- }
289
-
290
- if (this._coords) {
291
- for (const coords of this._coords) {
292
- coords.level = coords.level || 0;
293
- }
294
- }
295
- }
296
-
297
- toGeoJson(): FeatureCollection {
298
- const transformToPoint = (point: Coordinates, name?: string, type?: string): Feature<Point> => ({
299
- type: "Feature",
300
- properties: { name, level: point.level, ...(type && { type }) },
301
- geometry: {
302
- type: 'Point',
303
- coordinates: [point.lng, point.lat]
304
- }
305
- });
306
- const transformToMultiLineStrings = (segments: Coordinates[][], level: Level_t): Feature<MultiLineString> => ({
307
- type: "Feature",
308
- properties: { level, name: level?.toString() },
309
- geometry: {
310
- type: 'MultiLineString',
311
- coordinates: segments.map(s => s.map(({ lat, lng }) => [lng, lat]))
312
- }
313
- });
314
-
315
- const levelsOfItinerary = [...new Set(this.coords.map(c => Level.toString(c.level)))].map(Level.fromString);
316
- const segmentsSplitted: [Level_t, Coordinates[][]][] = levelsOfItinerary.map(loi => [loi, GeoUtils.createSegmentsAtLevel(this.coords, loi, true)]);
317
- const multiLineStrings = segmentsSplitted.map(([loi, segments]) => transformToMultiLineStrings(segments, loi))
318
- const legsStarts = this.legs.map((leg, idx) => transformToPoint(leg.start.coords, `Leg ${idx} start`, 'leg-start'));
319
- const legsEnds = this.legs.map((leg, idx) => transformToPoint(leg.end.coords, `Leg ${idx} end`, 'leg-end'));
320
- const steps = this.steps.map(step => transformToPoint(step.coords, `Step ${step.number}`, 'step'))
321
- return {
322
- type: "FeatureCollection",
323
- features: [
324
- transformToPoint(this.origin, 'origin', 'origin'),
325
- transformToPoint(this.destination, 'destination', 'destination'),
326
- ...multiLineStrings,
327
- ...legsStarts,
328
- ...legsEnds,
329
- ...steps
330
- ]
331
- };
332
- }
333
-
334
- /**
335
- * TODO: Remove it in router v3
336
- * Update steps info thanks to the coordinates of the whole itinerary.
337
- * This method will update:
338
- * - all steps number
339
- * - first/last steps
340
- * - previousBearing/nextBearing/angle of first and last step of each leg
341
- */
342
- updateStepsFromLegs() {
343
-
344
- const itineraryCoords = this.coords
345
- // Remove duplicates
346
- .filter((coords, idx, arr) => idx === 0 || !arr[idx - 1].equals(coords));
347
- const steps = this.legs.map(leg => leg.steps).flat();
348
-
349
-
350
- steps.map((step, stepId) => {
351
- const coordsId = itineraryCoords.findIndex(coords => coords.equals(step.coords));
352
- if (coordsId === -1) {
353
- throw new Error('Cannot find step coordinates in itinerary coordinates.');
354
- }
355
-
356
- const coordsBeforeStep = coordsId === 0 ? this.origin : itineraryCoords[coordsId - 1];
357
- const coordsAfterStep = coordsId === itineraryCoords.length - 1
358
- ? this.destination
359
- : itineraryCoords[coordsId + 1];
360
-
361
- step.previousBearing = coordsBeforeStep.bearingTo(step.coords);
362
- step.nextBearing = step.coords.bearingTo(coordsAfterStep);
363
- step.angle = diffAngle(step.previousBearing, step.nextBearing + Math.PI);
364
-
365
- step.number = stepId + 1;
366
- step.firstStep = stepId === 0;
367
- step.lastStep = stepId === steps.length - 1;
368
- });
369
- }
370
- }
@@ -1,107 +0,0 @@
1
- import chai from 'chai';
2
-
3
- import { Coordinates } from '@wemap/geo';
4
-
5
- import Leg from './Leg.js';
6
- import { step1, step2, step3, step4 } from './Step.spec.js';
7
-
8
- const { expect } = chai;
9
-
10
- const leg1 = new Leg({
11
- start: { coords: new Coordinates(0, 0) },
12
- end: { coords: new Coordinates(1, 0) },
13
- coords: [new Coordinates(0, 0), new Coordinates(1, 0)],
14
- transitMode: 'CAR',
15
- duration: 5
16
- });
17
-
18
- const leg2 = new Leg({
19
- start: { coords: new Coordinates(1, 0) },
20
- end: { coords: new Coordinates(2, 0) },
21
- coords: [new Coordinates(1, 0), new Coordinates(1.5, 0), new Coordinates(2, 0)],
22
- transitMode: 'WALK',
23
- duration: 200
24
- });
25
-
26
- const leg3 = new Leg({
27
- start: { name: 'foo1', coords: new Coordinates(2, 0) },
28
- end: { name: 'foo2', coords: new Coordinates(3, 0) },
29
- coords: [new Coordinates(2, 0), new Coordinates(3, 0)],
30
- transitMode: 'BUS',
31
- duration: 300,
32
- startTime: 0,
33
- endTime: 10,
34
- transportInfo: {
35
- name: 'foo3',
36
- routeColor: '#000',
37
- routeTextColor: '#fff',
38
- directionName: 'foo4'
39
- }
40
- });
41
-
42
- const leg4 = new Leg({
43
- start: { coords: new Coordinates(0, 0) },
44
- end: { coords: new Coordinates(4, 0) },
45
- coords: [new Coordinates(0, 0), new Coordinates(4, 0)],
46
- transitMode: 'BOAT',
47
- duration: 2000,
48
- steps: [step1, step2, step3, step4]
49
- });
50
-
51
- const leg5 = new Leg({
52
- start: { coords: new Coordinates(0, 0) },
53
- end: { coords: new Coordinates(4, 0) },
54
- coords: [new Coordinates(0, 0), new Coordinates(4, 0)],
55
- transitMode: 'BOAT',
56
- duration: 2000,
57
- steps: [step1, step2, step3, step4]
58
- });
59
-
60
-
61
- describe('Leg', () => {
62
-
63
- it('isPublicTransport', () => {
64
- expect(leg1.isPublicTransport()).is.false;
65
- expect(leg2.isPublicTransport()).is.false;
66
- expect(leg3.isPublicTransport()).is.true;
67
- expect(leg4.isPublicTransport()).is.true;
68
- });
69
-
70
- it('equals', () => {
71
- expect(leg1.equals(leg1)).is.true;
72
- expect(leg2.equals(leg2)).is.true;
73
- expect(leg3.equals(leg3)).is.true;
74
- expect(leg4.equals(leg4)).is.true;
75
- expect(leg4.equals(leg5)).is.true;
76
- expect(leg1.equals(leg2)).is.false;
77
- expect(leg1.equals(leg3)).is.false;
78
- expect(leg1.equals(leg4)).is.false;
79
- });
80
-
81
- it('from / to JSON', () => {
82
-
83
- const leg1Json = leg1.toJson();
84
- expect(leg1Json).be.an('object');
85
- const leg1Bis = Leg.fromJson(leg1Json);
86
- expect(leg1.equals(leg1Bis)).is.true;
87
-
88
- const leg2Json = leg2.toJson();
89
- expect(leg2Json).be.an('object');
90
- const leg2Bis = Leg.fromJson(leg2Json);
91
- expect(leg2.equals(leg2Bis)).is.true;
92
-
93
- const leg3Json = leg3.toJson();
94
- expect(leg3Json).be.an('object');
95
- const leg3Bis = Leg.fromJson(leg3Json);
96
- expect(leg3.equals(leg3Bis)).is.true;
97
-
98
- const leg4Json = leg4.toJson();
99
- expect(leg4Json).be.an('object');
100
- const leg4Bis = Leg.fromJson(leg4Json);
101
- expect(leg4.equals(leg4Bis)).is.true;
102
-
103
- });
104
-
105
- });
106
-
107
- export { leg1, leg2, leg3, leg4 };