@wemap/geo 0.3.6 → 0.3.8

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": "0.3.6",
15
+ "version": "0.3.8",
16
16
  "bugs": {
17
17
  "url": "https://github.com/wemap/wemap-utils-js/issues"
18
18
  },
@@ -31,5 +31,5 @@
31
31
  "lodash.isnumber": "^3.0.3",
32
32
  "lodash.isstring": "^4.0.1"
33
33
  },
34
- "gitHead": "32a1062949091aeb9ab107da3e75bc69589538ba"
34
+ "gitHead": "6e107eebc85ff97f17371feac903ddb53657678b"
35
35
  }
@@ -19,16 +19,7 @@ class WGS84UserPosition extends WGS84 {
19
19
 
20
20
  // Create a WGS84UserPosition with lat, lng, alt from WGS84 coordinates and
21
21
  // other fields from another WGS84UserPosition
22
- static fromWGS84(wgs84Coordinates, userPosition) {
23
-
24
- if (userPosition) {
25
- return new WGS84UserPosition(
26
- wgs84Coordinates.lat, wgs84Coordinates.lng,
27
- wgs84Coordinates.alt, wgs84Coordinates.level,
28
- userPosition.time, userPosition.accuracy,
29
- userPosition.bearing, userPosition.provider);
30
- }
31
-
22
+ static fromWGS84(wgs84Coordinates) {
32
23
  return new WGS84UserPosition(wgs84Coordinates.lat, wgs84Coordinates.lng,
33
24
  wgs84Coordinates.alt, wgs84Coordinates.level);
34
25
  }
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable max-statements */
2
- /* eslint-disable complexity */
2
+
3
3
  import isNumber from 'lodash.isnumber';
4
4
 
5
5
  import { rad2deg } from '@wemap/maths';
@@ -35,7 +35,53 @@ class MapMatching {
35
35
  this.network = network;
36
36
  }
37
37
 
38
- getProjection(location, useDistance = false, useBearing = false, useMultiLevelSegments = true, acceptEdgeFn = () => true) {
38
+ /**
39
+ * Check if the specified edge and its nodes can be used for projection
40
+ * @returns an array of two elements.
41
+ * First is true if projection will be used on the specified edge, false otherwise.
42
+ * Second is true if projection will be used on the nodes of the specified edge, false otherwise.
43
+ */
44
+ _shouldProjectOnEdgeAndNodes(edge, location, useBearing, useMultiLevelSegments, acceptEdgeFn) {
45
+
46
+ if (!acceptEdgeFn(edge)) {
47
+ // if edge selection is not verified
48
+ return [false, false];
49
+ }
50
+
51
+ if (location.level && !location.level.intersect(edge.level)
52
+ || !location.level && edge.level) {
53
+ // if location level is defined and do not intersect targeted level
54
+ return [false, false];
55
+ }
56
+
57
+ // ignore MultiLevelSegments if option used
58
+ const checkEdge = !(!useMultiLevelSegments && edge.level && edge.level.isRange);
59
+
60
+ if (useBearing) {
61
+ // if mapmatching bearing is enabled do not use nodes matching
62
+ if (checkEdge) {
63
+ // condition for optimisation
64
+ const edgeBearing = rad2deg(edge.bearing);
65
+ const diffAngle = MapMatching.diffAngle90(edgeBearing, location.bearing);
66
+ if (diffAngle > this._maxAngleBearing) {
67
+ // Do not try to project if angle is greater than the threshold
68
+ return [false, false];
69
+ }
70
+ }
71
+ return [checkEdge, false];
72
+ }
73
+
74
+ return [checkEdge, true];
75
+ }
76
+
77
+ static _updateProjectionLevelFromEdge = (_edge, _projection) => {
78
+ if (_edge.level) {
79
+ _projection.level = _edge.level.clone();
80
+ }
81
+ };
82
+
83
+ getProjection(location, useDistance = false, useBearing = false,
84
+ useMultiLevelSegments = true, acceptEdgeFn = () => true) {
39
85
 
40
86
  if (!this.network) {
41
87
  return null;
@@ -49,70 +95,52 @@ class MapMatching {
49
95
  return null;
50
96
  }
51
97
 
52
- const updateProjectionLevelFromEdge = (_edge, _projection) => {
53
- if (_edge.level) {
54
- _projection.level = _edge.level.clone();
55
- }
56
- };
57
-
58
98
  const projection = {};
59
99
  projection.origin = location;
60
100
  projection.distanceFromNearestElement = Number.MAX_VALUE;
61
101
 
102
+ const isProjectionBetter = (distanceOfNewProjection) => {
103
+ return distanceOfNewProjection < projection.distanceFromNearestElement
104
+ && (!useDistance || distanceOfNewProjection <= this._maxDistance);
105
+ };
106
+
62
107
  for (let i = 0; i < this.network.edges.length; i++) {
63
108
  const edge = this.network.edges[i];
64
109
 
65
- if (!acceptEdgeFn(edge)) {
66
- continue;
67
- }
110
+ const [checkEdge, checkNodes] = this._shouldProjectOnEdgeAndNodes(
111
+ edge, location, useBearing, useMultiLevelSegments, acceptEdgeFn);
68
112
 
69
- if (!useMultiLevelSegments && edge.level && edge.level.isRange) {
70
- continue;
71
- }
72
-
73
- if (location.level && !location.level.intersect(edge.level)
74
- || !location.level && edge.level) {
75
- continue;
76
- }
77
-
78
- if (useBearing) {
79
- const edgeBearing = rad2deg(edge.bearing);
80
- const diffAngle = MapMatching.diffAngle90(edgeBearing, location.bearing);
81
- if (diffAngle > this._maxAngleBearing) {
82
- continue;
83
- }
84
- } else {
113
+ if (checkNodes) {
85
114
  // Check node 1
86
115
  const distNode1 = location.distanceTo(edge.node1.coords);
87
- if (distNode1 < projection.distanceFromNearestElement
88
- && (!useDistance || distNode1 <= this._maxDistance)) {
116
+ if (isProjectionBetter(distNode1)) {
89
117
  projection.distanceFromNearestElement = distNode1;
90
118
  projection.nearestElement = edge.node1;
91
119
  projection.projection = edge.node1.coords.clone();
92
- updateProjectionLevelFromEdge(edge, projection.projection);
120
+ MapMatching._updateProjectionLevelFromEdge(edge, projection.projection);
93
121
  }
94
122
 
95
123
  // Check node 2
96
124
  const distNode2 = location.distanceTo(edge.node2.coords);
97
- if (distNode2 < projection.distanceFromNearestElement
98
- && (!useDistance || distNode2 <= this._maxDistance)) {
125
+ if (isProjectionBetter(distNode2)) {
99
126
  projection.distanceFromNearestElement = distNode2;
100
127
  projection.nearestElement = edge.node2;
101
128
  projection.projection = edge.node2.coords.clone();
102
- updateProjectionLevelFromEdge(edge, projection.projection);
129
+ MapMatching._updateProjectionLevelFromEdge(edge, projection.projection);
103
130
  }
104
131
  }
105
132
 
106
- // Check edge
107
- const segmentProjection = location.getProjection(edge);
108
- if (segmentProjection) {
109
- updateProjectionLevelFromEdge(edge, segmentProjection);
110
- const distEdge = location.distanceTo(segmentProjection);
111
- if (distEdge < projection.distanceFromNearestElement
112
- && (!useDistance || distEdge <= this._maxDistance)) {
113
- projection.distanceFromNearestElement = distEdge;
114
- projection.nearestElement = edge;
115
- projection.projection = segmentProjection.clone();
133
+ if (checkEdge) {
134
+ // Check edge
135
+ const segmentProjection = location.getProjection(edge);
136
+ if (segmentProjection) {
137
+ const distEdge = location.distanceTo(segmentProjection);
138
+ if (isProjectionBetter(distEdge)) {
139
+ projection.distanceFromNearestElement = distEdge;
140
+ projection.nearestElement = edge;
141
+ projection.projection = segmentProjection.clone();
142
+ MapMatching._updateProjectionLevelFromEdge(edge, projection.projection);
143
+ }
116
144
  }
117
145
  }
118
146
  }
@@ -23,7 +23,7 @@ class Network {
23
23
  );
24
24
  }
25
25
 
26
- toString(_nodeToStringFn, _edgeToStringFn) {
26
+ toDetailedString(_nodeToStringFn, _edgeToStringFn) {
27
27
 
28
28
  let nodeToStringFn = _nodeToStringFn;
29
29
  if (!nodeToStringFn) {