@wemap/geo 3.1.19 → 3.1.20
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 +2 -2
- package/src/Constants.js +1 -1
- package/src/Utils.js +55 -0
- package/src/Utils.spec.js +53 -0
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"directory": "packages/geo"
|
|
13
13
|
},
|
|
14
14
|
"name": "@wemap/geo",
|
|
15
|
-
"version": "3.1.
|
|
15
|
+
"version": "3.1.20",
|
|
16
16
|
"bugs": {
|
|
17
17
|
"url": "https://github.com/wemap/wemap-modules-js/issues"
|
|
18
18
|
},
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"lodash.isnumber": "^3.0.3",
|
|
34
34
|
"lodash.isstring": "^4.0.1"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "f7c26f0b16ec1d0d3ec5ca7efe6440424e18d3fb"
|
|
37
37
|
}
|
package/src/Constants.js
CHANGED
package/src/Utils.js
CHANGED
|
@@ -66,6 +66,61 @@ class Utils {
|
|
|
66
66
|
return sampledRoute;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Trim a route of Coordinates
|
|
71
|
+
* @param {Array.<Coordinates>} route ordered points
|
|
72
|
+
* @param {Coordinates} startPosition position where the trim starts. startPosition has to be on the route.
|
|
73
|
+
* @param {*} maxLength max route length
|
|
74
|
+
*/
|
|
75
|
+
static trimRoute(route, startPosition = route[0], length = Number.MAX_VALUE) {
|
|
76
|
+
|
|
77
|
+
const newRoute = [];
|
|
78
|
+
let previousPoint;
|
|
79
|
+
|
|
80
|
+
let currentPointIndex;
|
|
81
|
+
let cumulativeDistance = 0;
|
|
82
|
+
|
|
83
|
+
for (currentPointIndex = 1; currentPointIndex < route.length; currentPointIndex++) {
|
|
84
|
+
|
|
85
|
+
const p1 = route[currentPointIndex - 1];
|
|
86
|
+
const p2 = route[currentPointIndex];
|
|
87
|
+
|
|
88
|
+
if (startPosition.equalsTo(p1)) {
|
|
89
|
+
newRoute.push(p1);
|
|
90
|
+
previousPoint = p1;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const proj = startPosition.getSegmentProjection(p1, p2);
|
|
95
|
+
if (proj && proj.equalsTo(startPosition) && !proj.equalsTo(p2)) {
|
|
96
|
+
newRoute.push(proj);
|
|
97
|
+
previousPoint = proj;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!newRoute.length) {
|
|
103
|
+
throw new Error('startPosition is not on the route');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
while (currentPointIndex < route.length) {
|
|
107
|
+
const currentPoint = route[currentPointIndex];
|
|
108
|
+
const dist = previousPoint.distanceTo(currentPoint);
|
|
109
|
+
if (cumulativeDistance + dist >= length) {
|
|
110
|
+
const bearing = previousPoint.bearingTo(currentPoint);
|
|
111
|
+
const remainingLength = length - cumulativeDistance;
|
|
112
|
+
const end = previousPoint.destinationPoint(remainingLength, bearing);
|
|
113
|
+
newRoute.push(end);
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
newRoute.push(currentPoint);
|
|
117
|
+
previousPoint = currentPoint;
|
|
118
|
+
cumulativeDistance += dist;
|
|
119
|
+
currentPointIndex++;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return newRoute;
|
|
123
|
+
}
|
|
69
124
|
}
|
|
70
125
|
|
|
71
126
|
export default Utils;
|
package/src/Utils.spec.js
CHANGED
|
@@ -49,4 +49,57 @@ describe('Geo Utils', () => {
|
|
|
49
49
|
expect(samples.length).equals(2);
|
|
50
50
|
|
|
51
51
|
});
|
|
52
|
+
|
|
53
|
+
it('trimRoute', () => {
|
|
54
|
+
|
|
55
|
+
let newRoute;
|
|
56
|
+
|
|
57
|
+
const p1 = new Coordinates(45.0, 5.0);
|
|
58
|
+
const p2 = p1.destinationPoint(100, Math.PI / 4);
|
|
59
|
+
const p3 = p2.destinationPoint(49.9, Math.PI / 2);
|
|
60
|
+
const route = [p1, p2, p3];
|
|
61
|
+
|
|
62
|
+
expect(() => Utils.trimRoute(route, new Coordinates(0, 0))).throw(Error);
|
|
63
|
+
|
|
64
|
+
newRoute = Utils.trimRoute(route);
|
|
65
|
+
expect(newRoute.length).equals(3);
|
|
66
|
+
for (let i = 0; i < newRoute.length - 1; i++) {
|
|
67
|
+
expect(newRoute[i].equalsTo(route[i])).true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
newRoute = Utils.trimRoute(route, p1, 100);
|
|
71
|
+
expect(newRoute.length).equals(2);
|
|
72
|
+
expect(newRoute[0].equalsTo(p1)).true;
|
|
73
|
+
expect(newRoute[1].equalsTo(p2)).true;
|
|
74
|
+
|
|
75
|
+
newRoute = Utils.trimRoute(route, p1, 90);
|
|
76
|
+
expect(newRoute.length).equals(2);
|
|
77
|
+
expect(newRoute[0].equalsTo(p1)).true;
|
|
78
|
+
expect(newRoute[1].equalsTo(p1.destinationPoint(90, Math.PI / 4))).true;
|
|
79
|
+
|
|
80
|
+
newRoute = Utils.trimRoute(route, p2, 10);
|
|
81
|
+
expect(newRoute.length).equals(2);
|
|
82
|
+
expect(newRoute[0].equalsTo(p2)).true;
|
|
83
|
+
expect(newRoute[1].equalsTo(p2.destinationPoint(10, Math.PI / 2))).true;
|
|
84
|
+
|
|
85
|
+
const p4 = p1.destinationPoint(20, Math.PI / 4);
|
|
86
|
+
newRoute = Utils.trimRoute(route, p4);
|
|
87
|
+
expect(newRoute.length).equals(3);
|
|
88
|
+
expect(newRoute[0].equalsTo(p4)).true;
|
|
89
|
+
expect(newRoute[1].equalsTo(p2)).true;
|
|
90
|
+
expect(newRoute[2].equalsTo(p3)).true;
|
|
91
|
+
|
|
92
|
+
const p5 = p2.destinationPoint(20, Math.PI / 2);
|
|
93
|
+
newRoute = Utils.trimRoute(route, p4, 100);
|
|
94
|
+
expect(newRoute.length).equals(3);
|
|
95
|
+
expect(newRoute[0].equalsTo(p4)).true;
|
|
96
|
+
expect(newRoute[1].equalsTo(p2)).true;
|
|
97
|
+
expect(newRoute[2].equalsTo(p5)).true;
|
|
98
|
+
|
|
99
|
+
newRoute = Utils.trimRoute(route, p4, 200);
|
|
100
|
+
expect(newRoute.length).equals(3);
|
|
101
|
+
expect(newRoute[0].equalsTo(p4)).true;
|
|
102
|
+
expect(newRoute[1].equalsTo(p2)).true;
|
|
103
|
+
expect(newRoute[2].equalsTo(p3)).true;
|
|
104
|
+
});
|
|
52
105
|
});
|