@wemap/geo 3.1.14 → 3.1.19
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 +3 -3
- package/src/Utils.js +45 -20
- package/src/Utils.spec.js +33 -20
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.19",
|
|
16
16
|
"bugs": {
|
|
17
17
|
"url": "https://github.com/wemap/wemap-modules-js/issues"
|
|
18
18
|
},
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"license": "ISC",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@wemap/logger": "^3.0.0",
|
|
31
|
-
"@wemap/maths": "^3.1.
|
|
31
|
+
"@wemap/maths": "^3.1.15",
|
|
32
32
|
"lodash.isfinite": "^3.3.2",
|
|
33
33
|
"lodash.isnumber": "^3.0.3",
|
|
34
34
|
"lodash.isstring": "^4.0.1"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "4a4167fd2159c856b90e6a566595ae5945f92d2e"
|
|
37
37
|
}
|
package/src/Utils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable max-statements */
|
|
1
2
|
import Coordinates from './coordinates/Coordinates';
|
|
2
3
|
|
|
3
4
|
class Utils {
|
|
@@ -8,39 +9,63 @@ class Utils {
|
|
|
8
9
|
* @param {*} stepSize step size to sample
|
|
9
10
|
* @param {*} maxLength max route length to sample
|
|
10
11
|
*/
|
|
11
|
-
static sampleRoute(route, stepSize = 0.7,
|
|
12
|
+
static sampleRoute(route, stepSize = 0.7, startSampling = 0, length = Number.MAX_VALUE) {
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
let bearing, distance;
|
|
14
|
+
const endSampling = startSampling + length;
|
|
15
15
|
|
|
16
16
|
const sampledRoute = [];
|
|
17
|
-
let reportedDistance = 0;
|
|
18
|
-
let totalDistance = 0;
|
|
19
17
|
|
|
20
|
-
|
|
18
|
+
let lastSample = null;
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
let totalDistanceTraveled = 0;
|
|
21
|
+
let distanceToNextSample;
|
|
22
|
+
let startFound = false;
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
distance = p1.distanceTo(p2);
|
|
24
|
+
for (let segmentIndex = 0; segmentIndex < route.length - 1; segmentIndex++) {
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
26
|
+
const p1 = route[segmentIndex];
|
|
27
|
+
const p2 = route[segmentIndex + 1];
|
|
28
|
+
const segmentSize = p1.distanceTo(p2);
|
|
29
|
+
const segmentBearing = p1.bearingTo(p2);
|
|
30
|
+
|
|
31
|
+
let distanceTraveledOnSegment = 0;
|
|
32
|
+
|
|
33
|
+
if (!startFound) {
|
|
34
|
+
if (startSampling < totalDistanceTraveled + segmentSize) {
|
|
35
|
+
startFound = true;
|
|
36
|
+
distanceToNextSample = startSampling - totalDistanceTraveled;
|
|
37
|
+
} else {
|
|
38
|
+
totalDistanceTraveled += segmentSize;
|
|
39
|
+
continue;
|
|
36
40
|
}
|
|
37
|
-
reportedDistance = (ratio - 1) * distance;
|
|
38
41
|
}
|
|
42
|
+
|
|
43
|
+
lastSample = p1;
|
|
44
|
+
while (distanceTraveledOnSegment + distanceToNextSample < segmentSize
|
|
45
|
+
&& totalDistanceTraveled + distanceToNextSample <= endSampling) {
|
|
46
|
+
|
|
47
|
+
const newPoint = lastSample.destinationPoint(distanceToNextSample, segmentBearing);
|
|
48
|
+
newPoint.bearing = segmentBearing;
|
|
49
|
+
sampledRoute.push(newPoint);
|
|
50
|
+
lastSample = newPoint;
|
|
51
|
+
|
|
52
|
+
distanceTraveledOnSegment += distanceToNextSample;
|
|
53
|
+
totalDistanceTraveled += distanceToNextSample;
|
|
54
|
+
distanceToNextSample = stepSize;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (totalDistanceTraveled + distanceToNextSample > endSampling) {
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const rest = segmentSize - distanceTraveledOnSegment;
|
|
62
|
+
totalDistanceTraveled += rest;
|
|
63
|
+
distanceToNextSample -= rest;
|
|
39
64
|
}
|
|
40
|
-
sampledRoute.push(p2);
|
|
41
65
|
|
|
42
66
|
return sampledRoute;
|
|
43
67
|
}
|
|
68
|
+
|
|
44
69
|
}
|
|
45
70
|
|
|
46
71
|
export default Utils;
|
package/src/Utils.spec.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
/* eslint-disable max-statements */
|
|
2
|
+
import chai from 'chai';
|
|
3
|
+
import chaiAlmost from 'chai-almost';
|
|
2
4
|
|
|
3
5
|
import Utils from './Utils';
|
|
4
6
|
import Coordinates from './coordinates/Coordinates';
|
|
5
7
|
|
|
8
|
+
const expect = chai.expect;
|
|
9
|
+
chai.use(chaiAlmost(1e-3));
|
|
6
10
|
|
|
7
11
|
describe('Geo Utils', () => {
|
|
8
12
|
|
|
@@ -10,30 +14,39 @@ describe('Geo Utils', () => {
|
|
|
10
14
|
|
|
11
15
|
let samples;
|
|
12
16
|
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
new Coordinates(45.0003702, 5.0011008)
|
|
18
|
-
];
|
|
17
|
+
const p1 = new Coordinates(45.0, 5.0);
|
|
18
|
+
const p2 = p1.destinationPoint(100, Math.PI / 4);
|
|
19
|
+
const p3 = p2.destinationPoint(49.9, Math.PI / 2);
|
|
20
|
+
const route = [p1, p2, p3];
|
|
19
21
|
|
|
20
|
-
samples = Utils.sampleRoute(route
|
|
21
|
-
expect(samples.length).equals(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// Do not consider the turn, only straight lines
|
|
26
|
-
if (i === 9) {
|
|
22
|
+
samples = Utils.sampleRoute(route);
|
|
23
|
+
expect(samples.length).equals(Math.ceil(149.9 / 0.7));
|
|
24
|
+
for (let i = 0; i < samples.length - 1; i++) {
|
|
25
|
+
// Ignore the crow flies for the turn
|
|
26
|
+
if (i === Math.floor(100 / 0.7)) {
|
|
27
27
|
i++;
|
|
28
28
|
}
|
|
29
|
-
expect(
|
|
29
|
+
expect(samples[i].distanceTo(samples[i + 1])).to.almost.equal(0.7);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
samples = Utils.sampleRoute(route, 10
|
|
33
|
-
expect(samples.length).equals(
|
|
32
|
+
samples = Utils.sampleRoute(route, 10);
|
|
33
|
+
expect(samples.length).equals(15);
|
|
34
|
+
for (let i = 0; i < samples.length - 1; i++) {
|
|
35
|
+
expect(samples[i].distanceTo(samples[i + 1])).to.almost.equal(i === 14 ? 9.9 : 10);
|
|
36
|
+
}
|
|
34
37
|
|
|
35
|
-
samples = Utils.sampleRoute(route);
|
|
36
|
-
expect(samples.length).equals(
|
|
37
|
-
|
|
38
|
+
samples = Utils.sampleRoute(route, 10, 0, 99.9);
|
|
39
|
+
expect(samples.length).equals(10);
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
samples = Utils.sampleRoute(route, 10, 50, 12);
|
|
43
|
+
expect(samples.length).equals(2);
|
|
38
44
|
|
|
45
|
+
samples = Utils.sampleRoute(route, 10, 50, 100);
|
|
46
|
+
expect(samples.length).equals(10);
|
|
47
|
+
|
|
48
|
+
samples = Utils.sampleRoute(route, 10, 110, 12);
|
|
49
|
+
expect(samples.length).equals(2);
|
|
50
|
+
|
|
51
|
+
});
|
|
39
52
|
});
|