@math.gl/web-mercator 3.6.2 → 4.0.0-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.
Files changed (62) hide show
  1. package/dist/{esm/assert.js → assert.js} +0 -0
  2. package/dist/assert.js.map +1 -0
  3. package/dist/fit-bounds.d.ts +3 -3
  4. package/dist/fit-bounds.d.ts.map +1 -1
  5. package/dist/{esm/fit-bounds.js → fit-bounds.js} +4 -4
  6. package/dist/fit-bounds.js.map +1 -0
  7. package/dist/fly-to-viewport.d.ts +1 -1
  8. package/dist/fly-to-viewport.d.ts.map +1 -1
  9. package/dist/{esm/fly-to-viewport.js → fly-to-viewport.js} +2 -2
  10. package/dist/fly-to-viewport.js.map +1 -0
  11. package/dist/get-bounds.d.ts.map +1 -1
  12. package/dist/{esm/get-bounds.js → get-bounds.js} +4 -4
  13. package/dist/get-bounds.js.map +1 -0
  14. package/dist/index.cjs +630 -0
  15. package/dist/index.js +8 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/{esm/math-utils.js → math-utils.js} +0 -0
  18. package/dist/math-utils.js.map +1 -0
  19. package/dist/normalize-viewport-props.d.ts +1 -1
  20. package/dist/normalize-viewport-props.d.ts.map +1 -1
  21. package/dist/{esm/normalize-viewport-props.js → normalize-viewport-props.js} +2 -2
  22. package/dist/normalize-viewport-props.js.map +1 -0
  23. package/dist/web-mercator-utils.d.ts +2 -2
  24. package/dist/web-mercator-utils.d.ts.map +1 -1
  25. package/dist/{esm/web-mercator-utils.js → web-mercator-utils.js} +2 -2
  26. package/dist/web-mercator-utils.js.map +1 -0
  27. package/dist/web-mercator-viewport.d.ts +1 -1
  28. package/dist/web-mercator-viewport.d.ts.map +1 -1
  29. package/dist/{esm/web-mercator-viewport.js → web-mercator-viewport.js} +4 -4
  30. package/dist/web-mercator-viewport.js.map +1 -0
  31. package/package.json +6 -5
  32. package/src/fit-bounds.ts +1 -1
  33. package/src/fly-to-viewport.ts +1 -0
  34. package/src/get-bounds.ts +4 -3
  35. package/dist/es5/assert.js +0 -13
  36. package/dist/es5/assert.js.map +0 -1
  37. package/dist/es5/fit-bounds.js +0 -73
  38. package/dist/es5/fit-bounds.js.map +0 -1
  39. package/dist/es5/fly-to-viewport.js +0 -151
  40. package/dist/es5/fly-to-viewport.js.map +0 -1
  41. package/dist/es5/get-bounds.js +0 -59
  42. package/dist/es5/get-bounds.js.map +0 -1
  43. package/dist/es5/index.js +0 -164
  44. package/dist/es5/index.js.map +0 -1
  45. package/dist/es5/math-utils.js +0 -44
  46. package/dist/es5/math-utils.js.map +0 -1
  47. package/dist/es5/normalize-viewport-props.js +0 -63
  48. package/dist/es5/normalize-viewport-props.js.map +0 -1
  49. package/dist/es5/web-mercator-utils.js +0 -271
  50. package/dist/es5/web-mercator-utils.js.map +0 -1
  51. package/dist/es5/web-mercator-viewport.js +0 -328
  52. package/dist/es5/web-mercator-viewport.js.map +0 -1
  53. package/dist/esm/assert.js.map +0 -1
  54. package/dist/esm/fit-bounds.js.map +0 -1
  55. package/dist/esm/fly-to-viewport.js.map +0 -1
  56. package/dist/esm/get-bounds.js.map +0 -1
  57. package/dist/esm/index.js +0 -8
  58. package/dist/esm/index.js.map +0 -1
  59. package/dist/esm/math-utils.js.map +0 -1
  60. package/dist/esm/normalize-viewport-props.js.map +0 -1
  61. package/dist/esm/web-mercator-utils.js.map +0 -1
  62. package/dist/esm/web-mercator-viewport.js.map +0 -1
