@saber-usa/node-common 1.7.7 → 1.7.9-alpha.1

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/src/LLA.js CHANGED
@@ -1,179 +1,179 @@
1
- /**
2
- * Latitude, longitude, and altitude which define a ground point.
3
- *
4
- * Latitude must be between ±90°, longitude must be between ±180°.
5
- * Latitude is typically denoted as 90° N to 90° S (±90°).
6
- * Longitude is typically denoted as 180° E to 180° W (±180°).
7
- */
8
-
9
- class LLA {
10
- /**
11
- * Create an LLA object from degrees
12
- * @param {number} latitudeDeg - Latitude in degrees (±90°)
13
- * @param {number} longitudeDeg - Longitude in degrees (±180°)
14
- * @param {number} altitudeKm - Altitude in kilometers (default: 0)
15
- */
16
- constructor(latitudeDeg, longitudeDeg, altitudeKm = 0) {
17
- // If first parameter is an Angle object, use alternate constructor pattern
18
- if (
19
- latitudeDeg instanceof Angle || (latitudeDeg
20
- && typeof latitudeDeg === "object"
21
- && Object.prototype.hasOwnProperty.call(latitudeDeg, "Degrees"))
22
- ) {
23
- this._initFromAngles(latitudeDeg, longitudeDeg, altitudeKm);
24
- } else {
25
- this._initFromDegrees(latitudeDeg, longitudeDeg, altitudeKm);
26
- }
27
- }
28
-
29
- /**
30
- * Create from degree values
31
- * @private
32
- * @param {number} latitudeDeg - Latitude in degrees
33
- * @param {number} longitudeDeg - Longitude in degrees
34
- * @param {number} altitudeKm - Altitude in kilometers
35
- */
36
- _initFromDegrees(latitudeDeg, longitudeDeg, altitudeKm) {
37
- this.Latitude = new Angle(latitudeDeg);
38
- this.Longitude = new Angle(longitudeDeg);
39
- this.AltitudeKm = altitudeKm;
40
-
41
- // Apply wrapping constraints
42
- this._applyConstraints();
43
- }
44
-
45
- /**
46
- * Create from Angle objects
47
- * @private
48
- * @param {Angle} latitude - Latitude Angle object
49
- * @param {Angle} longitude - Longitude Angle object
50
- * @param {number} altitudeKm - Altitude in kilometers
51
- */
52
- _initFromAngles(latitude, longitude, altitudeKm) {
53
- this.Latitude = latitude;
54
- this.Longitude = longitude;
55
- this.AltitudeKm = altitudeKm;
56
-
57
- // Apply wrapping constraints
58
- this._applyConstraints();
59
- }
60
-
61
- /**
62
- * Apply latitude/longitude constraints
63
- * @private
64
- */
65
- _applyConstraints() {
66
- // Wrap latitude to ±90° (quarter revolution)
67
- if (this.Latitude.Degrees > 90) {
68
- this.Latitude = new Angle(180 - this.Latitude.Degrees);
69
- } else if (this.Latitude.Degrees < -90) {
70
- this.Latitude = new Angle(-180 - this.Latitude.Degrees);
71
- }
72
-
73
- // Wrap longitude to ±180° (half revolution)
74
- let lon = this.Longitude.Degrees;
75
- while (lon > 180) lon -= 360;
76
- while (lon < -180) lon += 360;
77
- this.Longitude = new Angle(lon);
78
- }
79
-
80
- /**
81
- * φ : Geodetic latitude, between ±90°.
82
- * @return {Angle} Latitude angle
83
- */
84
- get latitude() {
85
- return this.Latitude;
86
- }
87
-
88
- /**
89
- * λ : Geodetic longitude, between ±180°.
90
- * @return {Angle} Longitude angle
91
- */
92
- get longitude() {
93
- return this.Longitude;
94
- }
95
-
96
- /**
97
- * Altitude in kilometers.
98
- * @return {number} Altitude in km
99
- */
100
- get altitudeKm() {
101
- return this.AltitudeKm;
102
- }
103
-
104
-
105
- /**
106
- * Create LLA from JSON object
107
- * @param {Object} json - JSON object with latitude, longitude, altitude_km properties
108
- * @return {LLA} New LLA instance
109
- */
110
- static fromJSON(json) {
111
- return new LLA(json.latitude, json.longitude, json.altitude_km || 0);
112
- }
113
-
114
- /**
115
- * Convert to JSON object
116
- * @return {Object} JSON representation
117
- */
118
- toJSON() {
119
- return {
120
- latitude: this.Latitude.Degrees,
121
- longitude: this.Longitude.Degrees,
122
- altitude_km: this.AltitudeKm,
123
- };
124
- }
125
-
126
- /**
127
- * String representation
128
- * @return {string} String representation of LLA
129
- */
130
- toString() {
131
- return `LLA(${this.Latitude.Degrees.toFixed(6)}°, ${this.Longitude.Degrees.toFixed(6)}°, ${this.AltitudeKm.toFixed(3)} km)`;
132
- }
133
-
134
- /**
135
- * Create a copy of this LLA
136
- * @return {LLA} Copy of this LLA
137
- */
138
- clone() {
139
- return new LLA(this.Latitude.Degrees, this.Longitude.Degrees, this.AltitudeKm);
140
- }
141
-
142
- /**
143
- * Check equality with another LLA (within tolerance)
144
- * @param {LLA} other - Other LLA to compare
145
- * @param {number} tolerance - Tolerance for comparison (default: 1e-9)
146
- * @return {boolean} True if equal within tolerance
147
- */
148
- equals(other, tolerance = 1e-9) {
149
- return Math.abs(this.Latitude.Degrees - other.Latitude.Degrees) < tolerance
150
- && Math.abs(this.Longitude.Degrees - other.Longitude.Degrees) < tolerance
151
- && Math.abs(this.AltitudeKm - other.AltitudeKm) < tolerance;
152
- }
153
- }
154
-
155
- // Simple Angle class (you may already have this)
156
- class Angle {
157
- constructor(degrees) {
158
- this._degrees = degrees;
159
- }
160
-
161
- get Degrees() {
162
- return this._degrees;
163
- }
164
-
165
- get Radians() {
166
- return this._degrees * Math.PI / 180;
167
- }
168
-
169
- static FromDegrees(degrees) {
170
- return new Angle(degrees);
171
- }
172
-
173
- static FromRadians(radians) {
174
- return new Angle(radians * 180 / Math.PI);
175
- }
176
- }
177
-
178
- // Export for use
179
- export {LLA, Angle};
1
+ /**
2
+ * Latitude, longitude, and altitude which define a ground point.
3
+ *
4
+ * Latitude must be between ±90°, longitude must be between ±180°.
5
+ * Latitude is typically denoted as 90° N to 90° S (±90°).
6
+ * Longitude is typically denoted as 180° E to 180° W (±180°).
7
+ */
8
+
9
+ class LLA {
10
+ /**
11
+ * Create an LLA object from degrees
12
+ * @param {number} latitudeDeg - Latitude in degrees (±90°)
13
+ * @param {number} longitudeDeg - Longitude in degrees (±180°)
14
+ * @param {number} altitudeKm - Altitude in kilometers (default: 0)
15
+ */
16
+ constructor(latitudeDeg, longitudeDeg, altitudeKm = 0) {
17
+ // If first parameter is an Angle object, use alternate constructor pattern
18
+ if (
19
+ latitudeDeg instanceof Angle || (latitudeDeg
20
+ && typeof latitudeDeg === "object"
21
+ && Object.prototype.hasOwnProperty.call(latitudeDeg, "Degrees"))
22
+ ) {
23
+ this._initFromAngles(latitudeDeg, longitudeDeg, altitudeKm);
24
+ } else {
25
+ this._initFromDegrees(latitudeDeg, longitudeDeg, altitudeKm);
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Create from degree values
31
+ * @private
32
+ * @param {number} latitudeDeg - Latitude in degrees
33
+ * @param {number} longitudeDeg - Longitude in degrees
34
+ * @param {number} altitudeKm - Altitude in kilometers
35
+ */
36
+ _initFromDegrees(latitudeDeg, longitudeDeg, altitudeKm) {
37
+ this.Latitude = new Angle(latitudeDeg);
38
+ this.Longitude = new Angle(longitudeDeg);
39
+ this.AltitudeKm = altitudeKm;
40
+
41
+ // Apply wrapping constraints
42
+ this._applyConstraints();
43
+ }
44
+
45
+ /**
46
+ * Create from Angle objects
47
+ * @private
48
+ * @param {Angle} latitude - Latitude Angle object
49
+ * @param {Angle} longitude - Longitude Angle object
50
+ * @param {number} altitudeKm - Altitude in kilometers
51
+ */
52
+ _initFromAngles(latitude, longitude, altitudeKm) {
53
+ this.Latitude = latitude;
54
+ this.Longitude = longitude;
55
+ this.AltitudeKm = altitudeKm;
56
+
57
+ // Apply wrapping constraints
58
+ this._applyConstraints();
59
+ }
60
+
61
+ /**
62
+ * Apply latitude/longitude constraints
63
+ * @private
64
+ */
65
+ _applyConstraints() {
66
+ // Wrap latitude to ±90° (quarter revolution)
67
+ if (this.Latitude.Degrees > 90) {
68
+ this.Latitude = new Angle(180 - this.Latitude.Degrees);
69
+ } else if (this.Latitude.Degrees < -90) {
70
+ this.Latitude = new Angle(-180 - this.Latitude.Degrees);
71
+ }
72
+
73
+ // Wrap longitude to ±180° (half revolution)
74
+ let lon = this.Longitude.Degrees;
75
+ while (lon > 180) lon -= 360;
76
+ while (lon < -180) lon += 360;
77
+ this.Longitude = new Angle(lon);
78
+ }
79
+
80
+ /**
81
+ * φ : Geodetic latitude, between ±90°.
82
+ * @return {Angle} Latitude angle
83
+ */
84
+ get latitude() {
85
+ return this.Latitude;
86
+ }
87
+
88
+ /**
89
+ * λ : Geodetic longitude, between ±180°.
90
+ * @return {Angle} Longitude angle
91
+ */
92
+ get longitude() {
93
+ return this.Longitude;
94
+ }
95
+
96
+ /**
97
+ * Altitude in kilometers.
98
+ * @return {number} Altitude in km
99
+ */
100
+ get altitudeKm() {
101
+ return this.AltitudeKm;
102
+ }
103
+
104
+
105
+ /**
106
+ * Create LLA from JSON object
107
+ * @param {Object} json - JSON object with latitude, longitude, altitude_km properties
108
+ * @return {LLA} New LLA instance
109
+ */
110
+ static fromJSON(json) {
111
+ return new LLA(json.latitude, json.longitude, json.altitude_km || 0);
112
+ }
113
+
114
+ /**
115
+ * Convert to JSON object
116
+ * @return {Object} JSON representation
117
+ */
118
+ toJSON() {
119
+ return {
120
+ latitude: this.Latitude.Degrees,
121
+ longitude: this.Longitude.Degrees,
122
+ altitude_km: this.AltitudeKm,
123
+ };
124
+ }
125
+
126
+ /**
127
+ * String representation
128
+ * @return {string} String representation of LLA
129
+ */
130
+ toString() {
131
+ return `LLA(${this.Latitude.Degrees.toFixed(6)}°, ${this.Longitude.Degrees.toFixed(6)}°, ${this.AltitudeKm.toFixed(3)} km)`;
132
+ }
133
+
134
+ /**
135
+ * Create a copy of this LLA
136
+ * @return {LLA} Copy of this LLA
137
+ */
138
+ clone() {
139
+ return new LLA(this.Latitude.Degrees, this.Longitude.Degrees, this.AltitudeKm);
140
+ }
141
+
142
+ /**
143
+ * Check equality with another LLA (within tolerance)
144
+ * @param {LLA} other - Other LLA to compare
145
+ * @param {number} tolerance - Tolerance for comparison (default: 1e-9)
146
+ * @return {boolean} True if equal within tolerance
147
+ */
148
+ equals(other, tolerance = 1e-9) {
149
+ return Math.abs(this.Latitude.Degrees - other.Latitude.Degrees) < tolerance
150
+ && Math.abs(this.Longitude.Degrees - other.Longitude.Degrees) < tolerance
151
+ && Math.abs(this.AltitudeKm - other.AltitudeKm) < tolerance;
152
+ }
153
+ }
154
+
155
+ // Simple Angle class (you may already have this)
156
+ class Angle {
157
+ constructor(degrees) {
158
+ this._degrees = degrees;
159
+ }
160
+
161
+ get Degrees() {
162
+ return this._degrees;
163
+ }
164
+
165
+ get Radians() {
166
+ return this._degrees * Math.PI / 180;
167
+ }
168
+
169
+ static FromDegrees(degrees) {
170
+ return new Angle(degrees);
171
+ }
172
+
173
+ static FromRadians(radians) {
174
+ return new Angle(radians * 180 / Math.PI);
175
+ }
176
+ }
177
+
178
+ // Export for use
179
+ export {LLA, Angle};