@math.gl/web-mercator 3.5.7 → 3.6.0-alpha.3

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 (92) hide show
  1. package/dist/assert.d.ts +2 -0
  2. package/dist/assert.d.ts.map +1 -0
  3. package/dist/assert.js +8 -0
  4. package/dist/es5/assert.js.map +1 -1
  5. package/dist/es5/fit-bounds.js +27 -24
  6. package/dist/es5/fit-bounds.js.map +1 -1
  7. package/dist/es5/fly-to-viewport.js +23 -16
  8. package/dist/es5/fly-to-viewport.js.map +1 -1
  9. package/dist/es5/get-bounds.js +1 -1
  10. package/dist/es5/get-bounds.js.map +1 -1
  11. package/dist/es5/index.js.map +1 -1
  12. package/dist/es5/math-utils.js +3 -9
  13. package/dist/es5/math-utils.js.map +1 -1
  14. package/dist/es5/normalize-viewport-props.js +10 -10
  15. package/dist/es5/normalize-viewport-props.js.map +1 -1
  16. package/dist/es5/web-mercator-utils.js +44 -60
  17. package/dist/es5/web-mercator-utils.js.map +1 -1
  18. package/dist/es5/web-mercator-viewport.js +69 -51
  19. package/dist/es5/web-mercator-viewport.js.map +1 -1
  20. package/dist/esm/assert.js.map +1 -1
  21. package/dist/esm/fit-bounds.js +24 -22
  22. package/dist/esm/fit-bounds.js.map +1 -1
  23. package/dist/esm/fly-to-viewport.js +13 -9
  24. package/dist/esm/fly-to-viewport.js.map +1 -1
  25. package/dist/esm/get-bounds.js +1 -1
  26. package/dist/esm/get-bounds.js.map +1 -1
  27. package/dist/esm/index.js.map +1 -1
  28. package/dist/esm/math-utils.js +3 -3
  29. package/dist/esm/math-utils.js.map +1 -1
  30. package/dist/esm/normalize-viewport-props.js +13 -9
  31. package/dist/esm/normalize-viewport-props.js.map +1 -1
  32. package/dist/esm/web-mercator-utils.js +46 -52
  33. package/dist/esm/web-mercator-utils.js.map +1 -1
  34. package/dist/esm/web-mercator-viewport.js +76 -34
  35. package/dist/esm/web-mercator-viewport.js.map +1 -1
  36. package/dist/fit-bounds.d.ts +50 -0
  37. package/dist/fit-bounds.d.ts.map +1 -0
  38. package/dist/fit-bounds.js +63 -0
  39. package/dist/fly-to-viewport.d.ts +19 -0
  40. package/dist/fly-to-viewport.d.ts.map +1 -0
  41. package/dist/fly-to-viewport.js +87 -0
  42. package/dist/get-bounds.d.ts +3 -0
  43. package/dist/get-bounds.d.ts.map +1 -0
  44. package/dist/get-bounds.js +51 -0
  45. package/dist/index.d.ts +11 -0
  46. package/dist/index.d.ts.map +1 -0
  47. package/dist/index.js +8 -0
  48. package/dist/math-utils.d.ts +7 -0
  49. package/dist/math-utils.d.ts.map +1 -0
  50. package/dist/math-utils.js +26 -0
  51. package/dist/normalize-viewport-props.d.ts +16 -0
  52. package/dist/normalize-viewport-props.d.ts.map +1 -0
  53. package/dist/normalize-viewport-props.js +41 -0
  54. package/dist/{es5/web-mercator-utils.d.ts → web-mercator-utils.d.ts} +78 -95
  55. package/dist/web-mercator-utils.d.ts.map +1 -0
  56. package/dist/web-mercator-utils.js +290 -0
  57. package/dist/web-mercator-viewport.d.ts +182 -0
  58. package/dist/web-mercator-viewport.d.ts.map +1 -0
  59. package/dist/web-mercator-viewport.js +281 -0
  60. package/package.json +4 -4
  61. package/src/{assert.js → assert.ts} +1 -1
  62. package/src/fit-bounds.ts +127 -0
  63. package/src/{fly-to-viewport.js → fly-to-viewport.ts} +52 -14
  64. package/src/{get-bounds.js → get-bounds.ts} +7 -5
  65. package/src/{index.js → index.ts} +4 -0
  66. package/src/math-utils.ts +32 -0
  67. package/src/{normalize-viewport-props.js → normalize-viewport-props.ts} +19 -10
  68. package/src/{web-mercator-utils.js → web-mercator-utils.ts} +184 -76
  69. package/src/web-mercator-viewport.ts +415 -0
  70. package/dist/es5/fit-bounds.d.ts +0 -64
  71. package/dist/es5/fly-to-viewport.d.ts +0 -31
  72. package/dist/es5/get-bounds.d.ts +0 -3
  73. package/dist/es5/index.d.ts +0 -24
  74. package/dist/es5/normalize-viewport-props.d.ts +0 -16
  75. package/dist/es5/web-mercator-viewport.d.ts +0 -187
  76. package/dist/esm/fit-bounds.d.ts +0 -64
  77. package/dist/esm/fly-to-viewport.d.ts +0 -31
  78. package/dist/esm/get-bounds.d.ts +0 -3
  79. package/dist/esm/index.d.ts +0 -24
  80. package/dist/esm/normalize-viewport-props.d.ts +0 -16
  81. package/dist/esm/web-mercator-utils.d.ts +0 -190
  82. package/dist/esm/web-mercator-viewport.d.ts +0 -187
  83. package/src/fit-bounds.d.ts +0 -64
  84. package/src/fit-bounds.js +0 -76
  85. package/src/fly-to-viewport.d.ts +0 -31
  86. package/src/get-bounds.d.ts +0 -3
  87. package/src/index.d.ts +0 -24
  88. package/src/math-utils.js +0 -32
  89. package/src/normalize-viewport-props.d.ts +0 -16
  90. package/src/web-mercator-utils.d.ts +0 -190
  91. package/src/web-mercator-viewport.d.ts +0 -187
  92. package/src/web-mercator-viewport.js +0 -262
