@wemap/geo 3.2.15 → 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 +2 -2
- package/src/Utils.js +38 -0
- package/src/Utils.spec.js +37 -0
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
|
},
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"@wemap/logger": "^3.2.3",
|
|
30
30
|
"@wemap/maths": "^3.2.15"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "01e6c2ed3b4f24b230cb6a774fe159e7b20b810a"
|
|
33
33
|
}
|
package/src/Utils.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
/* eslint-disable max-statements */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
deg2rad, positiveMod
|
|
5
|
+
} from '@wemap/maths';
|
|
6
|
+
|
|
2
7
|
import Constants from './Constants.js';
|
|
3
8
|
import Coordinates from './coordinates/Coordinates.js';
|
|
4
9
|
|
|
@@ -123,6 +128,39 @@ class Utils {
|
|
|
123
128
|
|
|
124
129
|
return newRoute;
|
|
125
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
|
+
}
|
|
126
164
|
}
|
|
127
165
|
|
|
128
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
|
});
|