package/dist/index.cjs ADDED
@@ -0,0 +1,630 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __spreadValues = (a, b) => {
11
+ for (var prop in b || (b = {}))
12
+ if (__hasOwnProp.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ if (__getOwnPropSymbols)
15
+ for (var prop of __getOwnPropSymbols(b)) {
16
+ if (__propIsEnum.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ }
19
+ return a;
20
+ };
21
+ var __export = (target, all) => {
22
+ for (var name in all)
23
+ __defProp(target, name, { get: all[name], enumerable: true });
24
+ };
25
+ var __copyProps = (to, from, except, desc) => {
26
+ if (from && typeof from === "object" || typeof from === "function") {
27
+ for (let key of __getOwnPropNames(from))
28
+ if (!__hasOwnProp.call(to, key) && key !== except)
29
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
30
+ }
31
+ return to;
32
+ };
33
+ var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__getProtoOf(mod2)) : {}, __copyProps(
34
+ isNodeMode || !mod2 || !mod2.__esModule ? __defProp(target, "default", { value: mod2, enumerable: true }) : target,
35
+ mod2
36
+ ));
37
+ var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
38
+
39
+ // src/index.ts
40
+ var src_exports = {};
41
+ __export(src_exports, {
42
+ MAX_LATITUDE: () => MAX_LATITUDE,
43
+ WebMercatorViewport: () => WebMercatorViewport,
44
+ addMetersToLngLat: () => addMetersToLngLat,
45
+ altitudeToFovy: () => altitudeToFovy,
46
+ default: () => WebMercatorViewport,
47
+ fitBounds: () => fitBounds,
48
+ flyToViewport: () => flyToViewport,
49
+ fovyToAltitude: () => fovyToAltitude,
50
+ getBounds: () => getBounds,
51
+ getDistanceScales: () => getDistanceScales,
52
+ getFlyToDuration: () => getFlyToDuration,
53
+ getMeterZoom: () => getMeterZoom,
54
+ getProjectionMatrix: () => getProjectionMatrix,
55
+ getProjectionParameters: () => getProjectionParameters,
56
+ getViewMatrix: () => getViewMatrix,
57
+ lngLatToWorld: () => lngLatToWorld,
58
+ normalizeViewportProps: () => normalizeViewportProps,
59
+ pixelsToWorld: () => pixelsToWorld,
60
+ scaleToZoom: () => scaleToZoom,
61
+ unitsPerMeter: () => unitsPerMeter,
62
+ worldToLngLat: () => worldToLngLat,
63
+ worldToPixels: () => worldToPixels,
64
+ zoomToScale: () => zoomToScale
65
+ });
66
+ module.exports = __toCommonJS(src_exports);
67
+
68
+ // src/math-utils.ts
69
+ var import_vec4 = require("gl-matrix/vec4");
70
+ function createMat4() {
71
+ return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
72
+ }
73
+ function transformVector(matrix, vector) {
74
+ const result = (0, import_vec4.transformMat4)([], vector, matrix);
75
+ (0, import_vec4.scale)(result, result, 1 / result[3]);
76
+ return result;
77
+ }
78
+ function mod(value, divisor) {
79
+ const modulus = value % divisor;
80
+ return modulus < 0 ? divisor + modulus : modulus;
81
+ }
82
+ function lerp(start, end, step) {
83
+ return step * end + (1 - step) * start;
84
+ }
85
+ function clamp(x, min, max) {
86
+ return x < min ? min : x > max ? max : x;
87
+ }
88
+ function ieLog2(x) {
89
+ return Math.log(x) * Math.LOG2E;
90
+ }
91
+ var log2 = Math.log2 || ieLog2;
92
+
93
+ // src/web-mercator-utils.ts
94
+ var mat4 = __toESM(require("gl-matrix/mat4"), 1);
95
+ var vec2 = __toESM(require("gl-matrix/vec2"), 1);
96
+ var vec3 = __toESM(require("gl-matrix/vec3"), 1);
97
+
98
+ // src/assert.ts
99
+ function assert(condition, message) {
100
+ if (!condition) {
101
+ throw new Error(message || "@math.gl/web-mercator: assertion failed.");
102
+ }
103
+ }
104
+
105
+ // src/web-mercator-utils.ts
106
+ var PI = Math.PI;
107
+ var PI_4 = PI / 4;
108
+ var DEGREES_TO_RADIANS = PI / 180;
109
+ var RADIANS_TO_DEGREES = 180 / PI;
110
+ var TILE_SIZE = 512;
111
+ var EARTH_CIRCUMFERENCE = 4003e4;
112
+ var MAX_LATITUDE = 85.051129;
113
+ var DEFAULT_ALTITUDE = 1.5;
114
+ function zoomToScale(zoom) {
115
+ return Math.pow(2, zoom);
116
+ }
117
+ function scaleToZoom(scale5) {
118
+ return log2(scale5);
119
+ }
120
+ function lngLatToWorld(lngLat) {
121
+ const [lng, lat] = lngLat;
122
+ assert(Number.isFinite(lng));
123
+ assert(Number.isFinite(lat) && lat >= -90 && lat <= 90, "invalid latitude");
124
+ const lambda2 = lng * DEGREES_TO_RADIANS;
125
+ const phi2 = lat * DEGREES_TO_RADIANS;
126
+ const x = TILE_SIZE * (lambda2 + PI) / (2 * PI);
127
+ const y = TILE_SIZE * (PI + Math.log(Math.tan(PI_4 + phi2 * 0.5))) / (2 * PI);
128
+ return [x, y];
129
+ }
130
+ function worldToLngLat(xy) {
131
+ const [x, y] = xy;
132
+ const lambda2 = x / TILE_SIZE * (2 * PI) - PI;
133
+ const phi2 = 2 * (Math.atan(Math.exp(y / TILE_SIZE * (2 * PI) - PI)) - PI_4);
134
+ return [lambda2 * RADIANS_TO_DEGREES, phi2 * RADIANS_TO_DEGREES];
135
+ }
136
+ function getMeterZoom(options) {
137
+ const { latitude } = options;
138
+ assert(Number.isFinite(latitude));
139
+ const latCosine = Math.cos(latitude * DEGREES_TO_RADIANS);
140
+ return scaleToZoom(EARTH_CIRCUMFERENCE * latCosine) - 9;
141
+ }
142
+ function unitsPerMeter(latitude) {
143
+ const latCosine = Math.cos(latitude * DEGREES_TO_RADIANS);
144
+ return TILE_SIZE / EARTH_CIRCUMFERENCE / latCosine;
145
+ }
146
+ function getDistanceScales(options) {
147
+ const { latitude, longitude, highPrecision = false } = options;
148
+ assert(Number.isFinite(latitude) && Number.isFinite(longitude));
149
+ const worldSize = TILE_SIZE;
150
+ const latCosine = Math.cos(latitude * DEGREES_TO_RADIANS);
151
+ const unitsPerDegreeX = worldSize / 360;
152
+ const unitsPerDegreeY = unitsPerDegreeX / latCosine;
153
+ const altUnitsPerMeter = worldSize / EARTH_CIRCUMFERENCE / latCosine;
154
+ const result = {
155
+ unitsPerMeter: [altUnitsPerMeter, altUnitsPerMeter, altUnitsPerMeter],
156
+ metersPerUnit: [1 / altUnitsPerMeter, 1 / altUnitsPerMeter, 1 / altUnitsPerMeter],
157
+ unitsPerDegree: [unitsPerDegreeX, unitsPerDegreeY, altUnitsPerMeter],
158
+ degreesPerUnit: [1 / unitsPerDegreeX, 1 / unitsPerDegreeY, 1 / altUnitsPerMeter]
159
+ };
160
+ if (highPrecision) {
161
+ const latCosine2 = DEGREES_TO_RADIANS * Math.tan(latitude * DEGREES_TO_RADIANS) / latCosine;
162
+ const unitsPerDegreeY2 = unitsPerDegreeX * latCosine2 / 2;
163
+ const altUnitsPerDegree2 = worldSize / EARTH_CIRCUMFERENCE * latCosine2;
164
+ const altUnitsPerMeter2 = altUnitsPerDegree2 / unitsPerDegreeY * altUnitsPerMeter;
165
+ result.unitsPerDegree2 = [0, unitsPerDegreeY2, altUnitsPerDegree2];
166
+ result.unitsPerMeter2 = [altUnitsPerMeter2, 0, altUnitsPerMeter2];
167
+ }
168
+ return result;
169
+ }
170
+ function addMetersToLngLat(lngLatZ, xyz) {
171
+ const [longitude, latitude, z0] = lngLatZ;
172
+ const [x, y, z] = xyz;
173
+ const { unitsPerMeter: unitsPerMeter2, unitsPerMeter2: unitsPerMeter22 } = getDistanceScales({
174
+ longitude,
175
+ latitude,
176
+ highPrecision: true
177
+ });
178
+ const worldspace = lngLatToWorld(lngLatZ);
179
+ worldspace[0] += x * (unitsPerMeter2[0] + unitsPerMeter22[0] * y);
180
+ worldspace[1] += y * (unitsPerMeter2[1] + unitsPerMeter22[1] * y);
181
+ const newLngLat = worldToLngLat(worldspace);
182
+ const newZ = (z0 || 0) + (z || 0);
183
+ return Number.isFinite(z0) || Number.isFinite(z) ? [newLngLat[0], newLngLat[1], newZ] : newLngLat;
184
+ }
185
+ function getViewMatrix(options) {
186
+ const {
187
+ height,
188
+ pitch,
189
+ bearing,
190
+ altitude,
191
+ scale: scale5,
192
+ center
193
+ } = options;
194
+ const vm = createMat4();
195
+ mat4.translate(vm, vm, [0, 0, -altitude]);
196
+ mat4.rotateX(vm, vm, -pitch * DEGREES_TO_RADIANS);
197
+ mat4.rotateZ(vm, vm, bearing * DEGREES_TO_RADIANS);
198
+ const relativeScale = scale5 / height;
199
+ mat4.scale(vm, vm, [relativeScale, relativeScale, relativeScale]);
200
+ if (center) {
201
+ mat4.translate(vm, vm, vec3.negate([], center));
202
+ }
203
+ return vm;
204
+ }
205
+ function getProjectionParameters(options) {
206
+ const {
207
+ width,
208
+ height,
209
+ altitude,
210
+ pitch = 0,
211
+ offset,
212
+ center,
213
+ scale: scale5,
214
+ nearZMultiplier = 1,
215
+ farZMultiplier = 1
216
+ } = options;
217
+ let { fovy = altitudeToFovy(DEFAULT_ALTITUDE) } = options;
218
+ if (altitude !== void 0) {
219
+ fovy = altitudeToFovy(altitude);
220
+ }
221
+ const fovRadians = fovy * DEGREES_TO_RADIANS;
222
+ const pitchRadians = pitch * DEGREES_TO_RADIANS;
223
+ const focalDistance = fovyToAltitude(fovy);
224
+ let cameraToSeaLevelDistance = focalDistance;
225
+ if (center) {
226
+ cameraToSeaLevelDistance += center[2] * scale5 / Math.cos(pitchRadians) / height;
227
+ }
228
+ const fovAboveCenter = fovRadians * (0.5 + (offset ? offset[1] : 0) / height);
229
+ const topHalfSurfaceDistance = Math.sin(fovAboveCenter) * cameraToSeaLevelDistance / Math.sin(clamp(Math.PI / 2 - pitchRadians - fovAboveCenter, 0.01, Math.PI - 0.01));
230
+ const furthestDistance = Math.sin(pitchRadians) * topHalfSurfaceDistance + cameraToSeaLevelDistance;
231
+ const horizonDistance = cameraToSeaLevelDistance * 10;
232
+ const farZ = Math.min(furthestDistance * farZMultiplier, horizonDistance);
233
+ return {
234
+ fov: fovRadians,
235
+ aspect: width / height,
236
+ focalDistance,
237
+ near: nearZMultiplier,
238
+ far: farZ
239
+ };
240
+ }
241
+ function getProjectionMatrix(options) {
242
+ const { fov, aspect, near, far } = getProjectionParameters(options);
243
+ const projectionMatrix = mat4.perspective(
244
+ [],
245
+ fov,
246
+ aspect,
247
+ near,
248
+ far
249
+ );
250
+ return projectionMatrix;
251
+ }
252
+ function altitudeToFovy(altitude) {
253
+ return 2 * Math.atan(0.5 / altitude) * RADIANS_TO_DEGREES;
254
+ }
255
+ function fovyToAltitude(fovy) {
256
+ return 0.5 / Math.tan(0.5 * fovy * DEGREES_TO_RADIANS);
257
+ }
258
+ function worldToPixels(xyz, pixelProjectionMatrix) {
259
+ const [x, y, z = 0] = xyz;
260
+ assert(Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(z));
261
+ return transformVector(pixelProjectionMatrix, [x, y, z, 1]);
262
+ }
263
+ function pixelsToWorld(xyz, pixelUnprojectionMatrix, targetZ = 0) {
264
+ const [x, y, z] = xyz;
265
+ assert(Number.isFinite(x) && Number.isFinite(y), "invalid pixel coordinate");
266
+ if (Number.isFinite(z)) {
267
+ const coord = transformVector(pixelUnprojectionMatrix, [x, y, z, 1]);
268
+ return coord;
269
+ }
270
+ const coord0 = transformVector(pixelUnprojectionMatrix, [x, y, 0, 1]);
271
+ const coord1 = transformVector(pixelUnprojectionMatrix, [x, y, 1, 1]);
272
+ const z0 = coord0[2];
273
+ const z1 = coord1[2];
274
+ const t = z0 === z1 ? 0 : ((targetZ || 0) - z0) / (z1 - z0);
275
+ return vec2.lerp([], coord0, coord1, t);
276
+ }
277
+
278
+ // src/fit-bounds.ts
279
+ function fitBounds(options) {
280
+ const {
281
+ width,
282
+ height,
283
+ bounds,
284
+ minExtent = 0,
285
+ maxZoom = 24,
286
+ offset = [0, 0]
287
+ } = options;
288
+ const [[west, south], [east, north]] = bounds;
289
+ const padding = getPaddingObject(options.padding);
290
+ const nw = lngLatToWorld([west, clamp(north, -MAX_LATITUDE, MAX_LATITUDE)]);
291
+ const se = lngLatToWorld([east, clamp(south, -MAX_LATITUDE, MAX_LATITUDE)]);
292
+ const size = [
293
+ Math.max(Math.abs(se[0] - nw[0]), minExtent),
294
+ Math.max(Math.abs(se[1] - nw[1]), minExtent)
295
+ ];
296
+ const targetSize = [
297
+ width - padding.left - padding.right - Math.abs(offset[0]) * 2,
298
+ height - padding.top - padding.bottom - Math.abs(offset[1]) * 2
299
+ ];
300
+ assert(targetSize[0] > 0 && targetSize[1] > 0);
301
+ const scaleX = targetSize[0] / size[0];
302
+ const scaleY = targetSize[1] / size[1];
303
+ const offsetX = (padding.right - padding.left) / 2 / scaleX;
304
+ const offsetY = (padding.top - padding.bottom) / 2 / scaleY;
305
+ const center = [(se[0] + nw[0]) / 2 + offsetX, (se[1] + nw[1]) / 2 + offsetY];
306
+ const centerLngLat = worldToLngLat(center);
307
+ const zoom = Math.min(maxZoom, log2(Math.abs(Math.min(scaleX, scaleY))));
308
+ assert(Number.isFinite(zoom));
309
+ return {
310
+ longitude: centerLngLat[0],
311
+ latitude: centerLngLat[1],
312
+ zoom
313
+ };
314
+ }
315
+ function getPaddingObject(padding = 0) {
316
+ if (typeof padding === "number") {
317
+ return {
318
+ top: padding,
319
+ bottom: padding,
320
+ left: padding,
321
+ right: padding
322
+ };
323
+ }
324
+ assert(
325
+ Number.isFinite(padding.top) && Number.isFinite(padding.bottom) && Number.isFinite(padding.left) && Number.isFinite(padding.right)
326
+ );
327
+ return padding;
328
+ }
329
+
330
+ // src/get-bounds.ts
331
+ var import_vec2 = require("gl-matrix/vec2");
332
+ var DEGREES_TO_RADIANS2 = Math.PI / 180;
333
+ function getBounds(viewport, z = 0) {
334
+ const { width, height, unproject } = viewport;
335
+ const unprojectOps = { targetZ: z };
336
+ const bottomLeft = unproject([0, height], unprojectOps);
337
+ const bottomRight = unproject([width, height], unprojectOps);
338
+ let topLeft;
339
+ let topRight;
340
+ const halfFov = viewport.fovy ? 0.5 * viewport.fovy * DEGREES_TO_RADIANS2 : Math.atan(0.5 / viewport.altitude);
341
+ const angleToGround = (90 - viewport.pitch) * DEGREES_TO_RADIANS2;
342
+ if (halfFov > angleToGround - 0.01) {
343
+ topLeft = unprojectOnFarPlane(viewport, 0, z);
344
+ topRight = unprojectOnFarPlane(viewport, width, z);
345
+ } else {
346
+ topLeft = unproject([0, 0], unprojectOps);
347
+ topRight = unproject([width, 0], unprojectOps);
348
+ }
349
+ return [bottomLeft, bottomRight, topRight, topLeft];
350
+ }
351
+ function unprojectOnFarPlane(viewport, x, targetZ) {
352
+ const { pixelUnprojectionMatrix } = viewport;
353
+ const coord0 = transformVector(pixelUnprojectionMatrix, [x, 0, 1, 1]);
354
+ const coord1 = transformVector(pixelUnprojectionMatrix, [x, viewport.height, 1, 1]);
355
+ const z = targetZ * viewport.distanceScales.unitsPerMeter[2];
356
+ const t = (z - coord0[2]) / (coord1[2] - coord0[2]);
357
+ const coord = (0, import_vec2.lerp)([], coord0, coord1, t);
358
+ const result = worldToLngLat(coord);
359
+ result.push(targetZ);
360
+ return result;
361
+ }
362
+
363
+ // src/web-mercator-viewport.ts
364
+ var mat42 = __toESM(require("gl-matrix/mat4"), 1);
365
+ var vec22 = __toESM(require("gl-matrix/vec2"), 1);
366
+ var vec32 = __toESM(require("gl-matrix/vec3"), 1);
367
+ var WebMercatorViewport = class {
368
+ constructor(props = { width: 1, height: 1 }) {
369
+ this.equals = (viewport) => {
370
+ if (!(viewport instanceof WebMercatorViewport)) {
371
+ return false;
372
+ }
373
+ return viewport.width === this.width && viewport.height === this.height && mat42.equals(viewport.projectionMatrix, this.projectionMatrix) && mat42.equals(viewport.viewMatrix, this.viewMatrix);
374
+ };
375
+ this.project = (lngLatZ, options = {}) => {
376
+ const { topLeft = true } = options;
377
+ const worldPosition = this.projectPosition(lngLatZ);
378
+ const coord = worldToPixels(worldPosition, this.pixelProjectionMatrix);
379
+ const [x, y] = coord;
380
+ const y2 = topLeft ? y : this.height - y;
381
+ return lngLatZ.length === 2 ? [x, y2] : [x, y2, coord[2]];
382
+ };
383
+ this.unproject = (xyz, options = {}) => {
384
+ const { topLeft = true, targetZ = void 0 } = options;
385
+ const [x, y, z] = xyz;
386
+ const y2 = topLeft ? y : this.height - y;
387
+ const targetZWorld = targetZ && targetZ * this.distanceScales.unitsPerMeter[2];
388
+ const coord = pixelsToWorld([x, y2, z], this.pixelUnprojectionMatrix, targetZWorld);
389
+ const [X, Y, Z] = this.unprojectPosition(coord);
390
+ if (Number.isFinite(z)) {
391
+ return [X, Y, Z];
392
+ }
393
+ return Number.isFinite(targetZ) ? [X, Y, targetZ] : [X, Y];
394
+ };
395
+ this.projectPosition = (xyz) => {
396
+ const [X, Y] = lngLatToWorld(xyz);
397
+ const Z = (xyz[2] || 0) * this.distanceScales.unitsPerMeter[2];
398
+ return [X, Y, Z];
399
+ };
400
+ this.unprojectPosition = (xyz) => {
401
+ const [X, Y] = worldToLngLat(xyz);
402
+ const Z = (xyz[2] || 0) * this.distanceScales.metersPerUnit[2];
403
+ return [X, Y, Z];
404
+ };
405
+ let {
406
+ width,
407
+ height,
408
+ altitude = null,
409
+ fovy = null
410
+ } = props;
411
+ const {
412
+ latitude = 0,
413
+ longitude = 0,
414
+ zoom = 0,
415
+ pitch = 0,
416
+ bearing = 0,
417
+ position = null,
418
+ nearZMultiplier = 0.02,
419
+ farZMultiplier = 1.01
420
+ } = props;
421
+ width = width || 1;
422
+ height = height || 1;
423
+ if (fovy === null && altitude === null) {
424
+ altitude = DEFAULT_ALTITUDE;
425
+ fovy = altitudeToFovy(altitude);
426
+ } else if (fovy === null) {
427
+ fovy = altitudeToFovy(altitude);
428
+ } else if (altitude === null) {
429
+ altitude = fovyToAltitude(fovy);
430
+ }
431
+ const scale5 = zoomToScale(zoom);
432
+ altitude = Math.max(0.75, altitude);
433
+ const distanceScales = getDistanceScales({ longitude, latitude });
434
+ const center = lngLatToWorld([longitude, latitude]);
435
+ center.push(0);
436
+ if (position) {
437
+ vec32.add(center, center, vec32.mul([], position, distanceScales.unitsPerMeter));
438
+ }
439
+ this.projectionMatrix = getProjectionMatrix({
440
+ width,
441
+ height,
442
+ scale: scale5,
443
+ center,
444
+ pitch,
445
+ fovy,
446
+ nearZMultiplier,
447
+ farZMultiplier
448
+ });
449
+ this.viewMatrix = getViewMatrix({
450
+ height,
451
+ scale: scale5,
452
+ center,
453
+ pitch,
454
+ bearing,
455
+ altitude
456
+ });
457
+ this.width = width;
458
+ this.height = height;
459
+ this.scale = scale5;
460
+ this.latitude = latitude;
461
+ this.longitude = longitude;
462
+ this.zoom = zoom;
463
+ this.pitch = pitch;
464
+ this.bearing = bearing;
465
+ this.altitude = altitude;
466
+ this.fovy = fovy;
467
+ this.center = center;
468
+ this.meterOffset = position || [0, 0, 0];
469
+ this.distanceScales = distanceScales;
470
+ this._initMatrices();
471
+ Object.freeze(this);
472
+ }
473
+ _initMatrices() {
474
+ const { width, height, projectionMatrix, viewMatrix } = this;
475
+ const vpm = createMat4();
476
+ mat42.multiply(vpm, vpm, projectionMatrix);
477
+ mat42.multiply(vpm, vpm, viewMatrix);
478
+ this.viewProjectionMatrix = vpm;
479
+ const m = createMat4();
480
+ mat42.scale(m, m, [width / 2, -height / 2, 1]);
481
+ mat42.translate(m, m, [1, -1, 0]);
482
+ mat42.multiply(m, m, vpm);
483
+ const mInverse = mat42.invert(createMat4(), m);
484
+ if (!mInverse) {
485
+ throw new Error("Pixel project matrix not invertible");
486
+ }
487
+ this.pixelProjectionMatrix = m;
488
+ this.pixelUnprojectionMatrix = mInverse;
489
+ }
490
+ projectFlat(lngLat) {
491
+ return lngLatToWorld(lngLat);
492
+ }
493
+ unprojectFlat(xy) {
494
+ return worldToLngLat(xy);
495
+ }
496
+ getMapCenterByLngLatPosition({ lngLat, pos }) {
497
+ const fromLocation = pixelsToWorld(pos, this.pixelUnprojectionMatrix);
498
+ const toLocation = lngLatToWorld(lngLat);
499
+ const translate3 = vec22.add([], toLocation, vec22.negate([], fromLocation));
500
+ const newCenter = vec22.add([], this.center, translate3);
501
+ return worldToLngLat(newCenter);
502
+ }
503
+ fitBounds(bounds, options = {}) {
504
+ const { width, height } = this;
505
+ const { longitude, latitude, zoom } = fitBounds(Object.assign({ width, height, bounds }, options));
506
+ return new WebMercatorViewport({ width, height, longitude, latitude, zoom });
507
+ }
508
+ getBounds(options) {
509
+ const corners = this.getBoundingRegion(options);
510
+ const west = Math.min(...corners.map((p) => p[0]));
511
+ const east = Math.max(...corners.map((p) => p[0]));
512
+ const south = Math.min(...corners.map((p) => p[1]));
513
+ const north = Math.max(...corners.map((p) => p[1]));
514
+ return [
515
+ [west, south],
516
+ [east, north]
517
+ ];
518
+ }
519
+ getBoundingRegion(options = {}) {
520
+ return getBounds(this, options.z || 0);
521
+ }
522
+ getLocationAtPoint({ lngLat, pos }) {
523
+ return this.getMapCenterByLngLatPosition({ lngLat, pos });
524
+ }
525
+ };
526
+
527
+ // src/normalize-viewport-props.ts
528
+ var TILE_SIZE2 = 512;
529
+ function normalizeViewportProps(props) {
530
+ const { width, height, pitch = 0 } = props;
531
+ let { longitude, latitude, zoom, bearing = 0 } = props;
532
+ if (longitude < -180 || longitude > 180) {
533
+ longitude = mod(longitude + 180, 360) - 180;
534
+ }
535
+ if (bearing < -180 || bearing > 180) {
536
+ bearing = mod(bearing + 180, 360) - 180;
537
+ }
538
+ const minZoom = log2(height / TILE_SIZE2);
539
+ if (zoom <= minZoom) {
540
+ zoom = minZoom;
541
+ latitude = 0;
542
+ } else {
543
+ const halfHeightPixels = height / 2 / Math.pow(2, zoom);
544
+ const minLatitude = worldToLngLat([0, halfHeightPixels])[1];
545
+ if (latitude < minLatitude) {
546
+ latitude = minLatitude;
547
+ } else {
548
+ const maxLatitude = worldToLngLat([0, TILE_SIZE2 - halfHeightPixels])[1];
549
+ if (latitude > maxLatitude) {
550
+ latitude = maxLatitude;
551
+ }
552
+ }
553
+ }
554
+ return { width, height, longitude, latitude, zoom, pitch, bearing };
555
+ }
556
+
557
+ // src/fly-to-viewport.ts
558
+ var vec23 = __toESM(require("gl-matrix/vec2"), 1);
559
+ var EPSILON = 0.01;
560
+ var VIEWPORT_TRANSITION_PROPS = ["longitude", "latitude", "zoom"];
561
+ var DEFAULT_OPTS = {
562
+ curve: 1.414,
563
+ speed: 1.2
564
+ };
565
+ function flyToViewport(startProps, endProps, t, options) {
566
+ const { startZoom, startCenterXY, uDelta, w0, u1, S, rho, rho2, r0 } = getFlyToTransitionParams(
567
+ startProps,
568
+ endProps,
569
+ options
570
+ );
571
+ if (u1 < EPSILON) {
572
+ const viewport = {};
573
+ for (const key of VIEWPORT_TRANSITION_PROPS) {
574
+ const startValue = startProps[key];
575
+ const endValue = endProps[key];
576
+ viewport[key] = lerp(startValue, endValue, t);
577
+ }
578
+ return viewport;
579
+ }
580
+ const s = t * S;
581
+ const w = Math.cosh(r0) / Math.cosh(r0 + rho * s);
582
+ const u = w0 * ((Math.cosh(r0) * Math.tanh(r0 + rho * s) - Math.sinh(r0)) / rho2) / u1;
583
+ const scaleIncrement = 1 / w;
584
+ const newZoom = startZoom + scaleToZoom(scaleIncrement);
585
+ const newCenterWorld = vec23.scale([], uDelta, u);
586
+ vec23.add(newCenterWorld, newCenterWorld, startCenterXY);
587
+ const newCenter = worldToLngLat(newCenterWorld);
588
+ return {
589
+ longitude: newCenter[0],
590
+ latitude: newCenter[1],
591
+ zoom: newZoom
592
+ };
593
+ }
594
+ function getFlyToDuration(startProps, endProps, options) {
595
+ const opts = __spreadValues(__spreadValues({}, DEFAULT_OPTS), options);
596
+ const { screenSpeed, speed, maxDuration } = opts;
597
+ const { S, rho } = getFlyToTransitionParams(startProps, endProps, opts);
598
+ const length2 = 1e3 * S;
599
+ let duration;
600
+ if (Number.isFinite(screenSpeed)) {
601
+ duration = length2 / (screenSpeed / rho);
602
+ } else {
603
+ duration = length2 / speed;
604
+ }
605
+ return Number.isFinite(maxDuration) && duration > maxDuration ? 0 : duration;
606
+ }
607
+ function getFlyToTransitionParams(startProps, endProps, opts) {
608
+ opts = Object.assign({}, DEFAULT_OPTS, opts);
609
+ const rho = opts.curve;
610
+ const startZoom = startProps.zoom;
611
+ const startCenter = [startProps.longitude, startProps.latitude];
612
+ const startScale = zoomToScale(startZoom);
613
+ const endZoom = endProps.zoom;
614
+ const endCenter = [endProps.longitude, endProps.latitude];
615
+ const scale5 = zoomToScale(endZoom - startZoom);
616
+ const startCenterXY = lngLatToWorld(startCenter);
617
+ const endCenterXY = lngLatToWorld(endCenter);
618
+ const uDelta = vec23.sub([], endCenterXY, startCenterXY);
619
+ const w0 = Math.max(startProps.width, startProps.height);
620
+ const w1 = w0 / scale5;
621
+ const u1 = vec23.length(uDelta) * startScale;
622
+ const _u1 = Math.max(u1, EPSILON);
623
+ const rho2 = rho * rho;
624
+ const b0 = (w1 * w1 - w0 * w0 + rho2 * rho2 * _u1 * _u1) / (2 * w0 * rho2 * _u1);
625
+ const b1 = (w1 * w1 - w0 * w0 - rho2 * rho2 * _u1 * _u1) / (2 * w1 * rho2 * _u1);
626
+ const r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0);
627
+ const r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1);
628
+ const S = (r1 - r0) / rho;
629
+ return { startZoom, startCenterXY, uDelta, w0, u1, S, rho, rho2, r0, r1 };
630
+ }
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { default } from "./web-mercator-viewport.js";
2
+ export { default as WebMercatorViewport } from "./web-mercator-viewport.js";
3
+ export { default as getBounds } from "./get-bounds.js";
4
+ export { default as fitBounds } from "./fit-bounds.js";
5
+ export { default as normalizeViewportProps } from "./normalize-viewport-props.js";
6
+ export { default as flyToViewport, getFlyToDuration } from "./fly-to-viewport.js";
7
+ export { MAX_LATITUDE, lngLatToWorld, worldToLngLat, worldToPixels, pixelsToWorld, zoomToScale, scaleToZoom, altitudeToFovy, fovyToAltitude, getMeterZoom, unitsPerMeter, getDistanceScales, addMetersToLngLat, getViewMatrix, getProjectionMatrix, getProjectionParameters } from "./web-mercator-utils.js";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["default","WebMercatorViewport","getBounds","fitBounds","normalizeViewportProps","flyToViewport","getFlyToDuration","MAX_LATITUDE","lngLatToWorld","worldToLngLat","worldToPixels","pixelsToWorld","zoomToScale","scaleToZoom","altitudeToFovy","fovyToAltitude","getMeterZoom","unitsPerMeter","getDistanceScales","addMetersToLngLat","getViewMatrix","getProjectionMatrix","getProjectionParameters"],"mappings":"SACQA,O;SACAA,OAAO,IAAIC,mB;SAEXD,OAAO,IAAIE,S;SACXF,OAAO,IAAIG,S;SACXH,OAAO,IAAII,sB;SACXJ,OAAO,IAAIK,a,EAAeC,gB;SAGhCC,Y,EACAC,a,EACAC,a,EACAC,a,EACAC,a,EACAC,W,EACAC,W,EACAC,c,EACAC,c,EACAC,Y,EACAC,a,EACAC,iB,EACAC,iB,EACAC,a,EACAC,mB,EACAC,uB","sourcesContent":["// Classic web-mercator-project\nexport {default} from './web-mercator-viewport';\nexport {default as WebMercatorViewport} from './web-mercator-viewport';\n\nexport {default as getBounds} from './get-bounds';\nexport {default as fitBounds} from './fit-bounds';\nexport {default as normalizeViewportProps} from './normalize-viewport-props';\nexport {default as flyToViewport, getFlyToDuration} from './fly-to-viewport';\n\nexport {\n MAX_LATITUDE,\n lngLatToWorld,\n worldToLngLat,\n worldToPixels,\n pixelsToWorld,\n zoomToScale,\n scaleToZoom,\n altitudeToFovy,\n fovyToAltitude,\n getMeterZoom,\n unitsPerMeter,\n getDistanceScales,\n addMetersToLngLat,\n getViewMatrix,\n getProjectionMatrix,\n getProjectionParameters\n} from './web-mercator-utils';\n\n/** Types */\nexport type {FitBoundsOptions} from './fit-bounds';\nexport type {DistanceScales} from './web-mercator-utils';\n"],"file":"index.js"}
File without changes
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/math-utils.ts"],"names":["transformMat4","scale","createMat4","transformVector","matrix","vector","result","mod","value","divisor","modulus","lerp","start","end","step","clamp","x","min","max","ieLog2","Math","log","LOG2E","log2"],"mappings":"AAAA,SAAQA,aAAR,EAAuBC,KAAvB,QAAmC,gBAAnC;AAGA,OAAO,SAASC,UAAT,GAAgC;AACrC,SAAO,CAAC,CAAD,EAAI,CAAJ,EAAO,CAAP,EAAU,CAAV,EAAa,CAAb,EAAgB,CAAhB,EAAmB,CAAnB,EAAsB,CAAtB,EAAyB,CAAzB,EAA4B,CAA5B,EAA+B,CAA/B,EAAkC,CAAlC,EAAqC,CAArC,EAAwC,CAAxC,EAA2C,CAA3C,EAA8C,CAA9C,CAAP;AACD;AAGD,OAAO,SAASC,eAAT,CAAyBC,MAAzB,EAA2CC,MAA3C,EAAuE;AAC5E,QAAMC,MAAM,GAAGN,aAAa,CAAC,EAAD,EAAiBK,MAAjB,EAAyBD,MAAzB,CAA5B;AACAH,EAAAA,KAAK,CAACK,MAAD,EAASA,MAAT,EAAiB,IAAIA,MAAM,CAAC,CAAD,CAA3B,CAAL;AACA,SAAOA,MAAP;AACD;AAED,OAAO,SAASC,GAAT,CAAaC,KAAb,EAA4BC,OAA5B,EAAqD;AAC1D,QAAMC,OAAO,GAAGF,KAAK,GAAGC,OAAxB;AACA,SAAOC,OAAO,GAAG,CAAV,GAAcD,OAAO,GAAGC,OAAxB,GAAkCA,OAAzC;AACD;AAED,OAAO,SAASC,IAAT,CAAcC,KAAd,EAA6BC,GAA7B,EAA0CC,IAA1C,EAAgE;AACrE,SAAOA,IAAI,GAAGD,GAAP,GAAa,CAAC,IAAIC,IAAL,IAAaF,KAAjC;AACD;AAED,OAAO,SAASG,KAAT,CAAeC,CAAf,EAA0BC,GAA1B,EAAuCC,GAAvC,EAA4D;AACjE,SAAOF,CAAC,GAAGC,GAAJ,GAAUA,GAAV,GAAgBD,CAAC,GAAGE,GAAJ,GAAUA,GAAV,GAAgBF,CAAvC;AACD;;AAED,SAASG,MAAT,CAAgBH,CAAhB,EAAmC;AACjC,SAAOI,IAAI,CAACC,GAAL,CAASL,CAAT,IAAcI,IAAI,CAACE,KAA1B;AACD;;AAED,OAAO,MAAMC,IAAI,GAAGH,IAAI,CAACG,IAAL,IAAaJ,MAA1B","sourcesContent":["import {transformMat4, scale} from 'gl-matrix/vec4';\n\n// Helper, avoids low-precision 32 bit matrices from gl-matrix mat4.create()\nexport function createMat4(): number[] {\n return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];\n}\n\n// Transforms a vec4 with a projection matrix\nexport function transformVector(matrix: number[], vector: number[]): number[] {\n const result = transformMat4([] as number[], vector, matrix);\n scale(result, result, 1 / result[3]);\n return result;\n}\n\nexport function mod(value: number, divisor: number): number {\n const modulus = value % divisor;\n return modulus < 0 ? divisor + modulus : modulus;\n}\n\nexport function lerp(start: number, end: number, step: number): number {\n return step * end + (1 - step) * start;\n}\n\nexport function clamp(x: number, min: number, max: number): number {\n return x < min ? min : x > max ? max : x;\n}\n\nfunction ieLog2(x: number): number {\n return Math.log(x) * Math.LOG2E;\n}\n// Handle missing log2 in IE 11\nexport const log2 = Math.log2 || ieLog2;\n"],"file":"math-utils.js"}
@@ -1,5 +1,5 @@
1
1
  /** Description of viewport */
