@itowns/geographic 2.46.1-next.0 → 2.46.1-next.10
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/lib/Coordinates.js +18 -29
- package/lib/Crs.js +18 -1
- package/lib/Ellipsoid.js +19 -24
- package/lib/Extent.js +29 -51
- package/lib/OrientationUtils.js +46 -95
- package/package.json +7 -7
- package/src/Coordinates.ts +23 -24
- package/src/Crs.ts +35 -10
- package/src/Ellipsoid.ts +31 -30
- package/src/Extent.ts +33 -27
- package/src/OrientationUtils.ts +21 -23
- package/src/{Main.ts → index.ts} +3 -2
- package/lib/CoordStars.d.ts +0 -13
- package/lib/Coordinates.d.ts +0 -196
- package/lib/Crs.d.ts +0 -93
- package/lib/Ellipsoid.d.ts +0 -74
- package/lib/Extent.d.ts +0 -246
- package/lib/Main.d.ts +0 -6
- package/lib/OrientationUtils.d.ts +0 -105
- /package/lib/{Main.js → index.js} +0 -0
package/lib/OrientationUtils.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { Euler, MathUtils, Matrix4, Quaternion, Vector3 } from 'three';
|
|
2
2
|
import proj4 from 'proj4';
|
|
3
3
|
import Coordinates from "./Coordinates.js";
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const quat = new Quaternion();
|
|
4
|
+
const matrix = /* @__PURE__ */new Matrix4();
|
|
5
|
+
const north = /* @__PURE__ */new Vector3();
|
|
6
|
+
const east = /* @__PURE__ */new Vector3();
|
|
7
|
+
const axis = /* @__PURE__ */new Vector3().set(0, 0, 1);
|
|
8
|
+
const coord = /* @__PURE__ */new Coordinates('EPSG:4326', 0, 0, 0);
|
|
9
|
+
const euler = /* @__PURE__ */new Euler();
|
|
10
|
+
const quat = /* @__PURE__ */new Quaternion();
|
|
12
11
|
/**
|
|
13
12
|
* The transform from the platform frame to the local East, North, Up (ENU)
|
|
14
13
|
* frame is `RotationZ(heading).RotationX(pitch).RotationY(roll)`.
|
|
@@ -20,14 +19,10 @@ const quat = new Quaternion();
|
|
|
20
19
|
*
|
|
21
20
|
* @returns The target quaternion
|
|
22
21
|
*/
|
|
23
|
-
export function quaternionFromRollPitchHeading() {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
let target = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new Quaternion();
|
|
28
|
-
roll *= DEG2RAD;
|
|
29
|
-
pitch *= DEG2RAD;
|
|
30
|
-
heading *= DEG2RAD;
|
|
22
|
+
export function quaternionFromRollPitchHeading(roll = 0, pitch = 0, heading = 0, target = new Quaternion()) {
|
|
23
|
+
roll *= MathUtils.DEG2RAD;
|
|
24
|
+
pitch *= MathUtils.DEG2RAD;
|
|
25
|
+
heading *= MathUtils.DEG2RAD;
|
|
31
26
|
// return setFromEuler(euler.set(pitch, roll, heading , 'ZXY')).conjugate();
|
|
32
27
|
// Below is optimized version of above line
|
|
33
28
|
return target.setFromEuler(euler.set(-pitch, -roll, -heading, 'YXZ'));
|
|
@@ -54,14 +49,10 @@ export function quaternionFromRollPitchHeading() {
|
|
|
54
49
|
*
|
|
55
50
|
* @returns The target quaternion
|
|
56
51
|
*/
|
|
57
|
-
export function quaternionFromOmegaPhiKappa() {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
let target = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new Quaternion();
|
|
62
|
-
omega *= DEG2RAD;
|
|
63
|
-
phi *= DEG2RAD;
|
|
64
|
-
kappa *= DEG2RAD;
|
|
52
|
+
export function quaternionFromOmegaPhiKappa(omega = 0, phi = 0, kappa = 0, target = new Quaternion()) {
|
|
53
|
+
omega *= MathUtils.DEG2RAD;
|
|
54
|
+
phi *= MathUtils.DEG2RAD;
|
|
55
|
+
kappa *= MathUtils.DEG2RAD;
|
|
65
56
|
target.setFromEuler(euler.set(omega, phi, kappa, 'XYZ'));
|
|
66
57
|
target.set(target.w, target.z, -target.y, -target.x);
|
|
67
58
|
// <=> target.multiply(new THREE.Quaternion(1, 0, 0, 0));
|
|
@@ -77,8 +68,7 @@ export function quaternionFromOmegaPhiKappa() {
|
|
|
77
68
|
*
|
|
78
69
|
* @returns The target quaternion
|
|
79
70
|
*/
|
|
80
|
-
export function quaternionFromAttitude(attitude) {
|
|
81
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
71
|
+
export function quaternionFromAttitude(attitude, target = new Quaternion()) {
|
|
82
72
|
if ('roll' in attitude || 'pitch' in attitude || 'heading' in attitude) {
|
|
83
73
|
return quaternionFromRollPitchHeading(attitude.roll, attitude.pitch, attitude.heading, target);
|
|
84
74
|
}
|
|
@@ -98,13 +88,11 @@ export function quaternionFromAttitude(attitude) {
|
|
|
98
88
|
* @returns The target quaternion if coordinates is defined. Otherwise, a
|
|
99
89
|
* function to compute it from coordinates.
|
|
100
90
|
*/
|
|
101
|
-
export function quaternionFromEnuToGeocent(coordinates) {
|
|
102
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
91
|
+
export function quaternionFromEnuToGeocent(coordinates, target = new Quaternion()) {
|
|
103
92
|
if (coordinates) {
|
|
104
93
|
return quaternionFromEnuToGeocent()(coordinates, target);
|
|
105
94
|
}
|
|
106
|
-
return
|
|
107
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
95
|
+
return (coordinates, target = new Quaternion()) => {
|
|
108
96
|
const up = coordinates.geodesicNormal;
|
|
109
97
|
if (up.x == 0 && up.y == 0) {
|
|
110
98
|
return target.set(0, 0, 0, 1);
|
|
@@ -128,16 +116,12 @@ export function quaternionFromEnuToGeocent(coordinates) {
|
|
|
128
116
|
* @returns The target quaternion if coordinates is defined. Otherwise, a
|
|
129
117
|
* function to compute it from coordinates.
|
|
130
118
|
*/
|
|
131
|
-
export function quaternionFromGeocentToEnu(coordinates) {
|
|
132
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
119
|
+
export function quaternionFromGeocentToEnu(coordinates, target = new Quaternion()) {
|
|
133
120
|
if (coordinates) {
|
|
134
121
|
return quaternionFromGeocentToEnu()(coordinates, target);
|
|
135
122
|
}
|
|
136
123
|
const toGeocent = quaternionFromEnuToGeocent();
|
|
137
|
-
return
|
|
138
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
139
|
-
return toGeocent(coordinates, target).conjugate();
|
|
140
|
-
};
|
|
124
|
+
return (coordinates, target = new Quaternion()) => toGeocent(coordinates, target).conjugate();
|
|
141
125
|
}
|
|
142
126
|
/**
|
|
143
127
|
* Computes the rotation from a Lambert Conformal Conic (LCC) frame to the local
|
|
@@ -153,15 +137,13 @@ export function quaternionFromGeocentToEnu(coordinates) {
|
|
|
153
137
|
* @returns The target quaternion if coordinates is defined. Otherwise, a
|
|
154
138
|
* function to compute it from coordinates.
|
|
155
139
|
*/
|
|
156
|
-
export function quaternionFromLCCToEnu(proj, coordinates) {
|
|
157
|
-
let target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Quaternion();
|
|
140
|
+
export function quaternionFromLCCToEnu(proj, coordinates, target = new Quaternion()) {
|
|
158
141
|
if (coordinates) {
|
|
159
142
|
return quaternionFromLCCToEnu(proj)(coordinates, target);
|
|
160
143
|
}
|
|
161
144
|
const sinlat0 = Math.sin(proj.lat0);
|
|
162
|
-
return
|
|
163
|
-
|
|
164
|
-
const long = coordinates.as(coord.crs, coord).longitude * DEG2RAD;
|
|
145
|
+
return (coordinates, target = new Quaternion()) => {
|
|
146
|
+
const long = coordinates.as(coord.crs, coord).longitude * MathUtils.DEG2RAD;
|
|
165
147
|
return target.setFromAxisAngle(axis, sinlat0 * (proj.long0 - long));
|
|
166
148
|
};
|
|
167
149
|
}
|
|
@@ -178,16 +160,12 @@ export function quaternionFromLCCToEnu(proj, coordinates) {
|
|
|
178
160
|
* @returns The target quaternion if coordinates is defined. Otherwise, a
|
|
179
161
|
* function to compute it from coordinates.
|
|
180
162
|
*/
|
|
181
|
-
export function quaternionFromEnuToLCC(proj, coordinates) {
|
|
182
|
-
let target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Quaternion();
|
|
163
|
+
export function quaternionFromEnuToLCC(proj, coordinates, target = new Quaternion()) {
|
|
183
164
|
if (coordinates) {
|
|
184
165
|
return quaternionFromEnuToLCC(proj)(coordinates, target);
|
|
185
166
|
}
|
|
186
167
|
const fromLCC = quaternionFromLCCToEnu(proj);
|
|
187
|
-
return
|
|
188
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
189
|
-
return fromLCC(coordinates, target).conjugate();
|
|
190
|
-
};
|
|
168
|
+
return (coordinates, target = new Quaternion()) => fromLCC(coordinates, target).conjugate();
|
|
191
169
|
}
|
|
192
170
|
/**
|
|
193
171
|
* Computes the rotation from a Transverse Mercator frame (TMerc) to the
|
|
@@ -202,8 +180,7 @@ export function quaternionFromEnuToLCC(proj, coordinates) {
|
|
|
202
180
|
* @returns The target quaternion if coordinates is defined. Otherwise, a
|
|
203
181
|
* function to compute it from coordinates.
|
|
204
182
|
*/
|
|
205
|
-
export function quaternionFromTMercToEnu(proj, coordinates) {
|
|
206
|
-
let target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Quaternion();
|
|
183
|
+
export function quaternionFromTMercToEnu(proj, coordinates, target = new Quaternion()) {
|
|
207
184
|
if (coordinates) {
|
|
208
185
|
return quaternionFromTMercToEnu(proj)(coordinates, target);
|
|
209
186
|
}
|
|
@@ -216,11 +193,10 @@ export function quaternionFromTMercToEnu(proj, coordinates) {
|
|
|
216
193
|
const e2 = proj.e * proj.e;
|
|
217
194
|
eta0 = e2 / (1 - e2);
|
|
218
195
|
}
|
|
219
|
-
return
|
|
220
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
196
|
+
return (coordinates, target = new Quaternion()) => {
|
|
221
197
|
coordinates.as(coord.crs, coord);
|
|
222
|
-
const long = coord.longitude * DEG2RAD;
|
|
223
|
-
const lat = coord.latitude * DEG2RAD;
|
|
198
|
+
const long = coord.longitude * MathUtils.DEG2RAD;
|
|
199
|
+
const lat = coord.latitude * MathUtils.DEG2RAD;
|
|
224
200
|
const dlong = proj.long0 - long;
|
|
225
201
|
const coslat = Math.cos(lat);
|
|
226
202
|
const sinlat = Math.sin(lat);
|
|
@@ -245,16 +221,12 @@ export function quaternionFromTMercToEnu(proj, coordinates) {
|
|
|
245
221
|
* @returns The target quaternion if coordinates is defined. Otherwise, a
|
|
246
222
|
* function to compute it from coordinates.
|
|
247
223
|
*/
|
|
248
|
-
export function quaternionFromEnuToTMerc(proj, coordinates) {
|
|
249
|
-
let target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Quaternion();
|
|
224
|
+
export function quaternionFromEnuToTMerc(proj, coordinates, target = new Quaternion()) {
|
|
250
225
|
if (coordinates) {
|
|
251
226
|
return quaternionFromEnuToTMerc(proj)(coordinates, target);
|
|
252
227
|
}
|
|
253
228
|
const fromTMerc = quaternionFromTMercToEnu(proj);
|
|
254
|
-
return
|
|
255
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
256
|
-
return fromTMerc(coordinates, target).conjugate();
|
|
257
|
-
};
|
|
229
|
+
return (coordinates, target = new Quaternion()) => fromTMerc(coordinates, target).conjugate();
|
|
258
230
|
}
|
|
259
231
|
/**
|
|
260
232
|
* Computes the rotation from a LongLat frame to the local East North Up
|
|
@@ -267,12 +239,8 @@ export function quaternionFromEnuToTMerc(proj, coordinates) {
|
|
|
267
239
|
* @returns The target quaternion if coordinates is defined, otherwise, a
|
|
268
240
|
* function to compute it from coordinates.
|
|
269
241
|
*/
|
|
270
|
-
export function quaternionFromLongLatToEnu(coordinates) {
|
|
271
|
-
|
|
272
|
-
return coordinates ? target.set(0, 0, 0, 1) : function (coordinates) {
|
|
273
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
274
|
-
return quaternionFromLongLatToEnu(coordinates, target);
|
|
275
|
-
};
|
|
242
|
+
export function quaternionFromLongLatToEnu(coordinates, target = new Quaternion()) {
|
|
243
|
+
return coordinates ? target.set(0, 0, 0, 1) : (coordinates, target = new Quaternion()) => quaternionFromLongLatToEnu(coordinates, target);
|
|
276
244
|
}
|
|
277
245
|
/**
|
|
278
246
|
* Computes the rotation from the local East North Up (ENU) frame to a
|
|
@@ -284,12 +252,8 @@ export function quaternionFromLongLatToEnu(coordinates) {
|
|
|
284
252
|
* @returns The target quaternion if coordinates is defined, otherwise, a
|
|
285
253
|
* function to compute it from coordinates.
|
|
286
254
|
*/
|
|
287
|
-
export function quaternionFromEnuToLongLat(coordinates) {
|
|
288
|
-
|
|
289
|
-
return coordinates ? target.set(0, 0, 0, 1) : function (coordinates) {
|
|
290
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
291
|
-
return quaternionFromEnuToLongLat(coordinates, target);
|
|
292
|
-
};
|
|
255
|
+
export function quaternionFromEnuToLongLat(coordinates, target = new Quaternion()) {
|
|
256
|
+
return coordinates ? target.set(0, 0, 0, 1) : (coordinates, target = new Quaternion()) => quaternionFromEnuToLongLat(coordinates, target);
|
|
293
257
|
}
|
|
294
258
|
/**
|
|
295
259
|
* Warns for an unimplemented projection, sets the quaternion to the
|
|
@@ -301,13 +265,9 @@ export function quaternionFromEnuToLongLat(coordinates) {
|
|
|
301
265
|
* @returns The target quaternion if coordinates is defined, otherwise, a
|
|
302
266
|
* function to compute it from coordinates.
|
|
303
267
|
*/
|
|
304
|
-
export function quaternionUnimplemented(proj, coordinates) {
|
|
305
|
-
let target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Quaternion();
|
|
268
|
+
export function quaternionUnimplemented(proj, coordinates, target = new Quaternion()) {
|
|
306
269
|
console.warn('This quaternion function is not implemented for projections of type', proj.projName);
|
|
307
|
-
return coordinates ? target.set(0, 0, 0, 1) :
|
|
308
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
309
|
-
return quaternionUnimplemented(proj, coordinates, target);
|
|
310
|
-
};
|
|
270
|
+
return coordinates ? target.set(0, 0, 0, 1) : (coordinates, target = new Quaternion()) => quaternionUnimplemented(proj, coordinates, target);
|
|
311
271
|
}
|
|
312
272
|
/**
|
|
313
273
|
* Compute the quaternion that models the rotation from the local East North
|
|
@@ -320,14 +280,13 @@ export function quaternionUnimplemented(proj, coordinates) {
|
|
|
320
280
|
* @returns The target quaternion if coordinates is defined, otherwise, a
|
|
321
281
|
* function to compute it from coordinates.
|
|
322
282
|
*/
|
|
323
|
-
export function quaternionFromEnuToCRS(crsOrProj, coordinates) {
|
|
324
|
-
let target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Quaternion();
|
|
283
|
+
export function quaternionFromEnuToCRS(crsOrProj, coordinates, target = new Quaternion()) {
|
|
325
284
|
if (coordinates) {
|
|
326
285
|
return quaternionFromEnuToCRS(crsOrProj)(coordinates, target);
|
|
327
286
|
}
|
|
328
287
|
const proj = typeof crsOrProj === 'string' ? proj4(crsOrProj).oProj : crsOrProj;
|
|
329
|
-
const
|
|
330
|
-
switch (names[0]) {
|
|
288
|
+
const projOrFalse = proj4.Proj.projections.get(proj.projName);
|
|
289
|
+
switch (projOrFalse && projOrFalse.names[0]) {
|
|
331
290
|
case 'Geocentric':
|
|
332
291
|
return quaternionFromEnuToGeocent();
|
|
333
292
|
case 'Lambert Tangential Conformal Conic Projection':
|
|
@@ -351,14 +310,13 @@ export function quaternionFromEnuToCRS(crsOrProj, coordinates) {
|
|
|
351
310
|
* @returns The target quaternion if coordinates is defined, otherwise, a
|
|
352
311
|
* function to compute it from coordinates.
|
|
353
312
|
*/
|
|
354
|
-
export function quaternionFromCRSToEnu(crsOrProj, coordinates) {
|
|
355
|
-
let target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Quaternion();
|
|
313
|
+
export function quaternionFromCRSToEnu(crsOrProj, coordinates, target = new Quaternion()) {
|
|
356
314
|
if (coordinates) {
|
|
357
315
|
return quaternionFromCRSToEnu(crsOrProj)(coordinates, target);
|
|
358
316
|
}
|
|
359
317
|
const proj = typeof crsOrProj === 'string' ? proj4(crsOrProj).oProj : crsOrProj;
|
|
360
|
-
const
|
|
361
|
-
switch (names[0]) {
|
|
318
|
+
const projOrFalse = proj4.Proj.projections.get(proj.projName);
|
|
319
|
+
switch (projOrFalse && projOrFalse.names[0]) {
|
|
362
320
|
case 'Geocentric':
|
|
363
321
|
return quaternionFromGeocentToEnu();
|
|
364
322
|
case 'Lambert Tangential Conformal Conic Projection':
|
|
@@ -382,23 +340,16 @@ export function quaternionFromCRSToEnu(crsOrProj, coordinates) {
|
|
|
382
340
|
* @returns The target quaternion if coordinates is defined, otherwise, a
|
|
383
341
|
* function to compute it from coordinates.
|
|
384
342
|
*/
|
|
385
|
-
export function quaternionFromCRSToCRS(crsIn, crsOut, coordinates) {
|
|
386
|
-
let target = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new Quaternion();
|
|
343
|
+
export function quaternionFromCRSToCRS(crsIn, crsOut, coordinates, target = new Quaternion()) {
|
|
387
344
|
if (coordinates) {
|
|
388
345
|
return quaternionFromCRSToCRS(crsIn, crsOut)(coordinates, target);
|
|
389
346
|
}
|
|
390
347
|
if (crsIn == crsOut) {
|
|
391
|
-
return
|
|
392
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
393
|
-
return target.set(0, 0, 0, 1);
|
|
394
|
-
};
|
|
348
|
+
return (origin, target = new Quaternion()) => target.set(0, 0, 0, 1);
|
|
395
349
|
}
|
|
396
350
|
|
|
397
351
|
// get rotations from the local East/North/Up (ENU) frame to both CRS.
|
|
398
352
|
const fromCrs = quaternionFromCRSToEnu(crsIn);
|
|
399
353
|
const toCrs = quaternionFromEnuToCRS(crsOut);
|
|
400
|
-
return
|
|
401
|
-
let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Quaternion();
|
|
402
|
-
return toCrs(origin, target).multiply(fromCrs(origin, quat));
|
|
403
|
-
};
|
|
354
|
+
return (origin, target = new Quaternion()) => toCrs(origin, target).multiply(fromCrs(origin, quat));
|
|
404
355
|
}
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@itowns/geographic",
|
|
3
|
-
"version": "2.46.1-next.
|
|
3
|
+
"version": "2.46.1-next.10",
|
|
4
4
|
"description": "Geodesy",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "lib/
|
|
6
|
+
"main": "lib/index.js",
|
|
7
7
|
"exports": {
|
|
8
|
-
"types": "./src/
|
|
9
|
-
"default": "./lib/
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"default": "./lib/index.js"
|
|
10
10
|
},
|
|
11
|
-
"types": "./src/
|
|
11
|
+
"types": "./src/index.ts",
|
|
12
12
|
"scripts": {
|
|
13
13
|
"build": "",
|
|
14
14
|
"lint": "eslint \"src/**/*.{js,ts,tsx}\" \"test/**/*.js\"",
|
|
15
15
|
"transpile": "tsc && cross-env BABEL_DISABLE_CACHE=1 babel src --out-dir lib --extensions .js,.ts",
|
|
16
16
|
"test-unit": "npm run base-test-unit test/unit",
|
|
17
|
-
"base-test-unit": "cross-env BABEL_DISABLE_CACHE=1 mocha --import=../../config/babel-register/register.mjs",
|
|
17
|
+
"base-test-unit": "cross-env BABEL_DISABLE_CACHE=1 NODE_OPTIONS=--no-experimental-strip-types mocha --import=../../config/babel-register/register.mjs",
|
|
18
18
|
"test-with-coverage": "c8 -n src -r html cross-env npm run test-unit",
|
|
19
19
|
"test-with-coverage_lcov": "c8 -n src --reporter=lcov cross-env npm run test-unit",
|
|
20
20
|
"watch": "npm run transpile -- --watch",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"url": "https://github.com/itowns/itowns/issues"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"proj4": "^2.19.
|
|
38
|
+
"proj4": "^2.19.10",
|
|
39
39
|
"three": "^0.174.0"
|
|
40
40
|
},
|
|
41
41
|
"homepage": "https://itowns.github.io/"
|
package/src/Coordinates.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import proj4 from 'proj4';
|
|
3
|
-
import type { Converter } from 'proj4/dist/lib/core';
|
|
1
|
+
import { Vector3, type Vector3Like, type Matrix4, MathUtils } from 'three';
|
|
2
|
+
import proj4, { type Converter } from 'proj4';
|
|
4
3
|
import Ellipsoid from './Ellipsoid';
|
|
5
4
|
import * as CRS from './Crs';
|
|
6
5
|
|
|
7
|
-
import type {
|
|
6
|
+
import type { ProjectionLike } from './Crs';
|
|
8
7
|
|
|
9
|
-
const ellipsoid = new Ellipsoid();
|
|
8
|
+
const ellipsoid = /* @__PURE__ */ new Ellipsoid();
|
|
10
9
|
const projectionCache: Record<string, Record<string, Converter>> = {};
|
|
11
10
|
|
|
12
|
-
const v0 = new
|
|
13
|
-
const v1 = new
|
|
11
|
+
const v0 = /* @__PURE__ */ new Vector3();
|
|
12
|
+
const v1 = /* @__PURE__ */ new Vector3();
|
|
14
13
|
|
|
15
14
|
let coord0: Coordinates;
|
|
16
15
|
let coord1: Coordinates;
|
|
@@ -22,7 +21,7 @@ export interface CoordinatesLike {
|
|
|
22
21
|
readonly z: number;
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
function proj4cache(crsIn:
|
|
24
|
+
function proj4cache(crsIn: ProjectionLike, crsOut: ProjectionLike): Converter {
|
|
26
25
|
if (!projectionCache[crsIn]) {
|
|
27
26
|
projectionCache[crsIn] = {};
|
|
28
27
|
}
|
|
@@ -72,9 +71,9 @@ class Coordinates {
|
|
|
72
71
|
*/
|
|
73
72
|
readonly isCoordinates: boolean;
|
|
74
73
|
/**
|
|
75
|
-
* A default or user-defined CRS (see {@link
|
|
74
|
+
* A default or user-defined CRS (see {@link ProjectionLike}).
|
|
76
75
|
*/
|
|
77
|
-
crs:
|
|
76
|
+
crs: ProjectionLike;
|
|
78
77
|
|
|
79
78
|
/** The x value (or longitude) of this coordinate. */
|
|
80
79
|
x: number;
|
|
@@ -83,16 +82,16 @@ class Coordinates {
|
|
|
83
82
|
/** The z value (or altitude) of this coordinate. */
|
|
84
83
|
z: number;
|
|
85
84
|
|
|
86
|
-
private _normal:
|
|
85
|
+
private _normal: Vector3;
|
|
87
86
|
private _normalNeedsUpdate: boolean;
|
|
88
87
|
|
|
89
88
|
/**
|
|
90
|
-
* @param crs - A default or user-defined CRS (see {@link
|
|
89
|
+
* @param crs - A default or user-defined CRS (see {@link ProjectionLike}).
|
|
91
90
|
* @param x - x or longitude value.
|
|
92
91
|
* @param y - y or latitude value.
|
|
93
92
|
* @param z - z or altitude value.
|
|
94
93
|
*/
|
|
95
|
-
constructor(crs:
|
|
94
|
+
constructor(crs: ProjectionLike, x: number = 0, y: number = 0, z: number = 0) {
|
|
96
95
|
this.isCoordinates = true;
|
|
97
96
|
|
|
98
97
|
CRS.isValid(crs);
|
|
@@ -105,7 +104,7 @@ class Coordinates {
|
|
|
105
104
|
this.z = 0;
|
|
106
105
|
|
|
107
106
|
// Normal
|
|
108
|
-
this._normal = new
|
|
107
|
+
this._normal = new Vector3();
|
|
109
108
|
|
|
110
109
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
111
110
|
if ((x as any).length > 0) { // deepscan-disable-line
|
|
@@ -134,7 +133,7 @@ class Coordinates {
|
|
|
134
133
|
* Sets the Coordinate Reference System.
|
|
135
134
|
* @param crs - Coordinate Reference System (e.g. 'EPSG:4978')
|
|
136
135
|
*/
|
|
137
|
-
setCrs(crs:
|
|
136
|
+
setCrs(crs: ProjectionLike): this {
|
|
138
137
|
CRS.isValid(crs);
|
|
139
138
|
this.crs = crs;
|
|
140
139
|
return this;
|
|
@@ -180,7 +179,7 @@ class Coordinates {
|
|
|
180
179
|
*
|
|
181
180
|
* @param v - The source object.
|
|
182
181
|
*/
|
|
183
|
-
setFromVector3(v:
|
|
182
|
+
setFromVector3(v: Vector3Like): this {
|
|
184
183
|
return this.setFromValues(v.x, v.y, v.z);
|
|
185
184
|
}
|
|
186
185
|
|
|
@@ -247,7 +246,7 @@ class Coordinates {
|
|
|
247
246
|
* @returns A vector `(x, y, z)`, or copies x, y and z into the provided
|
|
248
247
|
* vector.
|
|
249
248
|
*/
|
|
250
|
-
toVector3(target:
|
|
249
|
+
toVector3(target: Vector3 = new Vector3()): Vector3 {
|
|
251
250
|
return target.copy(this);
|
|
252
251
|
}
|
|
253
252
|
|
|
@@ -262,7 +261,7 @@ class Coordinates {
|
|
|
262
261
|
* array.
|
|
263
262
|
*/
|
|
264
263
|
toArray(array: number[] = [], offset: number = 0): ArrayLike<number> {
|
|
265
|
-
return
|
|
264
|
+
return Vector3.prototype.toArray.call(this, array, offset);
|
|
266
265
|
}
|
|
267
266
|
|
|
268
267
|
/**
|
|
@@ -306,8 +305,8 @@ class Coordinates {
|
|
|
306
305
|
*
|
|
307
306
|
* @param mat - The matrix.
|
|
308
307
|
*/
|
|
309
|
-
applyMatrix4(mat:
|
|
310
|
-
|
|
308
|
+
applyMatrix4(mat: Matrix4): this {
|
|
309
|
+
Vector3.prototype.applyMatrix4.call(this, mat);
|
|
311
310
|
return this;
|
|
312
311
|
}
|
|
313
312
|
|
|
@@ -341,12 +340,12 @@ class Coordinates {
|
|
|
341
340
|
* const geographicCoords = geocentricCoords.as('EPSG:4326');
|
|
342
341
|
* ```
|
|
343
342
|
*/
|
|
344
|
-
as(crs:
|
|
343
|
+
as(crs: ProjectionLike, target = new Coordinates(crs)): Coordinates {
|
|
345
344
|
if (this.crs == crs) {
|
|
346
345
|
target.copy(this);
|
|
347
346
|
} else {
|
|
348
347
|
if (CRS.is4326(this.crs) && crs == 'EPSG:3857') {
|
|
349
|
-
this.y =
|
|
348
|
+
this.y = MathUtils.clamp(this.y, -89.999999, 89.999999);
|
|
350
349
|
}
|
|
351
350
|
|
|
352
351
|
target.setFromArray(proj4cache(this.crs, crs)
|
|
@@ -359,7 +358,7 @@ class Coordinates {
|
|
|
359
358
|
}
|
|
360
359
|
}
|
|
361
360
|
|
|
362
|
-
coord0 = new Coordinates('EPSG:4326', 0, 0, 0);
|
|
363
|
-
coord1 = new Coordinates('EPSG:4326', 0, 0, 0);
|
|
361
|
+
coord0 = /* @__PURE__ */ new Coordinates('EPSG:4326', 0, 0, 0);
|
|
362
|
+
coord1 = /* @__PURE__ */ new Coordinates('EPSG:4326', 0, 0, 0);
|
|
364
363
|
|
|
365
364
|
export default Coordinates;
|
package/src/Crs.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import proj4 from 'proj4';
|
|
2
|
-
|
|
3
2
|
import type { ProjectionDefinition } from 'proj4/dist/lib/defs';
|
|
4
3
|
|
|
4
|
+
type proj4Def = {
|
|
5
|
+
type: string,
|
|
6
|
+
PROJCS: proj4Def,
|
|
7
|
+
unknown?: string,
|
|
8
|
+
(alias: string): proj4Def & { name: string },
|
|
9
|
+
title: string,
|
|
10
|
+
}
|
|
11
|
+
|
|
5
12
|
proj4.defs('EPSG:4978', '+proj=geocent +datum=WGS84 +units=m +no_defs');
|
|
6
13
|
|
|
7
14
|
// Redefining proj4 global projections to match epsg.org database axis order.
|
|
@@ -15,7 +22,7 @@ proj4.defs('WGS84').axis = 'neu';
|
|
|
15
22
|
* projection definition previously defined with
|
|
16
23
|
* [`proj4.defs`](https://github.com/proj4js/proj4js#named-projections).
|
|
17
24
|
*/
|
|
18
|
-
export type
|
|
25
|
+
export type ProjectionLike = string;
|
|
19
26
|
|
|
20
27
|
function isString(s: unknown): s is string {
|
|
21
28
|
return typeof s === 'string' || s instanceof String;
|
|
@@ -53,7 +60,7 @@ export const UNIT = {
|
|
|
53
60
|
*
|
|
54
61
|
* @param crs - The CRS to test.
|
|
55
62
|
*/
|
|
56
|
-
export function is4326(crs:
|
|
63
|
+
export function is4326(crs: ProjectionLike) {
|
|
57
64
|
return crs === 'EPSG:4326';
|
|
58
65
|
}
|
|
59
66
|
|
|
@@ -79,7 +86,7 @@ function unitFromProj4Unit(proj: ProjectionDefinition) {
|
|
|
79
86
|
* @param crs - The CRS to extract the unit from.
|
|
80
87
|
* @returns Either `UNIT.METER`, `UNIT.DEGREE`, `UNIT.FOOT` or `undefined`.
|
|
81
88
|
*/
|
|
82
|
-
export function getUnit(crs:
|
|
89
|
+
export function getUnit(crs: ProjectionLike) {
|
|
83
90
|
mustBeString(crs);
|
|
84
91
|
const p = proj4.defs(crs);
|
|
85
92
|
if (!p) {
|
|
@@ -94,7 +101,7 @@ export function getUnit(crs: ProjectionAlias) {
|
|
|
94
101
|
* @param crs - The CRS to check.
|
|
95
102
|
* @throws {@link Error} if the CRS is not valid.
|
|
96
103
|
*/
|
|
97
|
-
export function isMetricUnit(crs:
|
|
104
|
+
export function isMetricUnit(crs: ProjectionLike) {
|
|
98
105
|
return getUnit(crs) === UNIT.METER;
|
|
99
106
|
}
|
|
100
107
|
|
|
@@ -104,7 +111,7 @@ export function isMetricUnit(crs: ProjectionAlias) {
|
|
|
104
111
|
* @param crs - The CRS to check.
|
|
105
112
|
* @throws {@link Error} if the CRS is not valid.
|
|
106
113
|
*/
|
|
107
|
-
export function isGeographic(crs:
|
|
114
|
+
export function isGeographic(crs: ProjectionLike) {
|
|
108
115
|
return getUnit(crs) === UNIT.DEGREE;
|
|
109
116
|
}
|
|
110
117
|
|
|
@@ -114,7 +121,7 @@ export function isGeographic(crs: ProjectionAlias) {
|
|
|
114
121
|
* @param crs - The CRS to test.
|
|
115
122
|
* @returns false if the crs isn't defined.
|
|
116
123
|
*/
|
|
117
|
-
export function isGeocentric(crs:
|
|
124
|
+
export function isGeocentric(crs: ProjectionLike) {
|
|
118
125
|
mustBeString(crs);
|
|
119
126
|
const projection = proj4.defs(crs);
|
|
120
127
|
return !projection ? false : projection.projName == 'geocent';
|
|
@@ -127,7 +134,7 @@ export function isGeocentric(crs: ProjectionAlias) {
|
|
|
127
134
|
* @param crs - The CRS to test.
|
|
128
135
|
* @throws {@link Error} if the crs is not valid.
|
|
129
136
|
*/
|
|
130
|
-
export function isValid(crs:
|
|
137
|
+
export function isValid(crs: ProjectionLike) {
|
|
131
138
|
const proj = proj4.defs(crs);
|
|
132
139
|
if (!proj) {
|
|
133
140
|
throw new Error(`Undefined crs '${crs}'. Add it with proj4.defs('${crs}', string)`);
|
|
@@ -143,7 +150,7 @@ export function isValid(crs: ProjectionAlias) {
|
|
|
143
150
|
* @param crs - The CRS to use.
|
|
144
151
|
* @returns 0.01 if the CRS is EPSG:4326, 0.001 otherwise.
|
|
145
152
|
*/
|
|
146
|
-
export function reasonableEpsilon(crs:
|
|
153
|
+
export function reasonableEpsilon(crs: ProjectionLike) {
|
|
147
154
|
if (is4326(crs)) {
|
|
148
155
|
return 0.01;
|
|
149
156
|
} else {
|
|
@@ -158,7 +165,7 @@ export function reasonableEpsilon(crs: ProjectionAlias) {
|
|
|
158
165
|
* @param crs - The CRS to get axis from.
|
|
159
166
|
* @returns the matching proj4 axis string, 'enu' for instance (east, north, up)
|
|
160
167
|
*/
|
|
161
|
-
export function axisOrder(crs:
|
|
168
|
+
export function axisOrder(crs: ProjectionLike) {
|
|
162
169
|
mustBeString(crs);
|
|
163
170
|
const projection = proj4.defs(crs);
|
|
164
171
|
return !projection ? undefined : projection.axis;
|
|
@@ -174,3 +181,21 @@ export function axisOrder(crs: ProjectionAlias) {
|
|
|
174
181
|
* @param proj4def - Proj4 or WKT string of the defined projection.
|
|
175
182
|
*/
|
|
176
183
|
export const defs = (code: string, proj4def: string) => proj4.defs(code, proj4def);
|
|
184
|
+
|
|
185
|
+
export function defsFromWkt(wkt: string) {
|
|
186
|
+
proj4.defs('unknown', wkt);
|
|
187
|
+
const proj4Defs = proj4.defs as unknown as proj4Def;
|
|
188
|
+
let projCS;
|
|
189
|
+
if (proj4Defs('unknown').type === 'COMPD_CS') {
|
|
190
|
+
console.warn('Compound coordinate system is not yet supported.');
|
|
191
|
+
projCS = proj4Defs('unknown').PROJCS;
|
|
192
|
+
} else {
|
|
193
|
+
projCS = proj4Defs('unknown');
|
|
194
|
+
}
|
|
195
|
+
const crsAlias = projCS.title || projCS.name || 'EPSG:XXXX';
|
|
196
|
+
if (!(crsAlias in proj4.defs)) {
|
|
197
|
+
proj4.defs(crsAlias, projCS);
|
|
198
|
+
}
|
|
199
|
+
delete proj4Defs.unknown;
|
|
200
|
+
return crsAlias;
|
|
201
|
+
}
|