@wemap/geo 8.1.0-alpha.0 → 9.0.0-alpha.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.d.ts CHANGED
@@ -1,27 +1,78 @@
1
+ import { Quaternion_t, Vector3_t } from '@wemap/maths';
1
2
  declare module '@wemap/geo' {
2
3
 
4
+ export type Level_t = null | number | [number, number];
5
+
3
6
  export class Level {
4
- val: number | null;
5
- isRange: boolean;
6
- low: number | null;
7
- up: number | null;
8
- constructor(arg1: number, arg2?: number);
9
- static fromString(str: string): Level;
7
+ static checkType(level: Level_t): void;
8
+ static isRange(level: Level_t): boolean;
9
+ static clone(level: Level_t): Level_t;
10
+ static fromString(str: string): Level_t;
11
+ static contains(container: Level_t, targeted: Level_t): boolean;
12
+ static intersect(first: Level_t, second: Level_t): Level_t;
13
+ static union(first: Level_t, second: Level_t): Level_t;
14
+ static multiplyBy(level: Level_t, factor: number): Level_t;
15
+ static toString(level: Level_t): string;
16
+ static equalsTo(first: Level_t, second: Level_t): boolean;
17
+ static diff(first: Level_t, second: Level_t): null | number;
18
+
19
+ /** @deprecated */
20
+ static fromLegacy(level: (null | { isRange: boolean, val?: number, low?: number, up?: number })): Level_t;
10
21
  }
11
22
 
23
+ type CoordinatesCompressedJson = [number, number] |
24
+ [number, number, number] |
25
+ [number, number, number, number] |
26
+ [number, number, null, number] |
27
+ [number, number, number, [number, number]];
28
+
12
29
  export class Coordinates {
13
30
  lat: number;
14
31
  lng: number;
15
32
  alt: number | null;
16
- level: Level | null;
33
+ level: Level_t | null;
17
34
 
18
- constructor(lat: number, lng: number, alt?: number | null, level?: Level | null);
35
+ constructor(lat: number, lng: number, alt?: number | null, level?: Level_t | null);
19
36
 
37
+ clone(): Coordinates;
38
+ wrap(): void;
39
+ static equalsTo(pos1: Coordinates, pos2: Coordinates, eps?: number, epsAlt?: number): boolean;
40
+ equalsTo(other: Coordinates): boolean;
41
+
42
+ destinationPoint(distance: number, bearing: number, elevation?: number): Coordinates;
43
+ move(distance: number, bearing: number, elevation?: number): void;
20
44
  distanceTo(other: Coordinates): number;
45
+ static distanceBetween(point1: Coordinates, point2: Coordinates): number;
21
46
  bearingTo(other: Coordinates): number;
22
- static fromJson(json: any): Coordinates;
23
- toJson(): any;
47
+
48
+ enuToEcefRotation: Quaternion_t;
49
+ ecefToEnuRotation: Quaternion_t;
50
+ ecef: Vector3_t;
51
+ static fromEcef(ecef: Vector3_t): Coordinates;
52
+
53
+ getSegmentProjection(p1: Coordinates, p2: Coordinates): Coordinates | null;
54
+
24
55
  toString(): string;
56
+ toJson(): { lat: number, lng: number, alt?: number, level?: number | [number, number] };
57
+ toCompressedJson(): CoordinatesCompressedJson;
58
+ static fromJson(json: { lat: number, lng: number, alt?: number, level?: number | [number, number] }): Coordinates;
59
+ static fromCompressedJson(json: CoordinatesCompressedJson): Coordinates;
60
+
61
+ /** @deprecated */
62
+ toLegacyJson(): any;
63
+ /** @deprecated */
64
+ toLegacyCompressedJson():
65
+ [number, number] |
66
+ [number, number, number] |
67
+ [number, number, number, string] |
68
+ [number, number, null, string];
69
+ /** @deprecated */
70
+ static fromLegacyCompressedJson(json:
71
+ [number, number] |
72
+ [number, number, number] |
73
+ [number, number, number, string] |
74
+ [number, number, null, string]): Coordinates;
75
+
25
76
  }
26
77
 
27
78
  export class GeoRef {
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "directory": "packages/geo"
14
14
  },
15
15
  "name": "@wemap/geo",
16
- "version": "8.1.0-alpha.0",
16
+ "version": "9.0.0-alpha.0",
17
17
  "bugs": {
18
18
  "url": "https://github.com/wemap/wemap-modules-js/issues"
19
19
  },
@@ -29,7 +29,7 @@
29
29
  "license": "ISC",
30
30
  "dependencies": {
31
31
  "@wemap/logger": "^8.0.1",
32
- "@wemap/maths": "^8.1.0-alpha.0"
32
+ "@wemap/maths": "^8.1.0"
33
33
  },
34
- "gitHead": "9e87dcdc938ef2e2469bc18b3e88a25eb5b16c53"
34
+ "gitHead": "fc17cf617195c987813ede500bf67dd0f135cf34"
35
35
  }
