@wemap/geo 1.0.4 → 2.7.0
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/index.js +2 -0
- package/package.json +7 -6
- package/src/Constants.js +11 -1
- package/src/Utils.js +46 -0
- package/src/Utils.spec.js +39 -0
- package/src/coordinates/Level.js +1 -0
- package/src/coordinates/Level.spec.js +39 -30
- package/src/coordinates/WGS84.js +150 -134
- package/src/coordinates/WGS84.spec.js +268 -0
- package/src/coordinates/WGS84UserPosition.js +136 -23
- package/src/coordinates/WGS84UserPosition.spec.js +247 -0
- package/src/rotations/Attitude.js +56 -30
- package/src/rotations/Attitude.spec.js +82 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import chai from 'chai';
|
|
2
|
+
import chaiAlmost from 'chai-almost';
|
|
3
|
+
|
|
4
|
+
import WGS84 from './WGS84';
|
|
5
|
+
import Level from './Level';
|
|
6
|
+
import Constants from '../Constants';
|
|
7
|
+
|
|
8
|
+
const expect = chai.expect;
|
|
9
|
+
chai.use(chaiAlmost());
|
|
10
|
+
|
|
11
|
+
describe('WGS84', () => {
|
|
12
|
+
|
|
13
|
+
it('creation', () => {
|
|
14
|
+
|
|
15
|
+
expect(() => new WGS84()).throw(Error);
|
|
16
|
+
|
|
17
|
+
expect(() => new WGS84(45)).throw(Error);
|
|
18
|
+
expect(() => new WGS84(45, 5)).not.throw(Error);
|
|
19
|
+
expect(() => new WGS84(-93, 5)).throw(Error);
|
|
20
|
+
expect(() => new WGS84(45, 202)).throw(Error);
|
|
21
|
+
|
|
22
|
+
expect(() => new WGS84(45, 5, true)).throw(Error);
|
|
23
|
+
expect(() => new WGS84(45, 5, false)).throw(Error);
|
|
24
|
+
expect(() => new WGS84(45, 5, null)).not.throw(Error);
|
|
25
|
+
expect(() => new WGS84(45, 5, 0)).not.throw(Error);
|
|
26
|
+
expect(() => new WGS84(45, 5, Number.POSITIVE_INFINITY)).throw(Error);
|
|
27
|
+
|
|
28
|
+
expect(() => new WGS84(45, 5, null, null)).not.throw(Error);
|
|
29
|
+
expect(() => new WGS84(45, 5, null, true)).throw(Error);
|
|
30
|
+
expect(() => new WGS84(45, 5, null, new Level(1))).not.throw(Error);
|
|
31
|
+
expect(() => new WGS84(45, 5, null, new Level(1, 2))).not.throw(Error);
|
|
32
|
+
expect(() => new WGS84(45, 5, 0, new Level(1))).not.throw(Error);
|
|
33
|
+
|
|
34
|
+
const level = new Level(2);
|
|
35
|
+
const position = new WGS84(45, 5, 0, level);
|
|
36
|
+
expect(position.lat).equals(45);
|
|
37
|
+
expect(position.lng).equals(5);
|
|
38
|
+
expect(position.alt).equals(0);
|
|
39
|
+
expect(position.level).equals(level);
|
|
40
|
+
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('update', () => {
|
|
44
|
+
const position = new WGS84(0, 0);
|
|
45
|
+
position.lat = 45;
|
|
46
|
+
position.lng = 5;
|
|
47
|
+
position.alt = 0;
|
|
48
|
+
position.level = new Level(2);
|
|
49
|
+
expect(position.lat).equals(45);
|
|
50
|
+
expect(position.lng).equals(5);
|
|
51
|
+
expect(position.alt).equals(0);
|
|
52
|
+
expect(Level.equalsTo(position.level, new Level(2))).true;
|
|
53
|
+
expect(position._ecef).equals(null);
|
|
54
|
+
expect(position.ecef).is.not.null;
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('clone', () => {
|
|
58
|
+
const level = new Level(0);
|
|
59
|
+
const position = new WGS84(45, 5, 0, level);
|
|
60
|
+
const clonePosition = position.clone();
|
|
61
|
+
|
|
62
|
+
expect(clonePosition.lat).equals(45);
|
|
63
|
+
expect(clonePosition.lng).equals(5);
|
|
64
|
+
expect(clonePosition.alt).equals(0);
|
|
65
|
+
expect(Level.equalsTo(clonePosition.level, level)).true;
|
|
66
|
+
expect(clonePosition.level).not.equals(level);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('equalsTo', () => {
|
|
70
|
+
let position;
|
|
71
|
+
|
|
72
|
+
position = new WGS84(0, 0);
|
|
73
|
+
expect(WGS84.equalsTo(position, new WGS84(0, 0))).true;
|
|
74
|
+
expect(WGS84.equalsTo(position, new WGS84(0, 0, 0))).false;
|
|
75
|
+
expect(WGS84.equalsTo(position, new WGS84(0, 0, null, new Level(0)))).false;
|
|
76
|
+
expect(WGS84.equalsTo(position, new WGS84(0, 0, null, null))).true;
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
position = new WGS84(45, 5, 0, new Level(0));
|
|
80
|
+
expect(WGS84.equalsTo(position, new WGS84(45, 5, 0, new Level(0)))).true;
|
|
81
|
+
expect(WGS84.equalsTo(position, new WGS84(45, 5, 1, new Level(0)))).false;
|
|
82
|
+
expect(WGS84.equalsTo(position, new WGS84(45, 5, 0, new Level(1)))).false;
|
|
83
|
+
expect(WGS84.equalsTo(position, {
|
|
84
|
+
lat: 45,
|
|
85
|
+
lng: 5
|
|
86
|
+
})).false;
|
|
87
|
+
expect(WGS84.equalsTo({
|
|
88
|
+
lat: 45,
|
|
89
|
+
lng: 5
|
|
90
|
+
}, position)).false;
|
|
91
|
+
expect(WGS84.equalsTo(position, null)).false;
|
|
92
|
+
expect(WGS84.equalsTo(null, position)).false;
|
|
93
|
+
expect(WGS84.equalsTo(null, null)).true;
|
|
94
|
+
|
|
95
|
+
expect(position.equalsTo(new WGS84(45, 5, 0, new Level(0)))).true;
|
|
96
|
+
expect(position.equalsTo(new WGS84(45, 5, 0, new Level(1)))).false;
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('toString', () => {
|
|
100
|
+
expect(new WGS84(45, 5).toString()).equals('[45.0000000, 5.0000000]');
|
|
101
|
+
expect(new WGS84(45, 5, 0).toString()).equals('[45.0000000, 5.0000000, 0.00]');
|
|
102
|
+
expect(new WGS84(45, 5, 1).toString()).equals('[45.0000000, 5.0000000, 1.00]');
|
|
103
|
+
expect(new WGS84(45, 5, null, new Level(0)).toString())
|
|
104
|
+
.equals('[45.0000000, 5.0000000, [0]]');
|
|
105
|
+
expect(new WGS84(45, 5, null, new Level(1)).toString())
|
|
106
|
+
.equals('[45.0000000, 5.0000000, [1]]');
|
|
107
|
+
expect(new WGS84(45, 5, 2, new Level(1)).toString())
|
|
108
|
+
.equals('[45.0000000, 5.0000000, 2.00, [1]]');
|
|
109
|
+
expect(new WGS84(45, 5, 2, new Level(1, 2)).toString())
|
|
110
|
+
.equals('[45.0000000, 5.0000000, 2.00, [1;2]]');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
it('move/destination', () => {
|
|
115
|
+
let position;
|
|
116
|
+
|
|
117
|
+
position = new WGS84(0, 0, 0, new Level(0)).move(100000, 0);
|
|
118
|
+
expect(WGS84.equalsTo(position, new WGS84(0.8983152841195214, 0, 0, new Level(0)))).true;
|
|
119
|
+
|
|
120
|
+
position = new WGS84(0, 0, 0, new Level(0)).move(100000, Math.PI / 2);
|
|
121
|
+
expect(WGS84.equalsTo(position, new WGS84(0, 0.8983152841195212, 0, new Level(0)))).true;
|
|
122
|
+
|
|
123
|
+
position = new WGS84(0, 0, 0).move(100000, 0, 10);
|
|
124
|
+
expect(WGS84.equalsTo(position, new WGS84(0.8983152841195214, 0, 10))).true;
|
|
125
|
+
|
|
126
|
+
expect(() => new WGS84(0, 0).move(100000, 0, 10)).throw(Error);
|
|
127
|
+
|
|
128
|
+
const level = new Level(1);
|
|
129
|
+
position = new WGS84(0, 0, null, level);
|
|
130
|
+
const newPosition = position.destinationPoint(100000, 0);
|
|
131
|
+
expect(newPosition).not.equals(position);
|
|
132
|
+
expect(newPosition.level).not.equal(position.level);
|
|
133
|
+
expect(WGS84.equalsTo(newPosition, new WGS84(0.8983152841195214, 0, null, level))).true;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
it('ecef', () => {
|
|
138
|
+
let position;
|
|
139
|
+
|
|
140
|
+
position = new WGS84(45, 5, 10);
|
|
141
|
+
expect(position.ecef).deep.equals(
|
|
142
|
+
[4492868.9655526765, 393075.10119330435, 4510030.995104634]
|
|
143
|
+
);
|
|
144
|
+
expect(WGS84.equalsTo(WGS84.fromECEF(position.ecef), position)).true;
|
|
145
|
+
expect(position.enuToEcefRotation).deep.equals(
|
|
146
|
+
[0.6241639651811595, 0.2585371795226045, 0.2821438218554906, 0.6811554412633039]
|
|
147
|
+
);
|
|
148
|
+
expect(position.ecefToEnuRotation).deep.equals(
|
|
149
|
+
[0.6241639651811595, -0.2585371795226045, -0.2821438218554906, -0.6811554412633039]
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
position = new WGS84(-15, 25, 1200);
|
|
153
|
+
expect(position.ecef).deep.equals([5584638.098155466, 2604159.5131940604, -1651093.9107271794]);
|
|
154
|
+
expect(WGS84.equalsTo(WGS84.fromECEF(position.ecef), position)).true;
|
|
155
|
+
expect(position.enuToEcefRotation).deep.equals(
|
|
156
|
+
[0.32708727738303844, 0.42626843901912514, 0.6691074207087071, 0.5134241817667833]
|
|
157
|
+
);
|
|
158
|
+
expect(position.ecefToEnuRotation).deep.equals(
|
|
159
|
+
[0.32708727738303844, -0.42626843901912514, -0.6691074207087071, -0.5134241817667833]
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
it('getSegmentProjection', () => {
|
|
166
|
+
let p1, p2, projection;
|
|
167
|
+
|
|
168
|
+
p1 = new WGS84(0, 0);
|
|
169
|
+
|
|
170
|
+
projection = new WGS84(45, 5).getSegmentProjection(p1, p1);
|
|
171
|
+
expect(projection).is.null;
|
|
172
|
+
|
|
173
|
+
p2 = new WGS84(0, 30);
|
|
174
|
+
|
|
175
|
+
projection = new WGS84(45, 5).getSegmentProjection(p1, p2);
|
|
176
|
+
expect(WGS84.equalsTo(projection, new WGS84(0, 5))).true;
|
|
177
|
+
|
|
178
|
+
projection = new WGS84(-10, 30).getSegmentProjection(p1, p2);
|
|
179
|
+
expect(WGS84.equalsTo(projection, new WGS84(0, 30))).true;
|
|
180
|
+
|
|
181
|
+
projection = new WGS84(10, 50).getSegmentProjection(p1, p2);
|
|
182
|
+
expect(projection).is.null;
|
|
183
|
+
|
|
184
|
+
p1 = new WGS84(0, 0);
|
|
185
|
+
p2 = new WGS84(30, 10);
|
|
186
|
+
|
|
187
|
+
projection = new WGS84(10, -30).getSegmentProjection(p1, p2);
|
|
188
|
+
expect(WGS84.equalsTo(projection, new WGS84(1.5735557770204767, 0.473398796436419))).true;
|
|
189
|
+
|
|
190
|
+
projection = new WGS84(0, 10).getSegmentProjection(p1, p2);
|
|
191
|
+
expect(WGS84.equalsTo(projection, new WGS84(2.7840283663153627, 0.8380346611000419))).true;
|
|
192
|
+
|
|
193
|
+
projection = new WGS84(45, 5).getSegmentProjection(p1, p2);
|
|
194
|
+
expect(projection).is.null;
|
|
195
|
+
|
|
196
|
+
p1 = new WGS84(0, 0, 10);
|
|
197
|
+
p2 = new WGS84(0, 30, 15);
|
|
198
|
+
|
|
199
|
+
projection = new WGS84(45, 5, 0).getSegmentProjection(p1, p2);
|
|
200
|
+
expect(WGS84.equalsTo(projection, new WGS84(0, 5, 12.5))).true;
|
|
201
|
+
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('distance', () => {
|
|
205
|
+
|
|
206
|
+
expect(WGS84.distanceBetween(new WGS84(0, 0), new WGS84(0, 5)))
|
|
207
|
+
.closeTo(556597.4539663679, Constants.EPS_MM);
|
|
208
|
+
expect(WGS84.distanceBetween(new WGS84(0, 0), new WGS84(10, 5)))
|
|
209
|
+
.closeTo(1243322.1397654496, Constants.EPS_MM);
|
|
210
|
+
expect(WGS84.distanceBetween(new WGS84(10, 10), new WGS84(10.003, 9.9995)))
|
|
211
|
+
.closeTo(338.4269853899686, Constants.EPS_MM);
|
|
212
|
+
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('bearing', () => {
|
|
216
|
+
|
|
217
|
+
expect(WGS84.bearingTo(new WGS84(0, 0), new WGS84(0, 5)))
|
|
218
|
+
.closeTo(1.5707963267948966, 1e-5);
|
|
219
|
+
expect(WGS84.bearingTo(new WGS84(0, 0), new WGS84(10, 5)))
|
|
220
|
+
.closeTo(0.4590649881870773, 1e-5);
|
|
221
|
+
expect(WGS84.bearingTo(new WGS84(10, 10), new WGS84(10.003, 9.9995)))
|
|
222
|
+
.closeTo(-0.16268256717197785, 1e-5);
|
|
223
|
+
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('json', () => {
|
|
227
|
+
|
|
228
|
+
expect(new WGS84(0, 0).toJson()).to.deep.equal({
|
|
229
|
+
lat: 0,
|
|
230
|
+
lng: 0
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
expect(new WGS84(5, -10, 2).toJson()).to.deep.equal({
|
|
234
|
+
lat: 5,
|
|
235
|
+
lng: -10,
|
|
236
|
+
alt: 2
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
expect(new WGS84(5, -10, null, new Level(1, 2)).toJson()).to.deep.equal({
|
|
240
|
+
lat: 5,
|
|
241
|
+
lng: -10,
|
|
242
|
+
level: '1;2'
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
expect(new WGS84(5, -10, 3, new Level(1, 2)).toJson()).to.deep.equal({
|
|
246
|
+
lat: 5,
|
|
247
|
+
lng: -10,
|
|
248
|
+
alt: 3,
|
|
249
|
+
level: '1;2'
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
let position;
|
|
253
|
+
|
|
254
|
+
position = new WGS84(0, 0);
|
|
255
|
+
expect(WGS84.equalsTo(WGS84.fromJson(position.toJson()), position)).true;
|
|
256
|
+
|
|
257
|
+
position = new WGS84(5, -10, 2);
|
|
258
|
+
expect(WGS84.equalsTo(WGS84.fromJson(position.toJson()), position)).true;
|
|
259
|
+
|
|
260
|
+
position = new WGS84(5, -10, null, new Level(1, 2));
|
|
261
|
+
expect(WGS84.equalsTo(WGS84.fromJson(position.toJson()), position)).true;
|
|
262
|
+
|
|
263
|
+
position = new WGS84(5, -10, 3, new Level(1, 2));
|
|
264
|
+
expect(WGS84.equalsTo(WGS84.fromJson(position.toJson()), position)).true;
|
|
265
|
+
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
});
|
|
@@ -1,20 +1,89 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import isNumber from 'lodash.isnumber';
|
|
2
|
+
import isFinite from 'lodash.isfinite';
|
|
3
|
+
import isString from 'lodash.isstring';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
import WGS84 from './WGS84';
|
|
6
|
+
import Constants from '../Constants';
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* A WGS84 User Position is a WGS84 position with specific data related to user (bearing, provider, time, accuracy)
|
|
8
10
|
*/
|
|
9
11
|
class WGS84UserPosition extends WGS84 {
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
_time = null;
|
|
14
|
+
_accuracy = null;
|
|
15
|
+
_bearing = null;
|
|
16
|
+
_provider = null;
|
|
17
|
+
|
|
18
|
+
constructor(lat, lng, alt, level, time,
|
|
19
|
+
accuracy, bearing, provider) {
|
|
13
20
|
super(lat, lng, alt, level);
|
|
14
|
-
this.bearing = bearing;
|
|
15
|
-
this.provider = provider;
|
|
16
21
|
this.time = time;
|
|
17
22
|
this.accuracy = accuracy;
|
|
23
|
+
this.bearing = bearing;
|
|
24
|
+
this.provider = provider;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get time() {
|
|
28
|
+
return this._time;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
set time(time) {
|
|
32
|
+
if (isNumber(time) && isFinite(time) && time >= 0) {
|
|
33
|
+
this._time = time;
|
|
34
|
+
} else {
|
|
35
|
+
if (typeof time !== 'undefined' && time !== null) {
|
|
36
|
+
throw new Error('time argument is not a positive number');
|
|
37
|
+
}
|
|
38
|
+
this._time = null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
get accuracy() {
|
|
44
|
+
return this._accuracy;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
set accuracy(accuracy) {
|
|
48
|
+
if (isNumber(accuracy) && isFinite(accuracy) && accuracy >= 0) {
|
|
49
|
+
this._accuracy = accuracy;
|
|
50
|
+
} else {
|
|
51
|
+
if (typeof accuracy !== 'undefined' && accuracy !== null) {
|
|
52
|
+
throw new Error('accuracy argument is not a positive number');
|
|
53
|
+
}
|
|
54
|
+
this._accuracy = null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
get bearing() {
|
|
60
|
+
return this._bearing;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
set bearing(bearing) {
|
|
64
|
+
if (isNumber(bearing) && isFinite(bearing)) {
|
|
65
|
+
this._bearing = bearing % (2 * Math.PI);
|
|
66
|
+
} else {
|
|
67
|
+
if (typeof bearing !== 'undefined' && bearing !== null) {
|
|
68
|
+
throw new Error('bearing argument is not a number');
|
|
69
|
+
}
|
|
70
|
+
this._bearing = null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get provider() {
|
|
75
|
+
return this._provider;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
set provider(provider) {
|
|
79
|
+
if (isString(provider)) {
|
|
80
|
+
this._provider = provider;
|
|
81
|
+
} else {
|
|
82
|
+
if (typeof provider !== 'undefined' && provider !== null) {
|
|
83
|
+
throw new Error('provider is not a string');
|
|
84
|
+
}
|
|
85
|
+
this._provider = null;
|
|
86
|
+
}
|
|
18
87
|
}
|
|
19
88
|
|
|
20
89
|
// Create a WGS84UserPosition with lat, lng, alt from WGS84 coordinates and
|
|
@@ -25,28 +94,72 @@ class WGS84UserPosition extends WGS84 {
|
|
|
25
94
|
}
|
|
26
95
|
|
|
27
96
|
clone() {
|
|
28
|
-
|
|
29
|
-
|
|
97
|
+
const cloned = WGS84UserPosition.fromWGS84(super.clone());
|
|
98
|
+
cloned.time = this.time;
|
|
99
|
+
cloned.accuracy = this.accuracy;
|
|
100
|
+
cloned.bearing = this.bearing;
|
|
101
|
+
cloned.provider = this.provider;
|
|
102
|
+
return cloned;
|
|
30
103
|
}
|
|
31
104
|
|
|
32
|
-
|
|
33
|
-
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Compares two WGS84UserPosition
|
|
108
|
+
* @param {WGS84UserPosition} pos1 position 1
|
|
109
|
+
* @param {WGS84UserPosition} pos2 position 2
|
|
110
|
+
* @param {Number} eps latitude and longitude epsilon in degrees (default: 1e-8 [~1mm at lat=0])
|
|
111
|
+
* @param {Number} epsAlt altitude epsilon in meters (default: 1e-3 [= 1mm])
|
|
112
|
+
*/
|
|
113
|
+
static equalsTo(pos1, pos2, eps = Constants.EPS_DEG_MM, epsAlt = Constants.EPS_MM) {
|
|
114
|
+
|
|
115
|
+
// Handle null comparison
|
|
116
|
+
if (pos1 === null && pos1 === pos2) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (!(pos1 instanceof WGS84UserPosition) || !(pos2 instanceof WGS84UserPosition)) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!super.equalsTo(pos1, pos2, eps, epsAlt)) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return pos1.time === pos2.time
|
|
129
|
+
&& pos1.accuracy === pos2.accuracy
|
|
130
|
+
&& pos1.bearing === pos2.bearing
|
|
131
|
+
&& pos1.provider === pos2.provider;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
equalsTo(other) {
|
|
135
|
+
return WGS84UserPosition.equalsTo(this, other);
|
|
34
136
|
}
|
|
35
137
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
138
|
+
|
|
139
|
+
toJson() {
|
|
140
|
+
const output = super.toJson();
|
|
141
|
+
if (this.time !== null) {
|
|
142
|
+
output.time = this.time;
|
|
143
|
+
}
|
|
144
|
+
if (this.accuracy !== null) {
|
|
145
|
+
output.accuracy = this.accuracy;
|
|
146
|
+
}
|
|
147
|
+
if (this.bearing !== null) {
|
|
148
|
+
output.bearing = this.bearing;
|
|
149
|
+
}
|
|
150
|
+
if (this.provider !== null) {
|
|
151
|
+
output.provider = this.provider;
|
|
152
|
+
}
|
|
153
|
+
return output;
|
|
44
154
|
}
|
|
45
155
|
|
|
46
|
-
static
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
156
|
+
static fromJson(json) {
|
|
157
|
+
const position = WGS84UserPosition.fromWGS84(WGS84.fromJson(json));
|
|
158
|
+
position.time = json.time;
|
|
159
|
+
position.accuracy = json.accuracy;
|
|
160
|
+
position.bearing = json.bearing;
|
|
161
|
+
position.provider = json.provider;
|
|
162
|
+
return position;
|
|
50
163
|
}
|
|
51
164
|
}
|
|
52
165
|
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/* eslint-disable max-statements */
|
|
2
|
+
import chai from 'chai';
|
|
3
|
+
import chaiAlmost from 'chai-almost';
|
|
4
|
+
|
|
5
|
+
import WGS84UserPosition from './WGS84UserPosition';
|
|
6
|
+
import Level from './Level';
|
|
7
|
+
import WGS84 from './WGS84';
|
|
8
|
+
|
|
9
|
+
const expect = chai.expect;
|
|
10
|
+
chai.use(chaiAlmost());
|
|
11
|
+
|
|
12
|
+
describe('WGS84UserPosition', () => {
|
|
13
|
+
|
|
14
|
+
it('creation', () => {
|
|
15
|
+
|
|
16
|
+
expect(() => new WGS84UserPosition()).throw(Error);
|
|
17
|
+
|
|
18
|
+
expect(() => new WGS84UserPosition(45)).throw(Error);
|
|
19
|
+
expect(() => new WGS84UserPosition(45, 5)).not.throw(Error);
|
|
20
|
+
expect(() => new WGS84UserPosition(45, 5, null)).not.throw(Error);
|
|
21
|
+
expect(() => new WGS84UserPosition(45, 5, null, null)).not.throw(Error);
|
|
22
|
+
|
|
23
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null)).not.throw(Error);
|
|
24
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, 0)).not.throw(Error);
|
|
25
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, 10)).not.throw(Error);
|
|
26
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, -10)).throw(Error);
|
|
27
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, Number.POSITIVE_INFINITY)).throw(Error);
|
|
28
|
+
|
|
29
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null, null)).not.throw(Error);
|
|
30
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null, 0)).not.throw(Error);
|
|
31
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null, 10)).not.throw(Error);
|
|
32
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null, -10)).throw(Error);
|
|
33
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null, Number.POSITIVE_INFINITY)).throw(Error);
|
|
34
|
+
|
|
35
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null,
|
|
36
|
+
null, null)).not.throw(Error);
|
|
37
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null,
|
|
38
|
+
null, 0)).not.throw(Error);
|
|
39
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null,
|
|
40
|
+
null, 10)).not.throw(Error);
|
|
41
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null,
|
|
42
|
+
null, -10)).not.throw(Error);
|
|
43
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null,
|
|
44
|
+
null, Number.POSITIVE_INFINITY)).throw(Error);
|
|
45
|
+
|
|
46
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null,
|
|
47
|
+
null, null, null)).not.throw(Error);
|
|
48
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null,
|
|
49
|
+
null, null, 0)).throw(Error);
|
|
50
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null,
|
|
51
|
+
null, null, 10)).throw(Error);
|
|
52
|
+
expect(() => new WGS84UserPosition(45, 5, null, null, null,
|
|
53
|
+
null, null, 'foo')).not.throw(Error);
|
|
54
|
+
|
|
55
|
+
const position = new WGS84UserPosition(45, 5, 0, new Level(2), 123456, 10, Math.PI, 'foo');
|
|
56
|
+
expect(position.lat).equals(45);
|
|
57
|
+
expect(position.lng).equals(5);
|
|
58
|
+
expect(position.alt).equals(0);
|
|
59
|
+
expect(Level.equalsTo(position.level, new Level(2))).true;
|
|
60
|
+
expect(position.time).equals(123456);
|
|
61
|
+
expect(position.accuracy).equals(10);
|
|
62
|
+
expect(position.bearing).equals(Math.PI);
|
|
63
|
+
expect(position.provider).equals('foo');
|
|
64
|
+
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('update', () => {
|
|
68
|
+
const position = new WGS84UserPosition(0, 0);
|
|
69
|
+
position.lat = 45;
|
|
70
|
+
position.lng = 5;
|
|
71
|
+
position.alt = 0;
|
|
72
|
+
position.level = new Level(2);
|
|
73
|
+
position.time = 123456;
|
|
74
|
+
position.accuracy = 10;
|
|
75
|
+
position.bearing = Math.PI;
|
|
76
|
+
position.provider = 'foo';
|
|
77
|
+
expect(position.lat).equals(45);
|
|
78
|
+
expect(position.lng).equals(5);
|
|
79
|
+
expect(position.alt).equals(0);
|
|
80
|
+
expect(Level.equalsTo(position.level, new Level(2))).true;
|
|
81
|
+
expect(position.time).equals(123456);
|
|
82
|
+
expect(position.accuracy).equals(10);
|
|
83
|
+
expect(position.bearing).equals(Math.PI);
|
|
84
|
+
expect(position.provider).equals('foo');
|
|
85
|
+
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('fromWGS84', () => {
|
|
89
|
+
const level = new Level(2);
|
|
90
|
+
const wgs84 = new WGS84(45, 5, 0, level, 123456);
|
|
91
|
+
const position = WGS84UserPosition.fromWGS84(wgs84);
|
|
92
|
+
expect(position).instanceOf(WGS84UserPosition);
|
|
93
|
+
expect(position.lat).equals(45);
|
|
94
|
+
expect(position.lng).equals(5);
|
|
95
|
+
expect(position.alt).equals(0);
|
|
96
|
+
expect(Level.equalsTo(position.level, level)).true;
|
|
97
|
+
expect(position.time).is.null;
|
|
98
|
+
expect(position.accuracy).is.null;
|
|
99
|
+
expect(position.bearing).is.null;
|
|
100
|
+
expect(position.provider).is.null;
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
it('clone', () => {
|
|
105
|
+
const level = new Level(2);
|
|
106
|
+
const position = new WGS84UserPosition(45, 5, 0, level, 123456, 10, Math.PI, 'foo');
|
|
107
|
+
const clonePosition = position.clone();
|
|
108
|
+
|
|
109
|
+
expect(clonePosition.lat).equals(45);
|
|
110
|
+
expect(clonePosition.lng).equals(5);
|
|
111
|
+
expect(clonePosition.alt).equals(0);
|
|
112
|
+
expect(Level.equalsTo(clonePosition.level, level)).true;
|
|
113
|
+
expect(clonePosition.level).not.equals(level);
|
|
114
|
+
expect(clonePosition.time).equals(123456);
|
|
115
|
+
expect(clonePosition.accuracy).equals(10);
|
|
116
|
+
expect(clonePosition.bearing).equals(Math.PI);
|
|
117
|
+
expect(clonePosition.provider).equals('foo');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
it('equalsTo', () => {
|
|
122
|
+
let position, position2;
|
|
123
|
+
|
|
124
|
+
expect(WGS84UserPosition.equalsTo(null, null)).true;
|
|
125
|
+
|
|
126
|
+
position = new WGS84UserPosition(0, 0);
|
|
127
|
+
expect(WGS84UserPosition.equalsTo(position, new WGS84UserPosition(0, 0))).true;
|
|
128
|
+
expect(WGS84UserPosition.equalsTo(position, new WGS84(0, 0))).false;
|
|
129
|
+
|
|
130
|
+
position = new WGS84UserPosition(45, 5, 0, new Level(2), 123456, 10, Math.PI, 'foo');
|
|
131
|
+
expect(WGS84UserPosition.equalsTo(position,
|
|
132
|
+
new WGS84UserPosition(45, 5, 0, new Level(2), 123456, 10, Math.PI, 'foo')
|
|
133
|
+
)).true;
|
|
134
|
+
|
|
135
|
+
position2 = position.clone();
|
|
136
|
+
position2.lat = 0;
|
|
137
|
+
expect(WGS84UserPosition.equalsTo(position, position2)).false;
|
|
138
|
+
|
|
139
|
+
position2 = position.clone();
|
|
140
|
+
position2.lng = 0;
|
|
141
|
+
expect(WGS84UserPosition.equalsTo(position, position2)).false;
|
|
142
|
+
|
|
143
|
+
position2 = position.clone();
|
|
144
|
+
position2.alt = 10;
|
|
145
|
+
expect(WGS84UserPosition.equalsTo(position, position2)).false;
|
|
146
|
+
|
|
147
|
+
position2 = position.clone();
|
|
148
|
+
position2.level = null;
|
|
149
|
+
expect(WGS84UserPosition.equalsTo(position, position2)).false;
|
|
150
|
+
|
|
151
|
+
position2 = position.clone();
|
|
152
|
+
position2.time = 0;
|
|
153
|
+
expect(WGS84UserPosition.equalsTo(position, position2)).false;
|
|
154
|
+
|
|
155
|
+
position2 = position.clone();
|
|
156
|
+
position2.accuracy = 0;
|
|
157
|
+
expect(WGS84UserPosition.equalsTo(position, position2)).false;
|
|
158
|
+
|
|
159
|
+
position2 = position.clone();
|
|
160
|
+
position2.bearing = 0;
|
|
161
|
+
expect(WGS84UserPosition.equalsTo(position, position2)).false;
|
|
162
|
+
|
|
163
|
+
position2 = position.clone();
|
|
164
|
+
position2.provider = 'foobar';
|
|
165
|
+
expect(WGS84UserPosition.equalsTo(position, position2)).false;
|
|
166
|
+
|
|
167
|
+
expect(position.equalsTo(
|
|
168
|
+
new WGS84UserPosition(45, 5, 0, new Level(2), 123456, 10, Math.PI, 'foo')
|
|
169
|
+
)).true;
|
|
170
|
+
expect(position.equalsTo(
|
|
171
|
+
new WGS84UserPosition(45, 5, 0, new Level(2), 123456, 0, Math.PI, 'foo')
|
|
172
|
+
)).false;
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('json', () => {
|
|
176
|
+
let position;
|
|
177
|
+
|
|
178
|
+
position = new WGS84UserPosition(5, -10);
|
|
179
|
+
expect(position.toJson()).to.deep.equal({
|
|
180
|
+
lat: 5,
|
|
181
|
+
lng: -10
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
position = new WGS84UserPosition(5, -10);
|
|
185
|
+
position.alt = 2;
|
|
186
|
+
expect(position.toJson()).to.deep.equal({
|
|
187
|
+
lat: 5,
|
|
188
|
+
lng: -10,
|
|
189
|
+
alt: 2
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
position = new WGS84UserPosition(5, -10);
|
|
193
|
+
position.level = new Level(2);
|
|
194
|
+
expect(position.toJson()).to.deep.equal({
|
|
195
|
+
lat: 5,
|
|
196
|
+
lng: -10,
|
|
197
|
+
level: '2'
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
position = new WGS84UserPosition(5, -10);
|
|
201
|
+
position.time = 10;
|
|
202
|
+
expect(position.toJson()).to.deep.equal({
|
|
203
|
+
lat: 5,
|
|
204
|
+
lng: -10,
|
|
205
|
+
time: 10
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
position = new WGS84UserPosition(5, -10);
|
|
209
|
+
position.accuracy = 10;
|
|
210
|
+
expect(position.toJson()).to.deep.equal({
|
|
211
|
+
lat: 5,
|
|
212
|
+
lng: -10,
|
|
213
|
+
accuracy: 10
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
position = new WGS84UserPosition(5, -10);
|
|
217
|
+
position.bearing = Math.PI;
|
|
218
|
+
expect(position.toJson()).to.deep.equal({
|
|
219
|
+
lat: 5,
|
|
220
|
+
lng: -10,
|
|
221
|
+
bearing: Math.PI
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
position = new WGS84UserPosition(5, -10);
|
|
225
|
+
position.provider = 'foo';
|
|
226
|
+
expect(position.toJson()).to.deep.equal({
|
|
227
|
+
lat: 5,
|
|
228
|
+
lng: -10,
|
|
229
|
+
provider: 'foo'
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
position = new WGS84UserPosition(5, -10);
|
|
234
|
+
expect(WGS84UserPosition.equalsTo(
|
|
235
|
+
WGS84UserPosition.fromJson(position.toJson()),
|
|
236
|
+
position)
|
|
237
|
+
).true;
|
|
238
|
+
|
|
239
|
+
position = new WGS84UserPosition(45, 5, 0, new Level(2), 123456, 10, Math.PI, 'foo');
|
|
240
|
+
expect(WGS84UserPosition.equalsTo(
|
|
241
|
+
WGS84UserPosition.fromJson(position.toJson()),
|
|
242
|
+
position)
|
|
243
|
+
).true;
|
|
244
|
+
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
});
|