@math.gl/web-mercator 3.6.0-alpha.1 → 3.6.0-alpha.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.
Files changed (59) hide show
  1. package/dist/es5/fit-bounds.js +37 -31
  2. package/dist/es5/fit-bounds.js.map +1 -1
  3. package/dist/es5/fly-to-viewport.js +98 -75
  4. package/dist/es5/fly-to-viewport.js.map +1 -1
  5. package/dist/es5/get-bounds.js +25 -26
  6. package/dist/es5/get-bounds.js.map +1 -1
  7. package/dist/es5/index.js +31 -23
  8. package/dist/es5/index.js.map +1 -1
  9. package/dist/es5/math-utils.js +8 -3
  10. package/dist/es5/math-utils.js.map +1 -1
  11. package/dist/es5/normalize-viewport-props.js +21 -23
  12. package/dist/es5/normalize-viewport-props.js.map +1 -1
  13. package/dist/es5/web-mercator-utils.js +118 -92
  14. package/dist/es5/web-mercator-utils.js.map +1 -1
  15. package/dist/es5/web-mercator-viewport.js +239 -178
  16. package/dist/es5/web-mercator-viewport.js.map +1 -1
  17. package/dist/esm/fit-bounds.js +6 -13
  18. package/dist/esm/fit-bounds.js.map +1 -1
  19. package/dist/esm/fly-to-viewport.js.map +1 -1
  20. package/dist/esm/get-bounds.js +1 -1
  21. package/dist/esm/get-bounds.js.map +1 -1
  22. package/dist/esm/index.js +1 -1
  23. package/dist/esm/index.js.map +1 -1
  24. package/dist/esm/math-utils.js +3 -0
  25. package/dist/esm/math-utils.js.map +1 -1
  26. package/dist/esm/normalize-viewport-props.js.map +1 -1
  27. package/dist/esm/web-mercator-utils.js +2 -1
  28. package/dist/esm/web-mercator-utils.js.map +1 -1
  29. package/dist/esm/web-mercator-viewport.js +1 -1
  30. package/dist/esm/web-mercator-viewport.js.map +1 -1
  31. package/dist/fit-bounds.d.ts +3 -2
  32. package/dist/fit-bounds.d.ts.map +1 -1
  33. package/dist/fit-bounds.js +6 -13
  34. package/dist/fly-to-viewport.d.ts +6 -6
  35. package/dist/fly-to-viewport.d.ts.map +1 -1
  36. package/dist/get-bounds.js +1 -1
  37. package/dist/index.d.ts +4 -1
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +1 -1
  40. package/dist/math-utils.d.ts +2 -1
  41. package/dist/math-utils.d.ts.map +1 -1
  42. package/dist/math-utils.js +3 -0
  43. package/dist/normalize-viewport-props.d.ts +1 -2
  44. package/dist/normalize-viewport-props.d.ts.map +1 -1
  45. package/dist/web-mercator-utils.d.ts +4 -3
  46. package/dist/web-mercator-utils.d.ts.map +1 -1
  47. package/dist/web-mercator-utils.js +3 -2
  48. package/dist/web-mercator-viewport.d.ts +4 -5
  49. package/dist/web-mercator-viewport.d.ts.map +1 -1
  50. package/dist/web-mercator-viewport.js +1 -2
  51. package/package.json +3 -3
  52. package/src/fit-bounds.ts +8 -16
  53. package/src/fly-to-viewport.ts +26 -11
  54. package/src/get-bounds.ts +4 -4
  55. package/src/index.ts +5 -0
  56. package/src/math-utils.ts +6 -2
  57. package/src/normalize-viewport-props.ts +1 -1
  58. package/src/web-mercator-utils.ts +9 -8
  59. package/src/web-mercator-viewport.ts +8 -10
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@ export {default as normalizeViewportProps} from './normalize-viewport-props';
8
8
  export {default as flyToViewport, getFlyToDuration} from './fly-to-viewport';
9
9
 