@@ -1,3 +1,5 @@
1
+ import Logger from '@wemap/logger';
2
+
1
3
  import {
2
4
  deg2rad, Vector3, Quaternion, rad2deg, wrap
3
5
  } from '@wemap/maths';
@@ -27,7 +29,7 @@ class Coordinates {
27
29
  /** @type {Number|null} */
28
30
  _alt = null;
29
31
 
30
- /** @type {Level|null} */
32
+ /** @type {null|number|[number, number]} */
31
33
  _level = null;
32
34
 
33
35
  /** @type {[Number, Number, Number]|null} */
@@ -39,7 +41,7 @@ class Coordinates {
39
41
  * @param {Number} lat
40
42
  * @param {Number} lng
41
43
  * @param {?(Number|null)} alt
42
- * @param {?(Level|null)} level
44
+ * @param {?(null|number|[number, number])} level
43
45
  */
44
46
  constructor(lat, lng, alt = null, level = null) {
45
47
  this.lat = lat;
@@ -78,7 +80,7 @@ class Coordinates {
78
80
  return this._alt;
79
81
  }
80
82
 
81
- /** @type {Level|null} */
83
+ /** @type {null|number|[number, number]} */
82
84
  get level() {
83
85
  return this._level;
84
86
  }
@@ -129,16 +131,10 @@ class Coordinates {
129
131
  this._ecef = null;
130
132
  }
131
133
 
132
- /** @type {Level|null} */
134
+ /** @type {null|number|[number, number]} */
133
135
  set level(level) {
134
- if (level instanceof Level) {
135
- this._level = level;
136
- } else {
137
- if (typeof level !== 'undefined' && level !== null) {
138
- throw new Error('level argument is not a Level object');
139
- }
140
- this._level = null;
141
- }
136
+ Level.checkType(level);
137
+ this._level = level;
142
138
  }
143
139
 
144
140
  /**
@@ -147,8 +143,8 @@ class Coordinates {
147
143
  */
148
144
  clone() {
149
145
  const output = new Coordinates(this.lat, this.lng, this.alt);
150
- if (this.level) {
151
- output.level = this.level.clone();
146
+ if (this.level !== null) {
147
+ output.level = Level.clone(this.level);
152
148
  }
153
149
  return output;
154
150
  }
@@ -429,7 +425,7 @@ class Coordinates {
429
425
  str += ', ' + this._alt.toFixed(2);
430
426
  }
431
427
  if (this._level !== null) {
432
- str += ', [' + this._level.toString() + ']';
428
+ str += ', [' + Level.toString(this._level) + ']';
433
429
  }
434
430
  str += ']';
435
431
  return str;
@@ -447,7 +443,24 @@ class Coordinates {
447
443
  output.alt = this.alt;
448
444
  }
449
445
  if (this.level !== null) {
450
- output.level = this.level.toString();
446
+ output.level = this.level;
447
+ }
448
+ return output;
449
+ }
450
+
451
+ /**
452
+ * @returns {!Object}
453
+ */
454
+ toLegacyJson() {
455
+ const output = {
456
+ lat: this.lat,
457
+ lng: this.lng
458
+ };
459
+ if (this.alt !== null) {
460
+ output.alt = this.alt;
461
+ }
462
+ if (this.level !== null) {
463
+ output.level = Level.toString(this.level);
451
464
  }
452
465
  return output;
453
466
  }
@@ -457,7 +470,11 @@ class Coordinates {
457
470
  * @returns {!Coordinates}
458
471
  */
459
472
  static fromJson(json) {
460
- return new Coordinates(json.lat, json.lng, json.alt, Level.fromString(json.level));
473
+ if (typeof json.level === 'string') {
474
+ Logger.warn('Still using legacy level format. Please update your project.');
475
+ return new Coordinates(json.lat, json.lng, json.alt, Level.fromString(json.level));
476
+ }
477
+ return new Coordinates(json.lat, json.lng, json.alt, json.level);
461
478
  }
462
479
 
463
480
  /**
@@ -469,7 +486,22 @@ class Coordinates {
469
486
  output.push(this.alt);
470
487
  }
471
488
  if (this.level !== null) {
472
- output.push(this.level.toString());
489
+ output.push(this.level);
490
+ }
491
+ return output;
492
+ }
493
+
494
+ /**
495
+ * @returns {!Object}
496
+ * @deprecated
497
+ */
498
+ toLegacyCompressedJson() {
499
+ const output = [this.lat, this.lng];
500
+ if (this.alt !== null || this.level !== null) {
501
+ output.push(this.alt);
502
+ }
503
+ if (this.level !== null) {
504
+ output.push(Level.toString(this.level));
473
505
  }
474
506
  return output;
475
507
  }
@@ -479,6 +511,21 @@ class Coordinates {
479
511
  * @returns {!Coordinates}
480
512
  */
481
513
  static fromCompressedJson(json) {
514
+ const coords = new Coordinates(json[0], json[1]);
515
+ if (json.length > 2) {
516
+ coords.alt = json[2];
517
+ }
518
+ if (json.length > 3) {
519
+ coords.level = json[3];
520
+ }
521
+ return coords;
522
+ }
523
+
524
+ /**
525
+ * @param {!Object} json
526
+ * @returns {!Coordinates}
527
+ */
528
+ static fromLegacyCompressedJson(json) {
482
529
  const coords = new Coordinates(json[0], json[1]);
483
530
  if (json.length > 2) {
484
531
  coords.alt = json[2];
@@ -2,7 +2,6 @@ import chai from 'chai';
2
2
  import chaiAlmost from 'chai-almost';
3
3
 
4
4
  import Coordinates from './Coordinates.js';
5
- import Level from './Level.js';
6
5
  import Constants from '../Constants.js';
7
6
 
8
7
  const expect = chai.expect;
@@ -26,18 +25,17 @@ describe('Coordinates', () => {
26
25
 
27
26
  expect(() => new Coordinates(45, 5, null, null)).not.throw(Error);
28
27
  expect(() => new Coordinates(45, 5, null, true)).throw(Error);
29
- expect(() => new Coordinates(45, 5, null, new Level(1))).not.throw(Error);
30
- expect(() => new Coordinates(45, 5, null, new Level(1, 2))).not.throw(Error);
31
- expect(() => new Coordinates(45, 5, 0, new Level(1))).not.throw(Error);
28
+ expect(() => new Coordinates(45, 5, null, 1)).not.throw(Error);
29
+ expect(() => new Coordinates(45, 5, null, [1, 2])).not.throw(Error);
30
+ expect(() => new Coordinates(45, 5, 0, 1)).not.throw(Error);
32
31
 
33
- const level = new Level(2);
34
- const position = new Coordinates(45, 5, 0, level);
32
+ const position = new Coordinates(45, 5, 0, 2);
35
33
  expect(position.lat).equals(45);
36
34
  expect(position.latitude).equals(45);
37
35
  expect(position.lng).equals(5);
38
36
  expect(position.longitude).equals(5);
39
37
  expect(position.alt).equals(0);
40
- expect(position.level).equals(level);
38
+ expect(position.level).equals(2);
41
39
 
42
40
  });
43
41
 
@@ -46,11 +44,11 @@ describe('Coordinates', () => {
46
44
  position.lat = 45;
47
45
  position.lng = 5;
48
46
  position.alt = 0;
49
- position.level = new Level(2);
47
+ position.level = 2;
50
48
  expect(position.lat).equals(45);
51
49
  expect(position.lng).equals(5);
52
50
  expect(position.alt).equals(0);
53
- expect(Level.equalsTo(position.level, new Level(2))).true;
51
+ expect(position.level).equals(2);
54
52
  expect(position._ecef).equals(null);
55
53
  expect(position.ecef).is.not.null;
56
54
 
@@ -65,14 +63,14 @@ describe('Coordinates', () => {
65
63
  });
66
64
 
67
65
  it('clone', () => {
68
- const level = new Level(0);
66
+ const level = [1, 2];
69
67
  const position = new Coordinates(45, 5, 0, level);
70
68
  const clonePosition = position.clone();
71
69
 
72
70
  expect(clonePosition.lat).equals(45);
73
71
  expect(clonePosition.lng).equals(5);
74
72
  expect(clonePosition.alt).equals(0);
75
- expect(Level.equalsTo(clonePosition.level, level)).true;
73
+ expect(clonePosition.level).deep.equals(level);
76
74
  expect(clonePosition.level).not.equals(level);
77
75
  });
78
76
 
@@ -82,14 +80,14 @@ describe('Coordinates', () => {
82
80
  position = new Coordinates(0, 0);
83
81
  expect(Coordinates.equalsTo(position, new Coordinates(0, 0))).true;
84
82
  expect(Coordinates.equalsTo(position, new Coordinates(0, 0, 0))).false;
85
- expect(Coordinates.equalsTo(position, new Coordinates(0, 0, null, new Level(0)))).false;
83
+ expect(Coordinates.equalsTo(position, new Coordinates(0, 0, null, 0))).false;
86
84
  expect(Coordinates.equalsTo(position, new Coordinates(0, 0, null, null))).true;
87
85
 
88
86
 
89
- position = new Coordinates(45, 5, 0, new Level(0));
90
- expect(Coordinates.equalsTo(position, new Coordinates(45, 5, 0, new Level(0)))).true;
91
- expect(Coordinates.equalsTo(position, new Coordinates(45, 5, 1, new Level(0)))).false;
92
- expect(Coordinates.equalsTo(position, new Coordinates(45, 5, 0, new Level(1)))).false;
87
+ position = new Coordinates(45, 5, 0, 0);
88
+ expect(Coordinates.equalsTo(position, new Coordinates(45, 5, 0, 0))).true;
89
+ expect(Coordinates.equalsTo(position, new Coordinates(45, 5, 1, 0))).false;
90
+ expect(Coordinates.equalsTo(position, new Coordinates(45, 5, 0, 1))).false;
93
91
  expect(Coordinates.equalsTo(position, {
94
92
  lat: 45,
95
93
  lng: 5
@@ -102,21 +100,21 @@ describe('Coordinates', () => {
102
100
  expect(Coordinates.equalsTo(null, position)).false;
103
101
  expect(Coordinates.equalsTo(null, null)).true;
104
102
 
105
- expect(position.equalsTo(new Coordinates(45, 5, 0, new Level(0)))).true;
106
- expect(position.equalsTo(new Coordinates(45, 5, 0, new Level(1)))).false;
103
+ expect(position.equalsTo(new Coordinates(45, 5, 0, 0))).true;
104
+ expect(position.equalsTo(new Coordinates(45, 5, 0, 1))).false;
107
105
  });
108
106
 
109
107
  it('toString', () => {
110
108
  expect(new Coordinates(45, 5).toString()).equals('[45.0000000, 5.0000000]');
111
109
  expect(new Coordinates(45, 5, 0).toString()).equals('[45.0000000, 5.0000000, 0.00]');
112
110
  expect(new Coordinates(45, 5, 1).toString()).equals('[45.0000000, 5.0000000, 1.00]');
113
- expect(new Coordinates(45, 5, null, new Level(0)).toString())
111
+ expect(new Coordinates(45, 5, null, 0).toString())
114
112
  .equals('[45.0000000, 5.0000000, [0]]');
115
- expect(new Coordinates(45, 5, null, new Level(1)).toString())
113
+ expect(new Coordinates(45, 5, null, 1).toString())
116
114
  .equals('[45.0000000, 5.0000000, [1]]');
117
- expect(new Coordinates(45, 5, 2, new Level(1)).toString())
115
+ expect(new Coordinates(45, 5, 2, 1).toString())
118
116
  .equals('[45.0000000, 5.0000000, 2.00, [1]]');
119
- expect(new Coordinates(45, 5, 2, new Level(1, 2)).toString())
117
+ expect(new Coordinates(45, 5, 2, [1, 2]).toString())
120
118
  .equals('[45.0000000, 5.0000000, 2.00, [1;2]]');
121
119
  });
122
120
 
@@ -124,18 +122,18 @@ describe('Coordinates', () => {
124
122
  it('move/destination', () => {
125
123
  let position;
126
124
 
127
- position = new Coordinates(0, 0, 0, new Level(0)).move(100000, 0);
128
- expect(Coordinates.equalsTo(position, new Coordinates(0.8983152841195214, 0, 0, new Level(0)))).true;
125
+ position = new Coordinates(0, 0, 0, 0).move(100000, 0);
126
+ expect(Coordinates.equalsTo(position, new Coordinates(0.8983152841195214, 0, 0, 0))).true;
129
127
 
130
- position = new Coordinates(0, 0, 0, new Level(0)).move(100000, Math.PI / 2);
131
- expect(Coordinates.equalsTo(position, new Coordinates(0, 0.8983152841195212, 0, new Level(0)))).true;
128
+ position = new Coordinates(0, 0, 0, 0).move(100000, Math.PI / 2);
129
+ expect(Coordinates.equalsTo(position, new Coordinates(0, 0.8983152841195212, 0, 0))).true;
132
130
 
133
131
  position = new Coordinates(0, 0, 0).move(100000, 0, 10);
134
132
  expect(Coordinates.equalsTo(position, new Coordinates(0.8983152841195214, 0, 10))).true;
135
133
 
136
134
  expect(() => new Coordinates(0, 0).move(100000, 0, 10)).throw(Error);
137
135
 
138
- const level = new Level(1);
136
+ const level = [1, 2];
139
137
  position = new Coordinates(0, 0, null, level);
140
138
  const newPosition = position.destinationPoint(100000, 0);
141
139
  expect(newPosition).not.equals(position);
@@ -246,13 +244,26 @@ describe('Coordinates', () => {
246
244
  alt: 2
247
245
  });
248
246
 
249
- expect(new Coordinates(5, -10, null, new Level(1, 2)).toJson()).to.deep.equal({
247
+ expect(new Coordinates(5, -10, null, [1, 2]).toJson()).to.deep.equal({
248
+ lat: 5,
249
+ lng: -10,
250
+ level: [1, 2]
251
+ });
252
+
253
+ expect(new Coordinates(5, -10, null, [1, 2]).toLegacyJson()).to.deep.equal({
250
254
  lat: 5,
251
255
  lng: -10,
252
256
  level: '1;2'
253
257
  });
254
258
 
255
- expect(new Coordinates(5, -10, 3, new Level(1, 2)).toJson()).to.deep.equal({
259
+ expect(new Coordinates(5, -10, 3, [1, 2]).toJson()).to.deep.equal({
260
+ lat: 5,
261
+ lng: -10,
262
+ alt: 3,
263
+ level: [1, 2]
264
+ });
265
+
266
+ expect(new Coordinates(5, -10, 3, [1, 2]).toLegacyJson()).to.deep.equal({
256
267
  lat: 5,
257
268
  lng: -10,
258
269
  alt: 3,
@@ -267,10 +278,10 @@ describe('Coordinates', () => {
267
278
  position = new Coordinates(5, -10, 2);
268
279
  expect(Coordinates.equalsTo(Coordinates.fromJson(position.toJson()), position)).true;
269
280
 
270
- position = new Coordinates(5, -10, null, new Level(1, 2));
281
+ position = new Coordinates(5, -10, null, [1, 2]);
271
282
  expect(Coordinates.equalsTo(Coordinates.fromJson(position.toJson()), position)).true;
272
283
 
273
- position = new Coordinates(5, -10, 3, new Level(1, 2));
284
+ position = new Coordinates(5, -10, 3, [1, 2]);
274
285
  expect(Coordinates.equalsTo(Coordinates.fromJson(position.toJson()), position)).true;
275
286
 
276
287
  });