@wemap/geo 6.0.0 → 6.2.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/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "directory": "packages/geo"
13
13
  },
14
14
  "name": "@wemap/geo",
15
- "version": "6.0.0",
15
+ "version": "6.2.3",
16
16
  "bugs": {
17
17
  "url": "https://github.com/wemap/wemap-modules-js/issues"
18
18
  },
@@ -30,5 +30,5 @@
30
30
  "@wemap/logger": "^6.0.0",
31
31
  "@wemap/maths": "^6.0.0"
32
32
  },
33
- "gitHead": "a377d6fd98b48ce0e083ece1c695642947c456cc"
33
+ "gitHead": "e88b0f15de31c30588aaacdadcbad2da46f9673d"
34
34
  }
package/src/Utils.js CHANGED
@@ -85,6 +85,10 @@ export function trimRoute(route, startPosition = route[0], length = Number.MAX_V
85
85
  let currentPointIndex;
86
86
  let cumulativeDistance = 0;
87
87
 
88
+ if (route.length <= 1) {
89
+ throw new Error('Route must have at least 2 points');
90
+ }
91
+
88
92
  for (currentPointIndex = 1; currentPointIndex < route.length; currentPointIndex++) {
89
93
 
90
94
  const p1 = route[currentPointIndex - 1];
package/src/Utils.spec.js CHANGED
@@ -60,6 +60,8 @@ describe('Geo Utils', () => {
60
60
  const route = [p1, p2, p3];
61
61
 
62
62
  expect(() => trimRoute(route, new Coordinates(0, 0))).throw(Error);
63
+ expect(() => trimRoute([p1], p1)).throw(Error);
64
+ expect(() => trimRoute([], null)).throw(Error);
63
65
 
64
66
  newRoute = trimRoute(route);
65
67
  expect(newRoute.length).equals(3);
@@ -7,6 +7,18 @@ import Logger from '@wemap/logger';
7
7
  */
8
8
  class Level {
9
9
 
10
+ /** @type {number?} */
11
+ val = null;
12
+
13
+ /** @type {boolean} */
14
+ isRange = false;
15
+
16
+ /** @type {number?} */
17
+ low = null;
18
+
19
+ /** @type {number?} */
20
+ up = null;
21
+
10
22
  /**
11
23
  * Level constructor
12
24
  * 1 argument: level is not a range and first argument is the level
@@ -12,13 +12,13 @@ Logger.enable(false);
12
12
  const checkSingle = (_level, _value) => {
13
13
  expect(_level.isRange).false;
14
14
  expect(_level.val).equals(_value);
15
- expect(_level.low).undefined;
16
- expect(_level.up).undefined;
15
+ expect(_level.low).is.null;
16
+ expect(_level.up).is.null;
17
17
  };
18
18
 
19
19
  const checkRange = (_level, _value1, _value2) => {
20
20
  expect(_level.isRange).true;
21
- expect(_level.val).undefined;
21
+ expect(_level.val).is.null;
22
22
  expect(_level.low).equals(Math.min(_value1, _value2));
23
23
  expect(_level.up).equals(Math.max(_value1, _value2));
24
24
  };
@@ -19,6 +19,9 @@ class GraphNode {
19
19
  /** @type {?T} */
20
20
  builtFrom = null;
21
21
 
22
+ /** @type {boolean} */
23
+ io = false;
24
+
22
25
  /**
23
26
  * @param {Coordinates} coords
24
27
  * @param {T} builtFrom
@@ -74,6 +77,7 @@ class GraphNode {
74
77
  const node = new GraphNode(this.coords);
75
78
  node.edges = this.edges.slice(0);
76
79
  node.builtFrom = this.builtFrom;
80
+ node.io = this.io;
77
81
  return node;
78
82
  }
79
83
 
@@ -94,7 +98,7 @@ class GraphNode {
94
98
  }
95
99
 
96
100
  generateLevelFromEdges() {
97
- let tmpLevel;
101
+ let tmpLevel = null;
98
102
  for (let i = 0; i < this.edges.length; i++) {
99
103
  const edge = this.edges[i];
100
104
  if (edge.level) {
@@ -196,6 +200,16 @@ class GraphNode {
196
200
  return true;
197
201
  }
198
202
 
203
+ /**
204
+ * Set node.io to true for nodes that make the link between
205
+ * indoor and outdoor edges
206
+ */
207
+ checkIO() {
208
+ this.io = this._coords.level !== null
209
+ && this.edges.some(edge => edge.level === null);
210
+ return true;
211
+ }
212
+
199
213
  /**
200
214
  * @param {GraphNode[]} nodes
201
215
  */
@@ -208,10 +222,15 @@ class GraphNode {
208
222
  // In some cases, node levels cannot be retrieve just using adjacent edges
209
223
  // (e.g stairs without network at one of its bounds)
210
224
  // To infer this node level, we use inferNodeLevelByRecursion()
211
- return nodes.reduce((acc, node) => acc
225
+ const res = nodes.reduce((acc, node) => acc
212
226
  && node.inferNodeLevelByNeighboors()
213
227
  && node.inferNodeLevelByRecursion()
214
228
  , true);
229
+
230
+ // Finally define nodes that are links between indoor and outdoor
231
+ nodes.forEach(node => node.checkIO());
232
+
233
+ return res;
215
234
  }
216
235
  }
217
236
 
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable complexity */
2
2
  /* eslint-disable max-statements */
3
3
  import { diffAngleLines } from '@wemap/maths';
4
+ import { GraphEdge } from '@wemap/geo';
4
5
 
5
6
  import Constants from '../Constants.js';
6
7
  import Coordinates from '../coordinates/Coordinates.js';
@@ -71,6 +72,12 @@ class MapMatching {
71
72
  * @returns {boolean} an array of two elements.
72
73
  * First is true if projection will be used on the specified edge, false otherwise.
73
74
  * Second is true if projection will be used on the nodes of the specified edge, false otherwise.
75
+ *
76
+ * @param {GraphEdge} edge
77
+ * @param {Coordinates} location
78
+ * @param {boolean} useBearing
79
+ * @param {boolean} useMultiLevelSegments
80
+ * @param {Function} acceptEdgeFn
74
81
  */
75
82
  _shouldProjectOnEdgeAndNodes(edge, location, useBearing, useMultiLevelSegments, acceptEdgeFn) {
76
83
 
@@ -95,11 +102,11 @@ class MapMatching {
95
102
  checkEdge = false;
96
103
  }
97
104
 
98
- if (!Level.equalsTo(location.level, edge.node1.coords.level)) {
105
+ if (!Level.equalsTo(location.level, edge.node1.coords.level) && !edge.node1.io) {
99
106
  checkNode1 = false;
100
107
  }
101
108
 
102
- if (!Level.equalsTo(location.level, edge.node2.coords.level)) {
109
+ if (!Level.equalsTo(location.level, edge.node2.coords.level) && !edge.node2.io) {
103
110
  checkNode2 = false;
104
111
  }
105
112
 
@@ -36,8 +36,16 @@ class GraphItinerary {
36
36
 
37
37
  itinerary.nodes = networkNodes.map(node => {
38
38
  const newNode = node.clone();
39
+
39
40
  // Remove node edges, they will be added later.
40
41
  newNode.edges = [];
42
+
43
+ // Let consider io nodes level = null for GraphItinerary
44
+ if (newNode.io) {
45
+ newNode.coords = newNode.coords.clone();
46
+ newNode.coords.level = null;
47
+ }
48
+
41
49
  return newNode;
42
50
  });
43
51
 
@@ -86,12 +86,19 @@ class GraphRouter {
86
86
  const startNode = retrieveOrCreateNearestNode(start);
87
87
  const endNode = retrieveOrCreateNearestNode(end);
88
88
 
89
- const graphItinerary = this.getShortestPathBetweenGraphNodes(
90
- startNode,
91
- endNode,
92
- acceptEdgeFn,
93
- weightEdgeFn
94
- );
89
+ let graphItinerary;
90
+
91
+ if (startNode === endNode) {
92
+ graphItinerary = GraphItinerary.fromNetworkNodes([startNode], []);
93
+ } else {
94
+ graphItinerary = this.getShortestPathBetweenGraphNodes(
95
+ startNode,
96
+ endNode,
97
+ acceptEdgeFn,
98
+ weightEdgeFn
99
+ );
100
+ }
101
+
95
102
  graphItinerary.start = start instanceof GraphNode ? start.coords : start;
96
103
  graphItinerary.end = end instanceof GraphNode ? end.coords : end;
97
104
 
@@ -133,6 +133,14 @@ describe('GraphRouter', () => {
133
133
  expect(getNodesNames(itinerary)).deep.equals(['p0', 'p1', 'p2']);
134
134
  });
135
135
 
136
+ it('use common node', () => {
137
+ itinerary = getShortestPath(router, start, nodes[0]);
138
+ expect(getNodesNames(itinerary)).deep.equals(['p0']);
139
+
140
+ itinerary = getShortestPath(router, nodes[0], start);
141
+ expect(getNodesNames(itinerary)).deep.equals(['p0']);
142
+ });
143
+
136
144
  it('errors', () => {
137
145
 
138
146
  expect(() => getShortestPath(router, null, end)).throw(Error);
@@ -141,8 +149,6 @@ describe('GraphRouter', () => {
141
149
  expect(() => getShortestPath(router, start, new Coordinates(0, 0))).throw(NoRouteFoundError);
142
150
  expect(() => getShortestPath(router, new Coordinates(43.609226, 3.8841013), null)).throw(Error);
143
151
 
144
- expect(() => getShortestPath(router, start, nodes[0])).throw(NoRouteFoundError);
145
- expect(() => getShortestPath(router, nodes[0], start)).throw(NoRouteFoundError);
146
152
  });
147
153
  });
148
154