@@ -1,187 +0,0 @@
1
- import { Bounds, FitBoundsOptions } from "./fit-bounds";
2
-
3
- type WebMercatorViewportOptions = {
4
- // Map state
5
- width: number;
6
- height: number;
7
- latitude?: number;
8
- longitude?: number;
9
- position?: number[];
10
- zoom?: number;
11
- pitch?: number;
12
- bearing?: number;
13
- altitude?: number;
14
- fovy?: number;
15
- nearZMultiplier?: number;
16
- farZMultiplier?: number;
17
- };
18
-
19
- /**
20
- * Creates view/projection matrices and "uniform values" from mercator params
21
- *
22
- * Note: `Viewport` instances are immutable in the sense that they only have accessors.
23
- * A new viewport instance should be created if any parameters have changed.
24
- */
25
- export default class WebMercatorViewport {
26
- latitude: number;
27
- longitude: number;
28
- zoom: number;
29
- pitch: number;
30
- bearing: number;
31
- altitude: number;
32
- fovy: number;
33
-
34
- meterOffset: number[];
35
- center: number[];
36
-
37
- width: number;
38
- height: number;
39
- scale: number;
40
- distanceScales: {
41
- unitsPerMeter: number[]
42
- };
43
-
44
- viewMatrix: number[];
45
- projectionMatrix: number[];
46
-
47
- viewProjectionMatrix: number[];
48
- pixelProjectionMatrix: number[];
49
- pixelUnprojectionMatrix: number[];
50
-
51
- /**
52
- * @classdesc
53
- * Creates view/projection matrices from mercator params
54
- * Note: The Viewport is immutable in the sense that it only has accessors.
55
- * A new viewport instance should be created if any parameters have changed.
56
- *
57
- * @class
58
- * @param {WebMercatorViewportOptions} opt - options
59
- *
60
- * @param [opt.width=1] - Width of "viewport" or window
61
- * @param [opt.height=1] - Height of "viewport" or window
62
- * @param [opt.scale=1] - Either use scale or zoom
63
- * @param [opt.pitch=0] - Camera angle in degrees (0 is straight down)
64
- * @param [opt.bearing=0] - Map rotation in degrees (0 means north is up)
65
- * @param [opt.fovy=] - Field of view of camera in degrees
66
- * @param [opt.altitude=] - Altitude of camera in screen units
67
- *
68
- * Web mercator projection short-hand parameters
69
- * @param [opt.latitude] - Center of viewport on map
70
- * @param [opt.longitude] - Center of viewport on map
71
- * @param [opt.zoom] - Scale = Math.pow(2,zoom) on map
72
-
73
- * Notes:
74
- * - Only one of center or [latitude, longitude] can be specified
75
- * - [latitude, longitude] can only be specified when "mercator" is true
76
- * - Altitude has a default value that matches assumptions in mapbox-gl
77
- * - Field of view is independent from altitude, provide `altitudeToFovy(1.5)` (default value) to match assumptions in mapbox-gl
78
- * - width and height are forced to 1 if supplied as 0, to avoid
79
- * division by zero. This is intended to reduce the burden of apps to
80
- * to check values before instantiating a Viewport.
81
- */
82
- constructor(options?: WebMercatorViewportOptions);
83
-
84
- /** Two viewports are equal if width and height are identical, and if
85
- * their view and projection matrices are (approximately) equal.
86
- */
87
- equals(viewport: WebMercatorViewport | null): boolean;
88
-
89
- /**
90
- * Projects xyz (possibly latitude and longitude) to pixel coordinates in window
91
- * using viewport projection parameters
92
- * - [longitude, latitude] to [x, y]
93
- * - [longitude, latitude, Z] => [x, y, z]
94
- * Note: By default, returns top-left coordinates for canvas/SVG type render
95
- *
96
- * @param lngLatZ - [lng, lat] or [lng, lat, Z]
97
- * @param options - options
98
- * @param options.topLeft=true - Whether projected coords are top left
99
- * @return - screen coordinates [x, y] or [x, y, z], z as pixel depth
100
- */
101
- project(lngLatZ: number[], options?: {topLeft?: boolean}): number[];
102
-
103
- /**
104
- * Unproject pixel coordinates on screen onto world coordinates, possibly `[lon, lat]` on map.
105
- *
106
- * - [x, y] => [lng, lat]
107
- * - [x, y, z] => [lng, lat, Z]
108
- *
109
- * @param xyz - screen coordinates, z as pixel depth
110
- * @param options - options
111
- * @param options.topLeft=true - Whether projected coords are top left
112
- * @param options.targetZ=0 - If pixel depth is unknown, targetZ is used as
113
- * the elevation plane to unproject onto
114
- * @return - [lng, lat, Z] or [X, Y, Z]
115
- */
116
- unproject(xyz: number[], options?: {topLeft?: boolean; targetZ?: number}): number[];
117
-
118
- /**
119
- * Project [lng,lat] on sphere onto [x,y] on 512*512 Mercator Zoom 0 tile.
120
- * Performs the nonlinear part of the web mercator projection.
121
- * Remaining projection is done with 4x4 matrices which also handles
122
- * perspective.
123
- *
124
- * @param lngLat - [lng, lat] coordinates
125
- * Specifies a point on the sphere to project onto the map.
126
- * @return [x,y] coordinates.
127
- */
128
- projectFlat(lngLat: number[]): number[];
129
-
130
- /**
131
- * Unproject world point [x,y] on map onto {lat, lon} on sphere
132
- *
133
- * @param xy - array with [x,y] members
134
- * representing point on projected map plane
135
- * @return - array with [lat,lon] of point on sphere.
136
- * Has toArray method if you need a GeoJSON Array.
137
- * Per cartographic tradition, lat and lon are specified as degrees.
138
- */
139
- unprojectFlat(xy: number[]): number[];
140
-
141
- /**
142
- * Get the map center that place a given [lng, lat] coordinate at screen
143
- * point [x, y]
144
- *
145
- * @param {object} opt
146
- * @param opt.lngLat - [lng,lat] coordinates
147
- * Specifies a point on the sphere.
148
- * @param opt.pos - [x,y] coordinates
149
- * Specifies a point on the screen.
150
- * @return [lng,lat] new map center.
151
- */
152
- getMapCenterByLngLatPosition({lngLat, pos}: {lngLat: number[], pos: number[]}): number[];
153
-
154
- /** @deprecated Legacy method name */
155
- getLocationAtPoint({lngLat, pos}: {lngLat: number[], pos: number[]}): number[];
156
-
157
- /**
158
- * Returns a new viewport that fit around the given rectangle.
159
- * Only supports non-perspective mode.
160
- * @param {Array} bounds - [[lon, lat], [lon, lat]]
161
- * @param {Object} [options]
162
- * @param {Number} [options.padding] - The amount of padding in pixels to add to the given bounds.
163
- * @param {Array} [options.offset] - The center of the given bounds relative to the map's center,
164
- * [x, y] measured in pixels.
165
- * @returns {WebMercatorViewport}
166
- */
167
- fitBounds(
168
- bounds: [[number, number], [number, number]],
169
- options?: Omit<FitBoundsOptions, 'width' | 'height' | 'bounds'>,
170
- ): WebMercatorViewport;
171
-
172
- /**
173
- * Returns the bounding box of the viewport.
174
- * @param {Object} [options]
175
- * @param {Number} [options.z] - The altitude at which the bounds should be calculated.
176
- * @returns {Array} bounds - [[lon, lat], [lon, lat]]
177
- */
178
- getBounds(options?: {z?: number}) : Array<number[]>;
179
-
180
- /**
181
- * Returns the bounding box of the viewport.
182
- * @param {Object} [options]
183
- * @param {Number} [options.z] - The altitude at which the bounds should be calculated.
184
- * @returns {Array} an array of 4 points that define the visible region
185
- */
186
- getBoundingRegion(options?: {z?: number}) : Array<number[]>;
187
- }
@@ -1,262 +0,0 @@
1
- // View and Projection Matrix calculations for mapbox-js style map view properties
2
- import {createMat4} from './math-utils';
3
-
4
- import {
5
- zoomToScale,
6
- pixelsToWorld,
7
- lngLatToWorld,
8
- worldToLngLat,
9
- worldToPixels,
10
- altitudeToFovy,
11
- fovyToAltitude,
12
- DEFAULT_ALTITUDE,
13
- getProjectionMatrix,
14
- getDistanceScales,
15
- getViewMatrix
16
- } from './web-mercator-utils';
17
- import fitBounds from './fit-bounds';
18
- import getBounds from './get-bounds';
19
-
20
- import * as mat4 from 'gl-matrix/mat4';
21
- import * as vec2 from 'gl-matrix/vec2';
22
- import * as vec3 from 'gl-matrix/vec3';
23
-
24
- export default class WebMercatorViewport {
25
- // eslint-disable-next-line max-statements
26
- constructor(
27
- {
28
- // Map state
29
- width,
30
- height,
31
- latitude = 0,
32
- longitude = 0,
33
- zoom = 0,
34
- pitch = 0,
35
- bearing = 0,
36
- altitude = null,
37
- fovy = null,
38
- position = null,
39
- nearZMultiplier = 0.02,
40
- farZMultiplier = 1.01
41
- } = {width: 1, height: 1}
42
- ) {
43
- // Silently allow apps to send in 0,0 to facilitate isomorphic render etc
44
- width = width || 1;
45
- height = height || 1;
46
-
47
- // `fovy` & `altitude` are independent parameters, one for the
48
- // projection and the latter for the view matrix. In the past,
49
- // the `fovy` was always derived from the `altitude`
50
- if (fovy === null && altitude === null) {
51
- altitude = DEFAULT_ALTITUDE;
52
- fovy = altitudeToFovy(altitude);
53
- } else if (fovy === null) {
54
- fovy = altitudeToFovy(altitude);
55
- } else if (altitude === null) {
56
- altitude = fovyToAltitude(fovy);
57
- }
58
-
59
- const scale = zoomToScale(zoom);
60
- // Altitude - prevent division by 0
61
- // TODO - just throw an Error instead?
62
- altitude = Math.max(0.75, altitude);
63
-
64
- const distanceScales = getDistanceScales({longitude, latitude});
65
-
66
- const center = lngLatToWorld([longitude, latitude]);
67
- center[2] = 0;
68
-
69
- if (position) {
70
- vec3.add(center, center, vec3.mul([], position, distanceScales.unitsPerMeter));
71
- }
72
-
73
- this.projectionMatrix = getProjectionMatrix({
74
- width,
75
- height,
76
- pitch,
77
- fovy,
78
- nearZMultiplier,
79
- farZMultiplier
80
- });
81
-
82
- this.viewMatrix = getViewMatrix({
83
- height,
84
- scale,
85
- center,
86
- pitch,
87
- bearing,
88
- altitude
89
- });
90
-
91
- // Save parameters
92
- this.width = width;
93
- this.height = height;
94
- this.scale = scale;
95
-
96
- this.latitude = latitude;
97
- this.longitude = longitude;
98
- this.zoom = zoom;
99
- this.pitch = pitch;
100
- this.bearing = bearing;
101
- this.altitude = altitude;
102
- this.fovy = fovy;
103
- this.center = center;
104
- this.meterOffset = position || [0, 0, 0];
105
-
106
- this.distanceScales = distanceScales;
107
-
108
- this._initMatrices();
109
-
110
- // Bind methods for easy access
111
- this.equals = this.equals.bind(this);
112
- this.project = this.project.bind(this);
113
- this.unproject = this.unproject.bind(this);
114
- this.projectPosition = this.projectPosition.bind(this);
115
- this.unprojectPosition = this.unprojectPosition.bind(this);
116
-
117
- Object.freeze(this);
118
- }
119
-
120
- _initMatrices() {
121
- const {width, height, projectionMatrix, viewMatrix} = this;
122
-
123
- // Note: As usual, matrix operations should be applied in "reverse" order
124
- // since vectors will be multiplied in from the right during transformation
125
- const vpm = createMat4();
126
- mat4.multiply(vpm, vpm, projectionMatrix);
127
- mat4.multiply(vpm, vpm, viewMatrix);
128
- this.viewProjectionMatrix = vpm;
129
-
130
- // Calculate matrices and scales needed for projection
131
- /**
132
- * Builds matrices that converts preprojected lngLats to screen pixels
133
- * and vice versa.
134
- * Note: Currently returns bottom-left coordinates!
135
- * Note: Starts with the GL projection matrix and adds steps to the
136
- * scale and translate that matrix onto the window.
137
- * Note: WebGL controls clip space to screen projection with gl.viewport
138
- * and does not need this step.
139
- */
140
- const m = createMat4();
141
-
142
- // matrix for conversion from location to screen coordinates
143
- mat4.scale(m, m, [width / 2, -height / 2, 1]);
144
- mat4.translate(m, m, [1, -1, 0]);
145
- mat4.multiply(m, m, vpm);
146
-
147
- const mInverse = mat4.invert(createMat4(), m);
148
- if (!mInverse) {
149
- throw new Error('Pixel project matrix not invertible');
150
- }
151
-
152
- this.pixelProjectionMatrix = m;
153
- this.pixelUnprojectionMatrix = mInverse;
154
- }
155
-
156
- // Two viewports are equal if width and height are identical, and if
157
- // their view and projection matrices are (approximately) equal.
158
- equals(viewport) {
159
- if (!(viewport instanceof WebMercatorViewport)) {
160
- return false;
161
- }
162
-
163
- return (
164
- viewport.width === this.width &&
165
- viewport.height === this.height &&
166
- mat4.equals(viewport.projectionMatrix, this.projectionMatrix) &&
167
- mat4.equals(viewport.viewMatrix, this.viewMatrix)
168
- );
169
- }
170
-
171
- // Projects xyz (possibly latitude and longitude) to pixel coordinates in window
172
- // using viewport projection parameters
173
- project(xyz, {topLeft = true} = {}) {
174
- const worldPosition = this.projectPosition(xyz);
175
- const coord = worldToPixels(worldPosition, this.pixelProjectionMatrix);
176
-
177
- const [x, y] = coord;
178
- const y2 = topLeft ? y : this.height - y;
179
- return xyz.length === 2 ? [x, y2] : [x, y2, coord[2]];
180
- }
181
-
182
- // Unproject pixel coordinates on screen onto world coordinates,
183
- // (possibly [lon, lat]) on map.
184
- unproject(xyz, {topLeft = true, targetZ = undefined} = {}) {
185
- const [x, y, z] = xyz;
186
-
187
- const y2 = topLeft ? y : this.height - y;
188
- const targetZWorld = targetZ && targetZ * this.distanceScales.unitsPerMeter[2];
189
- const coord = pixelsToWorld([x, y2, z], this.pixelUnprojectionMatrix, targetZWorld);
190
- const [X, Y, Z] = this.unprojectPosition(coord);
191
-
192
- if (Number.isFinite(z)) {
193
- return [X, Y, Z];
194
- }
195
- return Number.isFinite(targetZ) ? [X, Y, targetZ] : [X, Y];
196
- }
197
-
198
- // NON_LINEAR PROJECTION HOOKS
199
- // Used for web meractor projection
200
-
201
- projectPosition(xyz) {
202
- const [X, Y] = lngLatToWorld(xyz);
203
- const Z = (xyz[2] || 0) * this.distanceScales.unitsPerMeter[2];
204
- return [X, Y, Z];
205
- }
206
-
207
- unprojectPosition(xyz) {
208
- const [X, Y] = worldToLngLat(xyz);
209
- const Z = (xyz[2] || 0) * this.distanceScales.metersPerUnit[2];
210
- return [X, Y, Z];
211
- }
212
-
213
- // Project [lng,lat] on sphere onto [x,y] on 512*512 Mercator Zoom 0 tile.
214
- projectFlat(lngLat) {
215
- return lngLatToWorld(lngLat);
216
- }
217
-
218
- // Unproject world point [x,y] on map onto {lat, lon} on sphere
219
- unprojectFlat(xy) {
220
- return worldToLngLat(xy);
221
- }
222
-
223
- // Get the map center that place a given [lng, lat] coordinate at screen point [x, y]
224
- getMapCenterByLngLatPosition({lngLat, pos}) {
225
- const fromLocation = pixelsToWorld(pos, this.pixelUnprojectionMatrix);
226
- const toLocation = lngLatToWorld(lngLat);
227
-
228
- const translate = vec2.add([], toLocation, vec2.negate([], fromLocation));
229
- const newCenter = vec2.add([], this.center, translate);
230
-
231
- return worldToLngLat(newCenter);
232
- }
233
-
234
- // Legacy method name
235
- getLocationAtPoint({lngLat, pos}) {
236
- return this.getMapCenterByLngLatPosition({lngLat, pos});
237
- }
238
-
239
- // Returns a new viewport that fit around the given rectangle.
240
- fitBounds(bounds, options = {}) {
241
- const {width, height} = this;
242
- const {longitude, latitude, zoom} = fitBounds(Object.assign({width, height, bounds}, options));
243
- return new WebMercatorViewport({width, height, longitude, latitude, zoom});
244
- }
245
-
246
- getBounds(options) {
247
- const corners = this.getBoundingRegion(options);
248
-
249
- const west = Math.min(...corners.map((p) => p[0]));
250
- const east = Math.max(...corners.map((p) => p[0]));
251
- const south = Math.min(...corners.map((p) => p[1]));
252
- const north = Math.max(...corners.map((p) => p[1]));
253
- return [
254
- [west, south],
255
- [east, north]
256
- ];
257
- }
258
-
259
- getBoundingRegion(options = {}) {
260
- return getBounds(this, options.z || 0);
261
- }
262
- }