2
- export declare type ViewportProps = {
2
+ export type ViewportProps = {
3
3
  width: number;
4
4
  height: number;
5
5
  longitude: number;
@@ -1 +1 @@
1
- {"version":3,"file":"normalize-viewport-props.d.ts","sourceRoot":"","sources":["../src/normalize-viewport-props.ts"],"names":[],"mappings":"AAMA,8BAA8B;AAC9B,oBAAY,aAAa,GAAG;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;GAGG;AAEH,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAAC,KAAK,EAAE,aAAa,GAAG,aAAa,CAgClF"}
1
+ {"version":3,"file":"normalize-viewport-props.d.ts","sourceRoot":"","sources":["../src/normalize-viewport-props.ts"],"names":[],"mappings":"AAMA,8BAA8B;AAC9B,MAAM,MAAM,aAAa,GAAG;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;GAGG;AAEH,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAAC,KAAK,EAAE,aAAa,GAAG,aAAa,CAgClF"}
@@ -1,5 +1,5 @@
1
- import { worldToLngLat } from './web-mercator-utils';
2
- import { mod, log2 } from './math-utils';
1
+ import { worldToLngLat } from "./web-mercator-utils.js";
2
+ import { mod, log2 } from "./math-utils.js";
3
3
  const TILE_SIZE = 512;
4
4
  export default function normalizeViewportProps(props) {
5
5
  const {
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/normalize-viewport-props.ts"],"names":["worldToLngLat","mod","log2","TILE_SIZE","normalizeViewportProps","props","width","height","pitch","longitude","latitude","zoom","bearing","minZoom","halfHeightPixels","Math","pow","minLatitude","maxLatitude"],"mappings":"SAAQA,a;SACAC,G,EAAKC,I;AAGb,MAAMC,SAAS,GAAG,GAAlB;AAkBA,eAAe,SAASC,sBAAT,CAAgCC,KAAhC,EAAqE;AAClF,QAAM;AAACC,IAAAA,KAAD;AAAQC,IAAAA,MAAR;AAAgBC,IAAAA,KAAK,GAAG;AAAxB,MAA6BH,KAAnC;AACA,MAAI;AAACI,IAAAA,SAAD;AAAYC,IAAAA,QAAZ;AAAsBC,IAAAA,IAAtB;AAA4BC,IAAAA,OAAO,GAAG;AAAtC,MAA2CP,KAA/C;;AAGA,MAAII,SAAS,GAAG,CAAC,GAAb,IAAoBA,SAAS,GAAG,GAApC,EAAyC;AACvCA,IAAAA,SAAS,GAAGR,GAAG,CAACQ,SAAS,GAAG,GAAb,EAAkB,GAAlB,CAAH,GAA4B,GAAxC;AACD;;AACD,MAAIG,OAAO,GAAG,CAAC,GAAX,IAAkBA,OAAO,GAAG,GAAhC,EAAqC;AACnCA,IAAAA,OAAO,GAAGX,GAAG,CAACW,OAAO,GAAG,GAAX,EAAgB,GAAhB,CAAH,GAA0B,GAApC;AACD;;AAGD,QAAMC,OAAO,GAAGX,IAAI,CAACK,MAAM,GAAGJ,SAAV,CAApB;;AACA,MAAIQ,IAAI,IAAIE,OAAZ,EAAqB;AACnBF,IAAAA,IAAI,GAAGE,OAAP;AACAH,IAAAA,QAAQ,GAAG,CAAX;AACD,GAHD,MAGO;AAEL,UAAMI,gBAAgB,GAAGP,MAAM,GAAG,CAAT,GAAaQ,IAAI,CAACC,GAAL,CAAS,CAAT,EAAYL,IAAZ,CAAtC;AACA,UAAMM,WAAW,GAAGjB,aAAa,CAAC,CAAC,CAAD,EAAIc,gBAAJ,CAAD,CAAb,CAAqC,CAArC,CAApB;;AACA,QAAIJ,QAAQ,GAAGO,WAAf,EAA4B;AAC1BP,MAAAA,QAAQ,GAAGO,WAAX;AACD,KAFD,MAEO;AACL,YAAMC,WAAW,GAAGlB,aAAa,CAAC,CAAC,CAAD,EAAIG,SAAS,GAAGW,gBAAhB,CAAD,CAAb,CAAiD,CAAjD,CAApB;;AACA,UAAIJ,QAAQ,GAAGQ,WAAf,EAA4B;AAC1BR,QAAAA,QAAQ,GAAGQ,WAAX;AACD;AACF;AACF;;AAED,SAAO;AAACZ,IAAAA,KAAD;AAAQC,IAAAA,MAAR;AAAgBE,IAAAA,SAAhB;AAA2BC,IAAAA,QAA3B;AAAqCC,IAAAA,IAArC;AAA2CH,IAAAA,KAA3C;AAAkDI,IAAAA;AAAlD,GAAP;AACD","sourcesContent":["import {worldToLngLat} from './web-mercator-utils';\nimport {mod, log2} from './math-utils';\n\n// defined by mapbox-gl\nconst TILE_SIZE = 512;\n\n/** Description of viewport */\nexport type ViewportProps = {\n width: number;\n height: number;\n longitude: number;\n latitude: number;\n zoom: number;\n pitch?: number;\n bearing?: number;\n};\n\n/**\n * Apply mathematical constraints to viewport props\n * @param props\n */\n// eslint-disable-next-line complexity\nexport default function normalizeViewportProps(props: ViewportProps): ViewportProps {\n const {width, height, pitch = 0} = props;\n let {longitude, latitude, zoom, bearing = 0} = props;\n\n // Normalize degrees\n if (longitude < -180 || longitude > 180) {\n longitude = mod(longitude + 180, 360) - 180;\n }\n if (bearing < -180 || bearing > 180) {\n bearing = mod(bearing + 180, 360) - 180;\n }\n\n // Constrain zoom and shift center at low zoom levels\n const minZoom = log2(height / TILE_SIZE);\n if (zoom <= minZoom) {\n zoom = minZoom;\n latitude = 0;\n } else {\n // Eliminate white space above and below the map\n const halfHeightPixels = height / 2 / Math.pow(2, zoom);\n const minLatitude = worldToLngLat([0, halfHeightPixels])[1];\n if (latitude < minLatitude) {\n latitude = minLatitude;\n } else {\n const maxLatitude = worldToLngLat([0, TILE_SIZE - halfHeightPixels])[1];\n if (latitude > maxLatitude) {\n latitude = maxLatitude;\n }\n }\n }\n\n return {width, height, longitude, latitude, zoom, pitch, bearing};\n}\n"],"file":"normalize-viewport-props.js"}