@wemap/geo 3.2.6 → 3.2.16
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 +41 -1
- package/src/Utils.spec.js +37 -0
- package/src/coordinates/BoundingBox.js +2 -2
- package/src/coordinates/Coordinates.js +11 -8
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"directory": "packages/geo"
|
|
13
13
|
},
|
|
14
14
|
"name": "@wemap/geo",
|
|
15
|
-
"version": "3.2.
|
|
15
|
+
"version": "3.2.16",
|
|
16
16
|
"bugs": {
|
|
17
17
|
"url": "https://github.com/wemap/wemap-modules-js/issues"
|
|
18
18
|
},
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"license": "ISC",
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@wemap/logger": "^3.2.3",
|
|
30
|
-
"@wemap/maths": "^3.2.
|
|
30
|
+
"@wemap/maths": "^3.2.15"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "01e6c2ed3b4f24b230cb6a774fe159e7b20b810a"
|
|
33
33
|
}
|
package/src/Utils.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
/* eslint-disable max-statements */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
deg2rad, positiveMod
|
|
5
|
+
} from '@wemap/maths';
|
|
6
|
+
|
|
7
|
+
import Constants from './Constants.js';
|
|
2
8
|
import Coordinates from './coordinates/Coordinates.js';
|
|
3
9
|
|
|
4
10
|
class Utils {
|
|
@@ -106,7 +112,8 @@ class Utils {
|
|
|
106
112
|
while (currentPointIndex < route.length) {
|
|
107
113
|
const currentPoint = route[currentPointIndex];
|
|
108
114
|
const dist = previousPoint.distanceTo(currentPoint);
|
|
109
|
-
if (cumulativeDistance + dist >= length
|
|
115
|
+
if (cumulativeDistance + dist >= length
|
|
116
|
+
|| Math.abs(cumulativeDistance + dist - length) <= Constants.EPS_MM) {
|
|
110
117
|
const bearing = previousPoint.bearingTo(currentPoint);
|
|
111
118
|
const remainingLength = length - cumulativeDistance;
|
|
112
119
|
const end = previousPoint.destinationPoint(remainingLength, bearing);
|
|
@@ -121,6 +128,39 @@ class Utils {
|
|
|
121
128
|
|
|
122
129
|
return newRoute;
|
|
123
130
|
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @param {Coordinates[]} coords
|
|
134
|
+
* @param {number} precision
|
|
135
|
+
* @returns {Coordinates[]}
|
|
136
|
+
*/
|
|
137
|
+
static simplifyRoute(coords, precisionAngle = deg2rad(5)) {
|
|
138
|
+
|
|
139
|
+
const isClosed = (coords[0].equalsTo(coords[coords.length - 1]));
|
|
140
|
+
|
|
141
|
+
let newRoute = coords.slice(0, coords.length - (isClosed ? 1 : 0));
|
|
142
|
+
|
|
143
|
+
const len = newRoute.length;
|
|
144
|
+
for (let i = isClosed ? 0 : 1; i < len; i++) {
|
|
145
|
+
|
|
146
|
+
const p0 = coords[positiveMod(i - 1, len)];
|
|
147
|
+
const p1 = coords[i];
|
|
148
|
+
const p2 = coords[positiveMod(i + 1, len)];
|
|
149
|
+
|
|
150
|
+
const seg1Dir = p0.bearingTo(p1);
|
|
151
|
+
const seg2Dir = p1.bearingTo(p2);
|
|
152
|
+
|
|
153
|
+
if (Math.abs(seg2Dir - seg1Dir) < precisionAngle) {
|
|
154
|
+
newRoute = newRoute.filter(coord => coord !== p1);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (isClosed) {
|
|
159
|
+
newRoute.push(newRoute[0]);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return newRoute;
|
|
163
|
+
}
|
|
124
164
|
}
|
|
125
165
|
|
|
126
166
|
export default Utils;
|
package/src/Utils.spec.js
CHANGED
|
@@ -102,4 +102,41 @@ describe('Geo Utils', () => {
|
|
|
102
102
|
expect(newRoute[1].equalsTo(p2)).true;
|
|
103
103
|
expect(newRoute[2].equalsTo(p3)).true;
|
|
104
104
|
});
|
|
105
|
+
|
|
106
|
+
it('simplifyRoute', () => {
|
|
107
|
+
|
|
108
|
+
let route, routeSimplified;
|
|
109
|
+
|
|
110
|
+
route = [
|
|
111
|
+
new Coordinates(48.756308386983484, 2.0549525696601694),
|
|
112
|
+
new Coordinates(48.75630194782273, 2.054987452552288),
|
|
113
|
+
new Coordinates(48.756294588780854, 2.055026521391461),
|
|
114
|
+
new Coordinates(48.7563159759933, 2.0550474511267316),
|
|
115
|
+
new Coordinates(48.756350241508265, 2.0550837293345348)
|
|
116
|
+
];
|
|
117
|
+
|
|
118
|
+
routeSimplified = Utils.simplifyRoute(route);
|
|
119
|
+
expect(routeSimplified.length).equal(3);
|
|
120
|
+
expect(routeSimplified).to.include(route[0]);
|
|
121
|
+
expect(routeSimplified).to.not.include(route[1]);
|
|
122
|
+
expect(routeSimplified).to.include(route[2]);
|
|
123
|
+
expect(routeSimplified).to.not.include(route[3]);
|
|
124
|
+
expect(routeSimplified).to.include(route[4]);
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
route = [
|
|
128
|
+
new Coordinates(48.75627274161895, 2.054974197053283),
|
|
129
|
+
new Coordinates(48.756269751891594, 2.0549905921869933),
|
|
130
|
+
new Coordinates(48.756274912970845, 2.054992757591672),
|
|
131
|
+
new Coordinates(48.75628117553095, 2.0549584148199345),
|
|
132
|
+
new Coordinates(48.756276014452325, 2.054956249415256),
|
|
133
|
+
new Coordinates(48.75627274161895, 2.054974197053283)
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
routeSimplified = Utils.simplifyRoute(route);
|
|
137
|
+
expect(routeSimplified.length).equal(5);
|
|
138
|
+
expect(routeSimplified).to.not.include(route[0]);
|
|
139
|
+
expect(routeSimplified).to.not.include(route[5]);
|
|
140
|
+
|
|
141
|
+
});
|
|
105
142
|
});
|
|
@@ -6,10 +6,10 @@ class BoundingBox {
|
|
|
6
6
|
southWest;
|
|
7
7
|
|
|
8
8
|
constructor(northEast, southWest) {
|
|
9
|
-
this.southWest = southWest || null;
|
|
10
9
|
this.northEast = northEast || null;
|
|
10
|
+
this.southWest = southWest || null;
|
|
11
11
|
|
|
12
|
-
if (this.
|
|
12
|
+
if (this.northEast && this.southWest && this.getNorth() < this.getSouth()) {
|
|
13
13
|
throw new Error('Incorrect bounding box');
|
|
14
14
|
}
|
|
15
15
|
}
|
|
@@ -18,6 +18,8 @@ import Level from './Level.js';
|
|
|
18
18
|
*/
|
|
19
19
|
class Coordinates {
|
|
20
20
|
|
|
21
|
+
autoWrap = true;
|
|
22
|
+
|
|
21
23
|
constructor(lat, lng, alt, level) {
|
|
22
24
|
this.lat = lat;
|
|
23
25
|
this.lng = lng;
|
|
@@ -66,13 +68,8 @@ class Coordinates {
|
|
|
66
68
|
set lng(lng) {
|
|
67
69
|
if (typeof lng === 'number') {
|
|
68
70
|
this._lng = lng;
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
this._lng = this._lng % 360;
|
|
72
|
-
this._lng = (this._lng + 360) % 360;
|
|
73
|
-
if (this._lng > 180) {
|
|
74
|
-
this._lng -= 360;
|
|
75
|
-
}
|
|
71
|
+
if (this.autoWrap) {
|
|
72
|
+
this.wrap();
|
|
76
73
|
}
|
|
77
74
|
} else {
|
|
78
75
|
throw new Error('lng argument is not a number');
|
|
@@ -111,6 +108,12 @@ class Coordinates {
|
|
|
111
108
|
return output;
|
|
112
109
|
}
|
|
113
110
|
|
|
111
|
+
wrap() {
|
|
112
|
+
if (this._lng <= -180 || this._lng > 180) {
|
|
113
|
+
this._lng = Utils.wrap(this._lng, -180, 180);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
114
117
|
/**
|
|
115
118
|
* Compares two Coordinates
|
|
116
119
|
* @param {Coordinates} pos1 position 1
|
|
@@ -316,7 +319,7 @@ class Coordinates {
|
|
|
316
319
|
const projection = new Coordinates(poseCoordinates.lat, poseCoordinates.lng,
|
|
317
320
|
alt, Level.intersect(p1.level, p2.level));
|
|
318
321
|
|
|
319
|
-
if (Math.abs((p1.distanceTo(p2) - p1.distanceTo(projection) - p2.distanceTo(projection))) >
|
|
322
|
+
if (Math.abs((p1.distanceTo(p2) - p1.distanceTo(projection) - p2.distanceTo(projection))) > Constants.EPS_MM) {
|
|
320
323
|
return null;
|
|
321
324
|
}
|
|
322
325
|
|