10
10
  export {
11
+ MAX_LATITUDE,
11
12
  lngLatToWorld,
12
13
  worldToLngLat,
13
14
  worldToPixels,
@@ -23,3 +24,7 @@ export {
23
24
  getProjectionMatrix,
24
25
  getProjectionParameters
25
26
  } from './web-mercator-utils';
27
+
28
+ /** Types */
29
+ export type {FitBoundsOptions} from './fit-bounds';
30
+ export type {DistanceScales} from './web-mercator-utils';
package/src/math-utils.ts CHANGED
@@ -6,8 +6,8 @@ export function createMat4(): number[] {
6
6
  }
7
7
 
8
8
  // Transforms a vec4 with a projection matrix
9
- export function transformVector(matrix, vector) {
10
- const result = transformMat4([], vector, matrix);
9
+ export function transformVector(matrix: number[], vector: number[]): number[] {
10
+ const result = transformMat4([] as number[], vector, matrix);
11
11
  scale(result, result, 1 / result[3]);
12
12
  return result;
13
13
  }
@@ -21,6 +21,10 @@ export function lerp(start: number, end: number, step: number): number {
21
21
  return step * end + (1 - step) * start;
22
22
  }
23
23
 
24
+ export function clamp(x: number, min: number, max: number): number {
25
+ return x < min ? min : x > max ? max : x;
26
+ }
27
+
24
28
  function ieLog2(x: number): number {
25
29
  return Math.log(x) * Math.LOG2E;
26
30
  }
@@ -5,7 +5,7 @@ import {mod, log2} from './math-utils';
5
5
  const TILE_SIZE = 512;
6
6
 
7
7
  /** Description of viewport */
8
- type ViewportProps = {
8
+ export type ViewportProps = {
9
9
  width: number;
10
10
  height: number;
11
11
  longitude: number;
@@ -15,6 +15,8 @@ const RADIANS_TO_DEGREES = 180 / PI;
15
15
  const TILE_SIZE = 512;
16
16
  // Average circumference (40075 km equatorial, 40007 km meridional)
17
17
  const EARTH_CIRCUMFERENCE = 40.03e6;
18
+ // Latitude that makes a square world, 2 * atan(E ** PI) - PI / 2
19
+ export const MAX_LATITUDE = 85.051129;
18
20
 
19
21
  // Mapbox default altitude
20
22
  export const DEFAULT_ALTITUDE = 1.5;
@@ -66,7 +68,7 @@ export function scaleToZoom(scale: number): number {
66
68
  * Specifies a point on the sphere to project onto the map.
67
69
  * @return [x,y] coordinates.
68
70
  */
69
- export function lngLatToWorld(lngLat: number[]): number[] {
71
+ export function lngLatToWorld(lngLat: number[]): [number, number] {
70
72
  const [lng, lat] = lngLat;
71
73
  assert(Number.isFinite(lng));
72
74
  assert(Number.isFinite(lat) && lat >= -90 && lat <= 90, 'invalid latitude');
@@ -87,7 +89,7 @@ export function lngLatToWorld(lngLat: number[]): number[] {
87
89
  * Has toArray method if you need a GeoJSON Array.
88
90
  * Per cartographic tradition, lat and lon are specified as degrees.
89
91
  */
90
- export function worldToLngLat(xy: number[]): number[] {
92
+ export function worldToLngLat(xy: number[]): [number, number] {
91
93
  const [x, y] = xy;
92
94
  const lambda2 = (x / TILE_SIZE) * (2 * PI) - PI;
93
95
  const phi2 = 2 * (Math.atan(Math.exp((y / TILE_SIZE) * (2 * PI) - PI)) - PI_4);
@@ -189,7 +191,6 @@ export function addMetersToLngLat(lngLatZ: number[], xyz: number[]): number[] {
189
191
  worldspace[0] += x * (unitsPerMeter[0] + unitsPerMeter2[0] * y);
190
192
  worldspace[1] += y * (unitsPerMeter[1] + unitsPerMeter2[1] * y);
191
193
 
192
- // @ts-ignore
193
194
  const newLngLat = worldToLngLat(worldspace);
194
195
  const newZ = (z0 || 0) + (z || 0);
195
196
 
@@ -210,7 +211,7 @@ export function getViewMatrix(options: {
210
211
  altitude: number;
211
212
  // Pre-calculated parameters
212
213
  scale: number;
213
- center: number[];
214
+ center?: number[];
214
215
  }): number[] {
215
216
  const {
216
217
  // Viewport props
@@ -220,7 +221,7 @@ export function getViewMatrix(options: {
220
221
  altitude,
221
222
  // Pre-calculated parameters
222
223
  scale,
223
- center = null
224
+ center
224
225
  } = options;
225
226
  // VIEW MATRIX: PROJECTS MERCATOR WORLD COORDINATES
226
227
  // Note that mercator world coordinates typically need to be flipped
@@ -326,7 +327,7 @@ export function getProjectionMatrix(options: {
326
327
  const {fov, aspect, near, far} = getProjectionParameters(options);
327
328
 
328
329
  const projectionMatrix = mat4.perspective(
329
- [],
330
+ [] as number[],
330
331
  fov, // fov in radians
331
332
  aspect, // aspect ratio
332
333
  near, // near plane
@@ -370,7 +371,7 @@ export function fovyToAltitude(fovy: number): number {
370
371
  export function worldToPixels(xyz: number[], pixelProjectionMatrix: number[]): number[];
371
372
 
372
373
  // Project flat coordinates to pixels on screen.
373
- export function worldToPixels(xyz, pixelProjectionMatrix) {
374
+ export function worldToPixels(xyz: number[], pixelProjectionMatrix: number[]): number[] {
374
375
  const [x, y, z = 0] = xyz;
375
376
  assert(Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(z));
376
377
 
@@ -409,5 +410,5 @@ export function pixelsToWorld(
409
410
  const z1 = coord1[2];
410
411
 
411
412
  const t = z0 === z1 ? 0 : ((targetZ || 0) - z0) / (z1 - z0);
412
- return vec2.lerp([], coord0, coord1, t);
413
+ return vec2.lerp([] as number[], coord0, coord1, t);
413
414
  }
@@ -12,11 +12,12 @@ import {
12
12
  DEFAULT_ALTITUDE,
13
13
  getProjectionMatrix,
14
14
  getDistanceScales,
15
- getViewMatrix
15
+ getViewMatrix,
16
+ DistanceScales
16
17
  } from './web-mercator-utils';
17
18
  import fitBounds from './fit-bounds';
18
19
  import getBounds from './get-bounds';
19
- import type {Bounds, FitBoundsOptions} from './fit-bounds';
20
+ import type {FitBoundsOptions} from './fit-bounds';
20
21
 
21
22
  import * as mat4 from 'gl-matrix/mat4';
22
23
  import * as vec2 from 'gl-matrix/vec2';
@@ -84,9 +85,7 @@ export default class WebMercatorViewport {
84
85
  readonly width: number;
85
86
  readonly height: number;
86
87
  readonly scale: number;
87
- readonly distanceScales: {
88
- unitsPerMeter: number[];
89
- };
88
+ readonly distanceScales: DistanceScales;
90
89
 
91
90
  readonly viewMatrix: number[];
92
91
  readonly projectionMatrix: number[];
@@ -145,7 +144,7 @@ export default class WebMercatorViewport {
145
144
  const distanceScales = getDistanceScales({longitude, latitude});
146
145
 
147
146
  const center = lngLatToWorld([longitude, latitude]);
148
- center[2] = 0;
147
+ center.push(0);
149
148
 
150
149
  if (position) {
151
150
  vec3.add(center, center, vec3.mul([], position, distanceScales.unitsPerMeter));
@@ -198,7 +197,7 @@ export default class WebMercatorViewport {
198
197
  Object.freeze(this);
199
198
  }
200
199
 
201
- _initMatrices() {
200
+ _initMatrices(): void {
202
201
  const {width, height, projectionMatrix, viewMatrix} = this;
203
202
 
204
203
  // Note: As usual, matrix operations should be applied in "reverse" order
@@ -303,15 +302,14 @@ export default class WebMercatorViewport {
303
302
  // NON_LINEAR PROJECTION HOOKS
304
303
  // Used for web meractor projection
305
304
 
306
- projectPosition(xyz) {
305
+ projectPosition(xyz: number[]): [number, number, number] {
307
306
  const [X, Y] = lngLatToWorld(xyz);
308
307
  const Z = (xyz[2] || 0) * this.distanceScales.unitsPerMeter[2];
309
308
  return [X, Y, Z];
310
309
  }
311
310
 
312
- unprojectPosition(xyz) {
311
+ unprojectPosition(xyz: number[]): [number, number, number] {
313
312
  const [X, Y] = worldToLngLat(xyz);
314
- // @ts-expect-error
315
313
  const Z = (xyz[2] || 0) * this.distanceScales.metersPerUnit[2];
316
314
  return [X, Y, Z];
317
315
  }