@itowns/geographic 2.44.2

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/Extent.ts ADDED
@@ -0,0 +1,602 @@
1
+ import * as THREE from 'three';
2
+ import Coordinates from './Coordinates';
3
+ import * as CRS from './Crs';
4
+
5
+ import type { ProjectionLike } from './Crs';
6
+
7
+ const _dim = new THREE.Vector2();
8
+ const _dim2 = new THREE.Vector2();
9
+ const _box = new THREE.Box3();
10
+ const defaultScheme = new THREE.Vector2(2, 2);
11
+
12
+ const cNorthWest = new Coordinates('EPSG:4326', 0, 0, 0);
13
+ const cSouthWest = new Coordinates('EPSG:4326', 0, 0, 0);
14
+ const cNorthEast = new Coordinates('EPSG:4326', 0, 0, 0);
15
+
16
+ const southWest = new THREE.Vector3();
17
+ const northEast = new THREE.Vector3();
18
+
19
+ let _extent: Extent;
20
+
21
+ const cardinals = new Array(8);
22
+ for (let i = cardinals.length - 1; i >= 0; i--) {
23
+ cardinals[i] = new Coordinates('EPSG:4326', 0, 0, 0);
24
+ }
25
+
26
+ const _c = new Coordinates('EPSG:4326', 0, 0);
27
+
28
+ export interface ExtentLike {
29
+ readonly west: number;
30
+ readonly east: number;
31
+ readonly south: number;
32
+ readonly north: number;
33
+ }
34
+
35
+ /**
36
+ * A class representing a geographical extent.
37
+ *
38
+ * An extent is a geographical bounding rectangle defined by 4 limits: west,
39
+ * east, south and north.
40
+ *
41
+ * **Warning**: Using a geocentric projection is not suitable for representing a
42
+ * geographical extent. Please use a geographic projection.
43
+ */
44
+ class Extent {
45
+ /**
46
+ * Read-only flag to check if a given object is of type `Extent`.
47
+ */
48
+ readonly isExtent: true;
49
+ /**
50
+ * A default or user-defined CRS (see {@link ProjectionLike}).
51
+ */
52
+ crs: ProjectionLike;
53
+ /**
54
+ * West longitude bound of this extent.
55
+ */
56
+ west: number;
57
+ /**
58
+ * East longitude bound of this extent.
59
+ */
60
+ east: number;
61
+ /**
62
+ * South latitude bound of this extent.
63
+ */
64
+ south: number;
65
+ /**
66
+ * North latitude bound of this extent.
67
+ */
68
+ north: number;
69
+
70
+ /**
71
+ * @param crs - A default or user-defined CRS (see {@link ProjectionLike}).
72
+ * @param west - the `west` value of this extent. Default is 0.
73
+ * @param east - the `east` value of this extent. Default is 0.
74
+ * @param south - the `south` value of this extent. Default is 0.
75
+ * @param north - the `north` value of this extent. Default is 0.
76
+ */
77
+ constructor(crs: ProjectionLike, west = 0, east = 0, south = 0, north = 0) {
78
+ if (CRS.isGeocentric(crs)) {
79
+ throw new Error(
80
+ `Non-compatible geocentric projection ${crs} to build a geographical extent`,
81
+ );
82
+ }
83
+
84
+ this.isExtent = true;
85
+ this.crs = crs;
86
+
87
+ this.west = 0;
88
+ this.east = 0;
89
+ this.south = 0;
90
+ this.north = 0;
91
+
92
+ this.set(west, east, south, north);
93
+ }
94
+
95
+ /**
96
+ * Returns a new extent with the same bounds and crs as this one.
97
+ */
98
+ clone() {
99
+ return new Extent(this.crs, this.west, this.east, this.south, this.north);
100
+ }
101
+
102
+ /**
103
+ * Projects this extent to the specified projection.
104
+ *
105
+ * @param crs - target's projection.
106
+ * @param target - The target to store the projected extent. If this not
107
+ * provided a new extent will be created.
108
+ */
109
+ as(crs: string, target: Extent = new Extent('EPSG:4326')) {
110
+ CRS.isValid(crs);
111
+ if (this.crs != crs) {
112
+ // Compute min/max in x/y by projecting 8 cardinal points,
113
+ // and then taking the min/max of each coordinates.
114
+ const center = this.center(_c);
115
+ cardinals[0].setFromValues(this.west, this.north);
116
+ cardinals[1].setFromValues(center.x, this.north);
117
+ cardinals[2].setFromValues(this.east, this.north);
118
+ cardinals[3].setFromValues(this.east, center.y);
119
+ cardinals[4].setFromValues(this.east, this.south);
120
+ cardinals[5].setFromValues(center.x, this.south);
121
+ cardinals[6].setFromValues(this.west, this.south);
122
+ cardinals[7].setFromValues(this.west, center.y);
123
+
124
+ target.set(Infinity, -Infinity, Infinity, -Infinity);
125
+
126
+ // loop over the coordinates
127
+ for (let i = 0; i < cardinals.length; i++) {
128
+ // convert the coordinate.
129
+ cardinals[i].crs = this.crs;
130
+ cardinals[i].as(crs, _c);
131
+ target.north = Math.max(target.north, _c.y);
132
+ target.south = Math.min(target.south, _c.y);
133
+ target.east = Math.max(target.east, _c.x);
134
+ target.west = Math.min(target.west, _c.x);
135
+ }
136
+
137
+ target.crs = crs;
138
+ return target;
139
+ }
140
+
141
+ target.crs = crs;
142
+ target.set(this.west, this.east, this.south, this.north);
143
+
144
+ return target;
145
+ }
146
+
147
+ /**
148
+ * Returns the center of the extent.
149
+ *
150
+ * @param target - The target to store the center coordinate. If this not
151
+ * provided a new coordinate will be created.
152
+ */
153
+ center(target = new Coordinates(this.crs)) {
154
+ this.planarDimensions(_dim);
155
+
156
+ target.crs = this.crs;
157
+ target.setFromValues(this.west + _dim.x * 0.5, this.south + _dim.y * 0.5);
158
+
159
+ return target;
160
+ }
161
+
162
+ /**
163
+ * Returns the planar dimensions as two-vector planar distances west/east
164
+ * and south/north.
165
+ * The planar distance is a straight-line Euclidean distance calculated in a
166
+ * 2D Cartesian coordinate system.
167
+ *
168
+ * @param target - optional target
169
+ */
170
+ planarDimensions(target = new THREE.Vector2()) {
171
+ // Calculte the dimensions for x and y
172
+ return target.set(Math.abs(this.east - this.west), Math.abs(this.north - this.south));
173
+ }
174
+
175
+ /**
176
+ * Returns the geodetic dimensions as two-vector planar distances west/east
177
+ * and south/north.
178
+ * Geodetic distance is calculated in an ellispoid space as the distance
179
+ * across the curved surface of the ellipsoid.
180
+ *
181
+ * @param target - optional target
182
+ */
183
+ geodeticDimensions(target = new THREE.Vector2()) {
184
+ // set 3 corners extent
185
+ cNorthWest.crs = this.crs;
186
+ cSouthWest.crs = this.crs;
187
+ cNorthEast.crs = this.crs;
188
+
189
+ cNorthWest.setFromValues(this.west, this.north, 0);
190
+ cSouthWest.setFromValues(this.west, this.south, 0);
191
+ cNorthEast.setFromValues(this.east, this.north, 0);
192
+
193
+ // calcul geodetic distance northWest/northEast and northWest/southWest
194
+ return target.set(
195
+ cNorthWest.geodeticDistanceTo(cNorthEast),
196
+ cNorthWest.geodeticDistanceTo(cSouthWest),
197
+ );
198
+ }
199
+
200
+ /**
201
+ * Returns the spatial euclidean dimensions as a two-vector spatial
202
+ * euclidean distances between west/east corner and south/north corner.
203
+ * Spatial euclidean distance chord is calculated in an ellispoid space.
204
+ *
205
+ * @param target - optional target
206
+ */
207
+ spatialEuclideanDimensions(target = new THREE.Vector2()) {
208
+ // set 3 corners extent
209
+ cNorthWest.crs = this.crs;
210
+ cSouthWest.crs = this.crs;
211
+ cNorthEast.crs = this.crs;
212
+
213
+ cNorthWest.setFromValues(this.west, this.north, 0);
214
+ cSouthWest.setFromValues(this.west, this.south, 0);
215
+ cNorthEast.setFromValues(this.east, this.north, 0);
216
+
217
+ // calcul chord distance northWest/northEast and northWest/southWest
218
+ return target.set(
219
+ cNorthWest.spatialEuclideanDistanceTo(cNorthEast),
220
+ cNorthWest.spatialEuclideanDistanceTo(cSouthWest),
221
+ );
222
+ }
223
+
224
+ /**
225
+ * Checks whether a coordinates is inside the extent.
226
+ *
227
+ * @param coord - the given coordinates.
228
+ * @param epsilon - error margin when comparing to the coordinates.
229
+ * Default is 0.
230
+ */
231
+ isPointInside(coord: Coordinates, epsilon = 0) {
232
+ if (this.crs == coord.crs) {
233
+ _c.copy(coord);
234
+ } else {
235
+ coord.as(this.crs, _c);
236
+ }
237
+
238
+ // TODO this ignores altitude
239
+ return _c.x <= this.east + epsilon &&
240
+ _c.x >= this.west - epsilon &&
241
+ _c.y <= this.north + epsilon &&
242
+ _c.y >= this.south - epsilon;
243
+ }
244
+
245
+ /**
246
+ * Checks whether another extent is inside the extent.
247
+ *
248
+ * @param extent - the extent to check
249
+ * @param epsilon - error margin when comparing the extent bounds.
250
+ */
251
+ isInside(extent: Extent, epsilon = CRS.reasonableEpsilon(this.crs)) {
252
+ extent.as(this.crs, _extent);
253
+ return this.east - _extent.east <= epsilon &&
254
+ _extent.west - this.west <= epsilon &&
255
+ this.north - _extent.north <= epsilon &&
256
+ _extent.south - this.south <= epsilon;
257
+ }
258
+
259
+ /**
260
+ * Return the translation and scale to transform this extent to the input
261
+ * extent.
262
+ *
263
+ * @param extent - input extent
264
+ * @param target - copy the result to target.
265
+ * @returns A {@link THREE.Vector4} where the `x` property encodes the
266
+ * translation on west-east, the `y` property the translation on
267
+ * south-north, the `z` property the scale on west-east, the `w` property
268
+ * the scale on south-north.
269
+ */
270
+ offsetToParent(extent: Extent, target = new THREE.Vector4()) {
271
+ if (this.crs != extent.crs) {
272
+ throw new Error('unsupported mix');
273
+ }
274
+
275
+ extent.planarDimensions(_dim);
276
+ this.planarDimensions(_dim2);
277
+
278
+ const originX = (this.west - extent.west) / _dim.x;
279
+ const originY = (extent.north - this.north) / _dim.y;
280
+
281
+ const scaleX = _dim2.x / _dim.x;
282
+ const scaleY = _dim2.y / _dim.y;
283
+
284
+ return target.set(originX, originY, scaleX, scaleY);
285
+ }
286
+
287
+ /**
288
+ * Checks wheter this bounding box intersects with the given extent
289
+ * parameter.
290
+ * @param extent - the provided extent
291
+ */
292
+ intersectsExtent(extent: Extent) {
293
+ return Extent.intersectsExtent(this, extent);
294
+ }
295
+
296
+ static intersectsExtent(extentA: Extent, extentB: Extent) {
297
+ // TODO don't work when is on limit
298
+ const other = extentB.crs == extentA.crs ? extentB : extentB.as(extentA.crs, _extent);
299
+ return !(extentA.west >= other.east ||
300
+ extentA.east <= other.west ||
301
+ extentA.south >= other.north ||
302
+ extentA.north <= other.south);
303
+ }
304
+
305
+ /**
306
+ * Returns the intersection of this extent with another one.
307
+ * @param extent - extent to intersect
308
+ */
309
+ intersect(extent: Extent) {
310
+ if (!this.intersectsExtent(extent)) {
311
+ return new Extent(this.crs);
312
+ }
313
+ if (extent.crs != this.crs) {
314
+ extent = extent.as(this.crs, _extent);
315
+ }
316
+ return new Extent(this.crs,
317
+ Math.max(this.west, extent.west),
318
+ Math.min(this.east, extent.east),
319
+ Math.max(this.south, extent.south),
320
+ Math.min(this.north, extent.north));
321
+ }
322
+
323
+ /**
324
+ * Set west, east, south and north values.
325
+ *
326
+ * @param v0 - the `west` value of this extent. Default is 0.
327
+ * @param v1 - the `east` value of this extent. Default is 0.
328
+ * @param v2 - the `south` value of this extent. Default is 0.
329
+ * @param v3 - the `north` value of this extent. Default is 0.
330
+ */
331
+ set(v0: number, v1: number, v2: number, v3: number): this {
332
+ if (v0 == undefined) {
333
+ throw new Error('No values to set in the extent');
334
+ }
335
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
336
+ if ((v0 as any).west !== undefined) {
337
+ console.warn(
338
+ 'Deprecated Extent#constructor(string, Extent) and Extent#set(Extent),',
339
+ 'use new Extent(string).setFromExtent(Extent) instead.',
340
+ );
341
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
342
+ this.setFromExtent(v0 as any);
343
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
344
+ } else if ((v0 as any).length == 4) { // deepscan-disable-line
345
+ console.warn(
346
+ 'Deprecated Extent#constructor(string, number[]) and Extent#set(number[]),',
347
+ 'use new Extent(string).setFromArray(number[]) instead.',
348
+ );
349
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
350
+ this.setFromArray(v0 as any);
351
+ } else if (v3 !== undefined) {
352
+ this.west = v0;
353
+ this.east = v1;
354
+ this.south = v2;
355
+ this.north = v3;
356
+ }
357
+
358
+ return this;
359
+ }
360
+
361
+ /**
362
+ * Sets this extent `west` property to `array[offset + 0]`, `east` property
363
+ * to `array[offset + 1]`, `south` property to `array[offset + 2]` and
364
+ * `north` property to `array[offset + 3]`.
365
+ * @param array - the source array
366
+ * @param offset - offset into the array. Default is 0.
367
+ */
368
+ setFromArray(array: ArrayLike<number>, offset: number = 0): this {
369
+ this.west = array[offset];
370
+ this.east = array[offset + 1];
371
+ this.south = array[offset + 2];
372
+ this.north = array[offset + 3];
373
+ return this;
374
+ }
375
+
376
+ /**
377
+ * Sets this extent `west`, `east`, `south` and `north` properties from an
378
+ * `extent` bounds.
379
+ * @param extent - the source extent
380
+ */
381
+ setFromExtent(extent: ExtentLike): this {
382
+ this.west = extent.west;
383
+ this.east = extent.east;
384
+ this.south = extent.south;
385
+ this.north = extent.north;
386
+ return this;
387
+ }
388
+
389
+ /**
390
+ * Copies the passed extent to this extent.
391
+ * @param extent - extent to copy.
392
+ */
393
+ copy(extent: Extent): this {
394
+ this.crs = extent.crs;
395
+ return this.setFromExtent(extent);
396
+ }
397
+
398
+ /**
399
+ * Union this extent with the input extent.
400
+ * @param extent - the extent to union.
401
+ */
402
+ union(extent: Extent) {
403
+ if (extent.crs != this.crs) {
404
+ throw new Error('unsupported union between 2 diff crs');
405
+ }
406
+ if (this.west === Infinity) {
407
+ this.copy(extent);
408
+ } else {
409
+ const west = extent.west;
410
+ if (west < this.west) {
411
+ this.west = west;
412
+ }
413
+
414
+ const east = extent.east;
415
+ if (east > this.east) {
416
+ this.east = east;
417
+ }
418
+
419
+ const south = extent.south;
420
+ if (south < this.south) {
421
+ this.south = south;
422
+ }
423
+
424
+ const north = extent.north;
425
+ if (north > this.north) {
426
+ this.north = north;
427
+ }
428
+ }
429
+ }
430
+
431
+ /**
432
+ * expandByCoordinates perfoms the minimal extension
433
+ * for the coordinates to belong to this Extent object
434
+ * @param coordinates - The coordinates to belong
435
+ */
436
+ expandByCoordinates(coordinates: Coordinates) {
437
+ const coords = coordinates.crs == this.crs ? coordinates : coordinates.as(this.crs, _c);
438
+ this.expandByValuesCoordinates(coords.x, coords.y);
439
+ }
440
+
441
+ /**
442
+ * expandByValuesCoordinates perfoms the minimal extension
443
+ * for the coordinates values to belong to this Extent object
444
+ * @param we - The coordinate on west-east
445
+ * @param sn - The coordinate on south-north
446
+ *
447
+ */
448
+ expandByValuesCoordinates(we: number, sn: number) {
449
+ if (we < this.west) {
450
+ this.west = we;
451
+ }
452
+ if (we > this.east) {
453
+ this.east = we;
454
+ }
455
+ if (sn < this.south) {
456
+ this.south = sn;
457
+ }
458
+ if (sn > this.north) {
459
+ this.north = sn;
460
+ }
461
+ }
462
+
463
+ /**
464
+ * Instance Extent with THREE.Box3.
465
+ *
466
+ * If crs is a geocentric projection, the `box3.min` and `box3.max`
467
+ * should be the geocentric coordinates of `min` and `max` of a `box3`
468
+ * in local tangent plane.
469
+ *
470
+ * @param crs - Projection of extent to instancied.
471
+ * @param box - Bounding-box
472
+ */
473
+ static fromBox3(crs: ProjectionLike, box: THREE.Box3) {
474
+ if (CRS.isGeocentric(crs)) {
475
+ // if geocentric reproject box on 'EPSG:4326'
476
+ crs = 'EPSG:4326';
477
+ box = _box.copy(box);
478
+
479
+ cSouthWest.crs = crs;
480
+ cSouthWest.setFromVector3(box.min).as(crs, cSouthWest).toVector3(box.min);
481
+ cNorthEast.crs = crs;
482
+ cNorthEast.setFromVector3(box.max).as(crs, cNorthEast).toVector3(box.max);
483
+ }
484
+
485
+ return new Extent(crs).setFromExtent({
486
+ west: box.min.x,
487
+ east: box.max.x,
488
+ south: box.min.y,
489
+ north: box.max.y,
490
+ });
491
+ }
492
+
493
+ /**
494
+ * Return values of extent in string, separated by the separator input.
495
+ * @param sep - string separator
496
+ */
497
+ toString(sep = '') {
498
+ return `${this.east}${sep}${this.north}${sep}${this.west}${sep}${this.south}`;
499
+ }
500
+
501
+ /**
502
+ * Subdivide equally an extent from its center to return four extents:
503
+ * north-west, north-east, south-west and south-east.
504
+ *
505
+ * @returns An array containing the four sections of the extent. The order
506
+ * of the sections is [NW, NE, SW, SE].
507
+ */
508
+ subdivision() {
509
+ return this.subdivisionByScheme();
510
+ }
511
+
512
+ /**
513
+ * subdivise extent by scheme.x on west-east and scheme.y on south-north.
514
+ *
515
+ * @param scheme - The scheme to subdivise.
516
+ * @returns subdivised extents.
517
+ */
518
+ subdivisionByScheme(scheme = defaultScheme): Extent[] {
519
+ const subdivisedExtents = [];
520
+ const dimSub = this.planarDimensions(_dim).divide(scheme);
521
+ for (let x = scheme.x - 1; x >= 0; x--) {
522
+ for (let y = scheme.y - 1; y >= 0; y--) {
523
+ const west = this.west + x * dimSub.x;
524
+ const south = this.south + y * dimSub.y;
525
+ subdivisedExtents.push(new Extent(this.crs,
526
+ west,
527
+ west + dimSub.x,
528
+ south,
529
+ south + dimSub.y));
530
+ }
531
+ }
532
+ return subdivisedExtents;
533
+ }
534
+
535
+ /**
536
+ * Multiplies all extent `coordinates` (with an implicit 1 in the 4th
537
+ * dimension) and `matrix`.
538
+ *
539
+ * @param matrix - The matrix
540
+ * @returns return this extent instance.
541
+ */
542
+ applyMatrix4(matrix: THREE.Matrix4): this {
543
+ southWest.set(this.west, this.south, 0).applyMatrix4(matrix);
544
+ northEast.set(this.east, this.north, 0).applyMatrix4(matrix);
545
+ this.west = southWest.x;
546
+ this.east = northEast.x;
547
+ this.south = southWest.y;
548
+ this.north = northEast.y;
549
+ if (this.west > this.east) {
550
+ const temp = this.west;
551
+ this.west = this.east;
552
+ this.east = temp;
553
+ }
554
+ if (this.south > this.north) {
555
+ const temp = this.south;
556
+ this.south = this.north;
557
+ this.north = temp;
558
+ }
559
+ return this;
560
+ }
561
+
562
+ /**
563
+ * clamp south and north values
564
+ *
565
+ * @param south - The min south
566
+ * @param north - The max north
567
+ * @returns this extent
568
+ */
569
+ clampSouthNorth(south = this.south, north = this.north): this {
570
+ this.south = Math.max(this.south, south);
571
+ this.north = Math.min(this.north, north);
572
+ return this;
573
+ }
574
+
575
+ /**
576
+ * clamp west and east values
577
+ *
578
+ * @param west - The min west
579
+ * @param east - The max east
580
+ * @returns this extent
581
+ */
582
+ clampWestEast(west = this.west, east = this.east): this {
583
+ this.west = Math.max(this.west, west);
584
+ this.east = Math.min(this.east, east);
585
+ return this;
586
+ }
587
+
588
+ /**
589
+ * clamp this extent by passed extent
590
+ *
591
+ * @param extent - The maximum extent.
592
+ * @returns this extent.
593
+ */
594
+ clampByExtent(extent: ExtentLike): this {
595
+ this.clampSouthNorth(extent.south, extent.north);
596
+ return this.clampWestEast(extent.west, extent.east);
597
+ }
598
+ }
599
+
600
+ _extent = new Extent('EPSG:4326');
601
+
602
+ export default Extent;
package/src/Main.js ADDED
@@ -0,0 +1,8 @@
1
+ // Geodesic tools
2
+ export { default as Extent } from 'Extent';
3
+ export { default as Coordinates } from 'Coordinates';
4
+ export * as CRS from 'Crs';
5
+ export { default as CoordStars } from 'CoordStars';
6
+ export { default as OrientationUtils } from 'OrientationUtils';
7
+ export { default as Ellipsoid, ellipsoidSizes } from 'Ellipsoid';
8
+