@math.gl/web-mercator 3.5.3 → 3.6.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.
- package/dist/assert.d.ts +2 -0
- package/dist/assert.d.ts.map +1 -0
- package/dist/assert.js +8 -0
- package/dist/es5/assert.js.map +1 -1
- package/dist/es5/fit-bounds.js +40 -50
- package/dist/es5/fit-bounds.js.map +1 -1
- package/dist/es5/fly-to-viewport.js +82 -98
- package/dist/es5/fly-to-viewport.js.map +1 -1
- package/dist/es5/get-bounds.js +25 -24
- package/dist/es5/get-bounds.js.map +1 -1
- package/dist/es5/index.js +23 -25
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/math-utils.js +5 -11
- package/dist/es5/math-utils.js.map +1 -1
- package/dist/es5/normalize-viewport-props.js +36 -51
- package/dist/es5/normalize-viewport-props.js.map +1 -1
- package/dist/es5/web-mercator-utils.js +104 -144
- package/dist/es5/web-mercator-utils.js.map +1 -1
- package/dist/es5/web-mercator-viewport.js +198 -241
- package/dist/es5/web-mercator-viewport.js.map +1 -1
- package/dist/esm/assert.js.map +1 -1
- package/dist/esm/fit-bounds.js +24 -22
- package/dist/esm/fit-bounds.js.map +1 -1
- package/dist/esm/fly-to-viewport.js +13 -9
- package/dist/esm/fly-to-viewport.js.map +1 -1
- package/dist/esm/get-bounds.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math-utils.js +3 -3
- package/dist/esm/math-utils.js.map +1 -1
- package/dist/esm/normalize-viewport-props.js +30 -41
- package/dist/esm/normalize-viewport-props.js.map +1 -1
- package/dist/esm/web-mercator-utils.js +46 -52
- package/dist/esm/web-mercator-utils.js.map +1 -1
- package/dist/esm/web-mercator-viewport.js +75 -33
- package/dist/esm/web-mercator-viewport.js.map +1 -1
- package/dist/fit-bounds.d.ts +49 -0
- package/dist/fit-bounds.d.ts.map +1 -0
- package/dist/fit-bounds.js +70 -0
- package/dist/fly-to-viewport.d.ts +19 -0
- package/dist/fly-to-viewport.d.ts.map +1 -0
- package/dist/fly-to-viewport.js +87 -0
- package/dist/get-bounds.d.ts +3 -0
- package/dist/get-bounds.d.ts.map +1 -0
- package/dist/get-bounds.js +51 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/math-utils.d.ts +6 -0
- package/dist/math-utils.d.ts.map +1 -0
- package/dist/math-utils.js +23 -0
- package/dist/normalize-viewport-props.d.ts +17 -0
- package/dist/normalize-viewport-props.d.ts.map +1 -0
- package/dist/normalize-viewport-props.js +41 -0
- package/dist/{es5/web-mercator-utils.d.ts → web-mercator-utils.d.ts} +77 -94
- package/dist/web-mercator-utils.d.ts.map +1 -0
- package/dist/web-mercator-utils.js +289 -0
- package/dist/web-mercator-viewport.d.ts +183 -0
- package/dist/web-mercator-viewport.d.ts.map +1 -0
- package/dist/web-mercator-viewport.js +282 -0
- package/package.json +4 -4
- package/src/{assert.js → assert.ts} +1 -1
- package/src/fit-bounds.ts +135 -0
- package/src/{fly-to-viewport.js → fly-to-viewport.ts} +33 -10
- package/src/{get-bounds.js → get-bounds.ts} +4 -2
- package/src/{index.js → index.ts} +0 -0
- package/src/{math-utils.js → math-utils.ts} +7 -7
- package/src/normalize-viewport-props.ts +55 -0
- package/src/{web-mercator-utils.js → web-mercator-utils.ts} +181 -72
- package/src/web-mercator-viewport.ts +417 -0
- package/dist/es5/fit-bounds.d.ts +0 -64
- package/dist/es5/fly-to-viewport.d.ts +0 -31
- package/dist/es5/get-bounds.d.ts +0 -3
- package/dist/es5/index.d.ts +0 -23
- package/dist/es5/normalize-viewport-props.d.ts +0 -16
- package/dist/es5/web-mercator-viewport.d.ts +0 -187
- package/dist/esm/fit-bounds.d.ts +0 -64
- package/dist/esm/fly-to-viewport.d.ts +0 -31
- package/dist/esm/get-bounds.d.ts +0 -3
- package/dist/esm/index.d.ts +0 -23
- package/dist/esm/normalize-viewport-props.d.ts +0 -16
- package/dist/esm/web-mercator-utils.d.ts +0 -189
- package/dist/esm/web-mercator-viewport.d.ts +0 -187
- package/src/fit-bounds.d.ts +0 -64
- package/src/fit-bounds.js +0 -84
- package/src/fly-to-viewport.d.ts +0 -31
- package/src/get-bounds.d.ts +0 -3
- package/src/index.d.ts +0 -23
- package/src/normalize-viewport-props.d.ts +0 -16
- package/src/normalize-viewport-props.js +0 -55
- package/src/web-mercator-utils.d.ts +0 -189
- package/src/web-mercator-viewport.d.ts +0 -187
- package/src/web-mercator-viewport.js +0 -262
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
// View and Projection Matrix calculations for mapbox-js style map view properties
|
|
2
|
+
import { createMat4 } from './math-utils';
|
|
3
|
+
import { zoomToScale, pixelsToWorld, lngLatToWorld, worldToLngLat, worldToPixels, altitudeToFovy, fovyToAltitude, DEFAULT_ALTITUDE, getProjectionMatrix, getDistanceScales, getViewMatrix } from './web-mercator-utils';
|
|
4
|
+
import fitBounds from './fit-bounds';
|
|
5
|
+
import getBounds from './get-bounds';
|
|
6
|
+
import * as mat4 from 'gl-matrix/mat4';
|
|
7
|
+
import * as vec2 from 'gl-matrix/vec2';
|
|
8
|
+
import * as vec3 from 'gl-matrix/vec3';
|
|
9
|
+
/**
|
|
10
|
+
* The WebMercatorViewport class creates
|
|
11
|
+
* - view/projection matrices
|
|
12
|
+
* - "uniform values" (for shaders) from mercator params
|
|
13
|
+
*
|
|
14
|
+
* Note: Instances are immutable in the sense that they only have accessors.
|
|
15
|
+
* A new viewport instance should be created if any parameters have changed.
|
|
16
|
+
*/
|
|
17
|
+
export default class WebMercatorViewport {
|
|
18
|
+
/**
|
|
19
|
+
* @classdesc
|
|
20
|
+
* Creates view/projection matrices from mercator params
|
|
21
|
+
* Note: The Viewport is immutable in the sense that it only has accessors.
|
|
22
|
+
* A new viewport instance should be created if any parameters have changed.
|
|
23
|
+
*/
|
|
24
|
+
// eslint-disable-next-line max-statements
|
|
25
|
+
constructor(props = { width: 1, height: 1 }) {
|
|
26
|
+
let {
|
|
27
|
+
// Map state
|
|
28
|
+
width, height, altitude = null, fovy = null } = props;
|
|
29
|
+
const { latitude = 0, longitude = 0, zoom = 0, pitch = 0, bearing = 0, position = null, nearZMultiplier = 0.02, farZMultiplier = 1.01 } = props;
|
|
30
|
+
// Silently allow apps to send in 0,0 to facilitate isomorphic render etc
|
|
31
|
+
width = width || 1;
|
|
32
|
+
height = height || 1;
|
|
33
|
+
// `fovy` & `altitude` are independent parameters, one for the
|
|
34
|
+
// projection and the latter for the view matrix. In the past,
|
|
35
|
+
// the `fovy` was always derived from the `altitude`
|
|
36
|
+
if (fovy === null && altitude === null) {
|
|
37
|
+
altitude = DEFAULT_ALTITUDE;
|
|
38
|
+
fovy = altitudeToFovy(altitude);
|
|
39
|
+
}
|
|
40
|
+
else if (fovy === null) {
|
|
41
|
+
fovy = altitudeToFovy(altitude);
|
|
42
|
+
}
|
|
43
|
+
else if (altitude === null) {
|
|
44
|
+
altitude = fovyToAltitude(fovy);
|
|
45
|
+
}
|
|
46
|
+
const scale = zoomToScale(zoom);
|
|
47
|
+
// Altitude - prevent division by 0
|
|
48
|
+
// TODO - just throw an Error instead?
|
|
49
|
+
altitude = Math.max(0.75, altitude);
|
|
50
|
+
const distanceScales = getDistanceScales({ longitude, latitude });
|
|
51
|
+
const center = lngLatToWorld([longitude, latitude]);
|
|
52
|
+
center[2] = 0;
|
|
53
|
+
if (position) {
|
|
54
|
+
vec3.add(center, center, vec3.mul([], position, distanceScales.unitsPerMeter));
|
|
55
|
+
}
|
|
56
|
+
this.projectionMatrix = getProjectionMatrix({
|
|
57
|
+
width,
|
|
58
|
+
height,
|
|
59
|
+
pitch,
|
|
60
|
+
fovy,
|
|
61
|
+
nearZMultiplier,
|
|
62
|
+
farZMultiplier
|
|
63
|
+
});
|
|
64
|
+
this.viewMatrix = getViewMatrix({
|
|
65
|
+
height,
|
|
66
|
+
scale,
|
|
67
|
+
center,
|
|
68
|
+
pitch,
|
|
69
|
+
bearing,
|
|
70
|
+
altitude
|
|
71
|
+
});
|
|
72
|
+
// Save parameters
|
|
73
|
+
this.width = width;
|
|
74
|
+
this.height = height;
|
|
75
|
+
this.scale = scale;
|
|
76
|
+
this.latitude = latitude;
|
|
77
|
+
this.longitude = longitude;
|
|
78
|
+
this.zoom = zoom;
|
|
79
|
+
this.pitch = pitch;
|
|
80
|
+
this.bearing = bearing;
|
|
81
|
+
this.altitude = altitude;
|
|
82
|
+
this.fovy = fovy;
|
|
83
|
+
this.center = center;
|
|
84
|
+
this.meterOffset = position || [0, 0, 0];
|
|
85
|
+
this.distanceScales = distanceScales;
|
|
86
|
+
this._initMatrices();
|
|
87
|
+
// Bind methods for easy access
|
|
88
|
+
this.equals = this.equals.bind(this);
|
|
89
|
+
this.project = this.project.bind(this);
|
|
90
|
+
this.unproject = this.unproject.bind(this);
|
|
91
|
+
this.projectPosition = this.projectPosition.bind(this);
|
|
92
|
+
this.unprojectPosition = this.unprojectPosition.bind(this);
|
|
93
|
+
Object.freeze(this);
|
|
94
|
+
}
|
|
95
|
+
_initMatrices() {
|
|
96
|
+
const { width, height, projectionMatrix, viewMatrix } = this;
|
|
97
|
+
// Note: As usual, matrix operations should be applied in "reverse" order
|
|
98
|
+
// since vectors will be multiplied in from the right during transformation
|
|
99
|
+
const vpm = createMat4();
|
|
100
|
+
mat4.multiply(vpm, vpm, projectionMatrix);
|
|
101
|
+
mat4.multiply(vpm, vpm, viewMatrix);
|
|
102
|
+
this.viewProjectionMatrix = vpm;
|
|
103
|
+
// Calculate matrices and scales needed for projection
|
|
104
|
+
/**
|
|
105
|
+
* Builds matrices that converts preprojected lngLats to screen pixels
|
|
106
|
+
* and vice versa.
|
|
107
|
+
* Note: Currently returns bottom-left coordinates!
|
|
108
|
+
* Note: Starts with the GL projection matrix and adds steps to the
|
|
109
|
+
* scale and translate that matrix onto the window.
|
|
110
|
+
* Note: WebGL controls clip space to screen projection with gl.viewport
|
|
111
|
+
* and does not need this step.
|
|
112
|
+
*/
|
|
113
|
+
const m = createMat4();
|
|
114
|
+
// matrix for conversion from location to screen coordinates
|
|
115
|
+
mat4.scale(m, m, [width / 2, -height / 2, 1]);
|
|
116
|
+
mat4.translate(m, m, [1, -1, 0]);
|
|
117
|
+
mat4.multiply(m, m, vpm);
|
|
118
|
+
const mInverse = mat4.invert(createMat4(), m);
|
|
119
|
+
if (!mInverse) {
|
|
120
|
+
throw new Error('Pixel project matrix not invertible');
|
|
121
|
+
}
|
|
122
|
+
this.pixelProjectionMatrix = m;
|
|
123
|
+
this.pixelUnprojectionMatrix = mInverse;
|
|
124
|
+
}
|
|
125
|
+
/** Two viewports are equal if width and height are identical, and if
|
|
126
|
+
* their view and projection matrices are (approximately) equal.
|
|
127
|
+
*/
|
|
128
|
+
equals(viewport) {
|
|
129
|
+
if (!(viewport instanceof WebMercatorViewport)) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
return (viewport.width === this.width &&
|
|
133
|
+
viewport.height === this.height &&
|
|
134
|
+
mat4.equals(viewport.projectionMatrix, this.projectionMatrix) &&
|
|
135
|
+
mat4.equals(viewport.viewMatrix, this.viewMatrix));
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Projects xyz (possibly latitude and longitude) to pixel coordinates in window
|
|
139
|
+
* using viewport projection parameters
|
|
140
|
+
* - [longitude, latitude] to [x, y]
|
|
141
|
+
* - [longitude, latitude, Z] => [x, y, z]
|
|
142
|
+
* Note: By default, returns top-left coordinates for canvas/SVG type render
|
|
143
|
+
*
|
|
144
|
+
* @param lngLatZ - [lng, lat] or [lng, lat, Z]
|
|
145
|
+
* @param options - options
|
|
146
|
+
* @param options.topLeft=true - Whether projected coords are top left
|
|
147
|
+
* @return - screen coordinates [x, y] or [x, y, z], z as pixel depth
|
|
148
|
+
*/
|
|
149
|
+
project(lngLatZ, options = {}) {
|
|
150
|
+
const { topLeft = true } = options;
|
|
151
|
+
const worldPosition = this.projectPosition(lngLatZ);
|
|
152
|
+
const coord = worldToPixels(worldPosition, this.pixelProjectionMatrix);
|
|
153
|
+
const [x, y] = coord;
|
|
154
|
+
const y2 = topLeft ? y : this.height - y;
|
|
155
|
+
return lngLatZ.length === 2 ? [x, y2] : [x, y2, coord[2]];
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Unproject pixel coordinates on screen onto world coordinates, possibly `[lon, lat]` on map.
|
|
159
|
+
*
|
|
160
|
+
* - [x, y] => [lng, lat]
|
|
161
|
+
* - [x, y, z] => [lng, lat, Z]
|
|
162
|
+
*
|
|
163
|
+
* @param xyz - screen coordinates, z as pixel depth
|
|
164
|
+
* @param options - options
|
|
165
|
+
* @param options.topLeft=true - Whether projected coords are top left
|
|
166
|
+
* @param options.targetZ=0 - If pixel depth is unknown, targetZ is used as
|
|
167
|
+
* the elevation plane to unproject onto
|
|
168
|
+
* @return - [lng, lat, Z] or [X, Y, Z]
|
|
169
|
+
*/
|
|
170
|
+
unproject(xyz, options = {}) {
|
|
171
|
+
const { topLeft = true, targetZ = undefined } = options;
|
|
172
|
+
const [x, y, z] = xyz;
|
|
173
|
+
const y2 = topLeft ? y : this.height - y;
|
|
174
|
+
const targetZWorld = targetZ && targetZ * this.distanceScales.unitsPerMeter[2];
|
|
175
|
+
const coord = pixelsToWorld([x, y2, z], this.pixelUnprojectionMatrix, targetZWorld);
|
|
176
|
+
const [X, Y, Z] = this.unprojectPosition(coord);
|
|
177
|
+
if (Number.isFinite(z)) {
|
|
178
|
+
return [X, Y, Z];
|
|
179
|
+
}
|
|
180
|
+
return Number.isFinite(targetZ) ? [X, Y, targetZ] : [X, Y];
|
|
181
|
+
}
|
|
182
|
+
// NON_LINEAR PROJECTION HOOKS
|
|
183
|
+
// Used for web meractor projection
|
|
184
|
+
projectPosition(xyz) {
|
|
185
|
+
const [X, Y] = lngLatToWorld(xyz);
|
|
186
|
+
const Z = (xyz[2] || 0) * this.distanceScales.unitsPerMeter[2];
|
|
187
|
+
return [X, Y, Z];
|
|
188
|
+
}
|
|
189
|
+
unprojectPosition(xyz) {
|
|
190
|
+
const [X, Y] = worldToLngLat(xyz);
|
|
191
|
+
// @ts-expect-error
|
|
192
|
+
const Z = (xyz[2] || 0) * this.distanceScales.metersPerUnit[2];
|
|
193
|
+
return [X, Y, Z];
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Project [lng,lat] on sphere onto [x,y] on 512*512 Mercator Zoom 0 tile.
|
|
197
|
+
* Performs the nonlinear part of the web mercator projection.
|
|
198
|
+
* Remaining projection is done with 4x4 matrices which also handles
|
|
199
|
+
* perspective.
|
|
200
|
+
*
|
|
201
|
+
* @param lngLat - [lng, lat] coordinates
|
|
202
|
+
* Specifies a point on the sphere to project onto the map.
|
|
203
|
+
* @return [x,y] coordinates.
|
|
204
|
+
*/
|
|
205
|
+
projectFlat(lngLat) {
|
|
206
|
+
return lngLatToWorld(lngLat);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Unproject world point [x,y] on map onto {lat, lon} on sphere
|
|
210
|
+
*
|
|
211
|
+
* @param xy - array with [x,y] members
|
|
212
|
+
* representing point on projected map plane
|
|
213
|
+
* @return - array with [lat,lon] of point on sphere.
|
|
214
|
+
* Has toArray method if you need a GeoJSON Array.
|
|
215
|
+
* Per cartographic tradition, lat and lon are specified as degrees.
|
|
216
|
+
*/
|
|
217
|
+
unprojectFlat(xy) {
|
|
218
|
+
return worldToLngLat(xy);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Get the map center that place a given [lng, lat] coordinate at screen point [x, y]
|
|
222
|
+
* @param opt
|
|
223
|
+
* @param opt.lngLat - [lng,lat] coordinates
|
|
224
|
+
* Specifies a point on the sphere.
|
|
225
|
+
* @param opt.pos - [x,y] coordinates
|
|
226
|
+
* Specifies a point on the screen.
|
|
227
|
+
* @return [lng,lat] new map center.
|
|
228
|
+
*/
|
|
229
|
+
getMapCenterByLngLatPosition({ lngLat, pos }) {
|
|
230
|
+
const fromLocation = pixelsToWorld(pos, this.pixelUnprojectionMatrix);
|
|
231
|
+
const toLocation = lngLatToWorld(lngLat);
|
|
232
|
+
const translate = vec2.add([], toLocation, vec2.negate([], fromLocation));
|
|
233
|
+
const newCenter = vec2.add([], this.center, translate);
|
|
234
|
+
return worldToLngLat(newCenter);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Returns a new viewport that fit around the given rectangle.
|
|
238
|
+
* Only supports non-perspective mode.
|
|
239
|
+
* @param bounds - [[lon, lat], [lon, lat]]
|
|
240
|
+
* @param [options]
|
|
241
|
+
* @param [options.padding] - The amount of padding in pixels to add to the given bounds.
|
|
242
|
+
* @param [options.offset] - The center of the given bounds relative to the map's center,
|
|
243
|
+
* [x, y] measured in pixels.
|
|
244
|
+
* @returns {WebMercatorViewport}
|
|
245
|
+
*/
|
|
246
|
+
fitBounds(bounds, options = {}) {
|
|
247
|
+
const { width, height } = this;
|
|
248
|
+
const { longitude, latitude, zoom } = fitBounds(Object.assign({ width, height, bounds }, options));
|
|
249
|
+
return new WebMercatorViewport({ width, height, longitude, latitude, zoom });
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Returns the bounding box of the viewport.
|
|
253
|
+
* @param [options]
|
|
254
|
+
* @param [options.z] - The altitude at which the bounds should be calculated.
|
|
255
|
+
* @returns {Array} bounds - [[lon, lat], [lon, lat]]
|
|
256
|
+
*/
|
|
257
|
+
getBounds(options) {
|
|
258
|
+
const corners = this.getBoundingRegion(options);
|
|
259
|
+
const west = Math.min(...corners.map((p) => p[0]));
|
|
260
|
+
const east = Math.max(...corners.map((p) => p[0]));
|
|
261
|
+
const south = Math.min(...corners.map((p) => p[1]));
|
|
262
|
+
const north = Math.max(...corners.map((p) => p[1]));
|
|
263
|
+
return [
|
|
264
|
+
[west, south],
|
|
265
|
+
[east, north]
|
|
266
|
+
];
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Returns the bounding box of the viewport.
|
|
270
|
+
* @param [options]
|
|
271
|
+
* @param [options.z] - The altitude at which the bounds should be calculated.
|
|
272
|
+
* @returns {Array} an array of 4 points that define the visible region
|
|
273
|
+
*/
|
|
274
|
+
getBoundingRegion(options = {}) {
|
|
275
|
+
return getBounds(this, options.z || 0);
|
|
276
|
+
}
|
|
277
|
+
// DEPRECATED
|
|
278
|
+
/** @deprecated Legacy method name */
|
|
279
|
+
getLocationAtPoint({ lngLat, pos }) {
|
|
280
|
+
return this.getMapCenterByLngLatPosition({ lngLat, pos });
|
|
281
|
+
}
|
|
282
|
+
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Utilities for perspective-enabled Web Mercator projections",
|
|
4
4
|
"author": "Uber Technologies, Inc.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "3.
|
|
6
|
+
"version": "3.6.0-alpha.1",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"webgl",
|
|
9
9
|
"javascript",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"type": "git",
|
|
19
19
|
"url": "https://github.com/uber-web/math.gl.git"
|
|
20
20
|
},
|
|
21
|
-
"types": "
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
22
22
|
"main": "dist/es5/index.js",
|
|
23
23
|
"module": "dist/esm/index.js",
|
|
24
24
|
"files": [
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@babel/runtime": "^7.12.0",
|
|
30
|
-
"gl-matrix": "
|
|
30
|
+
"gl-matrix": "~3.3.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"mapbox-gl": "^1.0.0",
|
|
34
34
|
"mock-browser": "^0.92.14"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "45386981340895bf201c2ebb76257c9bbffd8ff6"
|
|
37
37
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Replacement for the external assert method to reduce bundle size
|
|
2
2
|
// Note: We don't use the second "message" argument in calling code,
|
|
3
3
|
// so no need to support it here
|
|
4
|
-
export default function assert(condition, message) {
|
|
4
|
+
export default function assert(condition: unknown, message?: string): void {
|
|
5
5
|
if (!condition) {
|
|
6
6
|
throw new Error(message || '@math.gl/web-mercator: assertion failed.');
|
|
7
7
|
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import WebMercatorViewport from './web-mercator-viewport';
|
|
2
|
+
import assert from './assert';
|
|
3
|
+
import {log2} from './math-utils';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options for fitBounds
|
|
7
|
+
* @param width - viewport width
|
|
8
|
+
* @param height - viewport height
|
|
9
|
+
* @param bounds - [[lon, lat], [lon, lat]]
|
|
10
|
+
* @param minExtent - The width/height of the bounded area will never be smaller than this
|
|
11
|
+
* @param padding - The amount of padding in pixels
|
|
12
|
+
* to add to the given bounds. Can also be an object with top, bottom, left and right
|
|
13
|
+
* properties defining the padding.
|
|
14
|
+
* @param options.offset= - The center of the given bounds relative to the map's center,
|
|
15
|
+
*/
|
|
16
|
+
export type FitBoundsOptions = {
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
bounds: [[number, number], [number, number]];
|
|
20
|
+
minExtent?: number; // 0.01 would be about 1000 meters (degree is ~110KM)
|
|
21
|
+
maxZoom?: number; // ~x4,000,000 => About 10 meter extents
|
|
22
|
+
// options
|
|
23
|
+
padding?: number | Padding;
|
|
24
|
+
offset?: number[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* An object describing the padding to add to the bounds.
|
|
29
|
+
* @property top - Padding from top in pixels to add to the given bounds
|
|
30
|
+
* @property bottom - Padding from bottom in pixels to add to the given bounds
|
|
31
|
+
* @property left - Padding from left in pixels to add to the given bounds
|
|
32
|
+
* @property right - Padding from right in pixels to add to the given bounds
|
|
33
|
+
*/
|
|
34
|
+
export type Padding = {
|
|
35
|
+
top: number;
|
|
36
|
+
bottom: number;
|
|
37
|
+
left: number;
|
|
38
|
+
right: number;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type Bounds = {
|
|
42
|
+
longitude: number;
|
|
43
|
+
latitude: number;
|
|
44
|
+
zoom: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns map settings {latitude, longitude, zoom}
|
|
49
|
+
* that will contain the provided corners within the provided width.
|
|
50
|
+
*
|
|
51
|
+
* > _Note: Only supports non-perspective mode._
|
|
52
|
+
*
|
|
53
|
+
* @param options fit bounds parameters
|
|
54
|
+
* @returns - latitude, longitude and zoom
|
|
55
|
+
*/
|
|
56
|
+
export default function fitBounds(options: FitBoundsOptions): Bounds {
|
|
57
|
+
const {
|
|
58
|
+
width,
|
|
59
|
+
height,
|
|
60
|
+
bounds,
|
|
61
|
+
minExtent = 0, // 0.01 would be about 1000 meters (degree is ~110KM)
|
|
62
|
+
maxZoom = 24, // ~x4,000,000 => About 10 meter extents
|
|
63
|
+
offset = [0, 0]
|
|
64
|
+
} = options;
|
|
65
|
+
|
|
66
|
+
const [[west, south], [east, north]] = bounds;
|
|
67
|
+
const padding = getPaddingObject(options.padding);
|
|
68
|
+
|
|
69
|
+
const viewport = new WebMercatorViewport({
|
|
70
|
+
width,
|
|
71
|
+
height,
|
|
72
|
+
longitude: 0,
|
|
73
|
+
latitude: 0,
|
|
74
|
+
zoom: 0
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const nw = viewport.project([west, north]);
|
|
78
|
+
const se = viewport.project([east, south]);
|
|
79
|
+
|
|
80
|
+
// width/height on the Web Mercator plane
|
|
81
|
+
const size = [
|
|
82
|
+
Math.max(Math.abs(se[0] - nw[0]), minExtent),
|
|
83
|
+
Math.max(Math.abs(se[1] - nw[1]), minExtent)
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
const targetSize = [
|
|
87
|
+
width - padding.left - padding.right - Math.abs(offset[0]) * 2,
|
|
88
|
+
height - padding.top - padding.bottom - Math.abs(offset[1]) * 2
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
assert(targetSize[0] > 0 && targetSize[1] > 0);
|
|
92
|
+
|
|
93
|
+
// scale = screen pixels per unit on the Web Mercator plane
|
|
94
|
+
const scaleX = targetSize[0] / size[0];
|
|
95
|
+
const scaleY = targetSize[1] / size[1];
|
|
96
|
+
|
|
97
|
+
// Find how much we need to shift the center
|
|
98
|
+
const offsetX = (padding.right - padding.left) / 2 / scaleX;
|
|
99
|
+
const offsetY = (padding.bottom - padding.top) / 2 / scaleY;
|
|
100
|
+
|
|
101
|
+
const center = [(se[0] + nw[0]) / 2 + offsetX, (se[1] + nw[1]) / 2 + offsetY];
|
|
102
|
+
|
|
103
|
+
const centerLngLat = viewport.unproject(center);
|
|
104
|
+
const zoom = Math.min(maxZoom, viewport.zoom + log2(Math.abs(Math.min(scaleX, scaleY))));
|
|
105
|
+
|
|
106
|
+
assert(Number.isFinite(zoom));
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
longitude: centerLngLat[0],
|
|
110
|
+
latitude: centerLngLat[1],
|
|
111
|
+
zoom
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Helpers
|
|
116
|
+
function getPaddingObject(padding: number | Padding = 0): Padding {
|
|
117
|
+
if (typeof padding === 'number') {
|
|
118
|
+
return {
|
|
119
|
+
top: padding,
|
|
120
|
+
bottom: padding,
|
|
121
|
+
left: padding,
|
|
122
|
+
right: padding
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Make sure all the required properties are set
|
|
127
|
+
assert(
|
|
128
|
+
Number.isFinite(padding.top) &&
|
|
129
|
+
Number.isFinite(padding.bottom) &&
|
|
130
|
+
Number.isFinite(padding.left) &&
|
|
131
|
+
Number.isFinite(padding.right)
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
return padding;
|
|
135
|
+
}
|
|
@@ -10,29 +10,47 @@ const DEFAULT_OPTS = {
|
|
|
10
10
|
// screenSpeed and maxDuration are used only if specified
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
export type FlytoTransitionOptions = {
|
|
14
|
+
curve?: number;
|
|
15
|
+
speed?: number;
|
|
16
|
+
screenSpeed?: number;
|
|
17
|
+
maxDuration?: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type ViewportProps = {
|
|
21
|
+
longitude: number;
|
|
22
|
+
latitude: number;
|
|
23
|
+
zoom: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
13
26
|
/**
|
|
14
27
|
* mapbox-gl-js flyTo : https://www.mapbox.com/mapbox-gl-js/api/#map#flyto.
|
|
15
28
|
* It implements “Smooth and efficient zooming and panning.” algorithm by
|
|
16
29
|
* "Jarke J. van Wijk and Wim A.A. Nuij"
|
|
17
30
|
*/
|
|
18
|
-
export default function flyToViewport(
|
|
31
|
+
export default function flyToViewport(
|
|
32
|
+
startProps: ViewportProps,
|
|
33
|
+
endProps: ViewportProps,
|
|
34
|
+
t: number,
|
|
35
|
+
options?: FlytoTransitionOptions
|
|
36
|
+
): ViewportProps {
|
|
19
37
|
// Equations from above paper are referred where needed.
|
|
20
38
|
|
|
21
|
-
const viewport = {};
|
|
22
|
-
|
|
23
39
|
const {startZoom, startCenterXY, uDelta, w0, u1, S, rho, rho2, r0} = getFlyToTransitionParams(
|
|
24
40
|
startProps,
|
|
25
41
|
endProps,
|
|
26
|
-
|
|
42
|
+
options
|
|
27
43
|
);
|
|
28
44
|
|
|
29
45
|
// If change in center is too small, do linear interpolaiton.
|
|
30
46
|
if (u1 < EPSILON) {
|
|
47
|
+
const viewport = {};
|
|
31
48
|
for (const key of VIEWPORT_TRANSITION_PROPS) {
|
|
32
49
|
const startValue = startProps[key];
|
|
33
50
|
const endValue = endProps[key];
|
|
34
51
|
viewport[key] = lerp(startValue, endValue, t);
|
|
35
52
|
}
|
|
53
|
+
// @ts-expect-error
|
|
36
54
|
return viewport;
|
|
37
55
|
}
|
|
38
56
|
|
|
@@ -48,15 +66,20 @@ export default function flyToViewport(startProps, endProps, t, opts = {}) {
|
|
|
48
66
|
vec2.add(newCenterWorld, newCenterWorld, startCenterXY);
|
|
49
67
|
|
|
50
68
|
const newCenter = worldToLngLat(newCenterWorld);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
69
|
+
return {
|
|
70
|
+
longitude: newCenter[0],
|
|
71
|
+
latitude: newCenter[1],
|
|
72
|
+
zoom: newZoom
|
|
73
|
+
};
|
|
55
74
|
}
|
|
56
75
|
|
|
57
76
|
// returns transition duration in milliseconds
|
|
58
|
-
export function getFlyToDuration(
|
|
59
|
-
|
|
77
|
+
export function getFlyToDuration(
|
|
78
|
+
startProps: ViewportProps,
|
|
79
|
+
endProps: ViewportProps,
|
|
80
|
+
options?: FlytoTransitionOptions
|
|
81
|
+
): number {
|
|
82
|
+
const opts = {...DEFAULT_OPTS, ...options};
|
|
60
83
|
const {screenSpeed, speed, maxDuration} = opts;
|
|
61
84
|
const {S, rho} = getFlyToTransitionParams(startProps, endProps, opts);
|
|
62
85
|
const length = 1000 * S;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {worldToLngLat} from './web-mercator-utils';
|
|
2
2
|
import * as vec2 from 'gl-matrix/vec2';
|
|
3
3
|
import {transformVector} from './math-utils';
|
|
4
|
+
import type WebMercatorViewport from './web-mercator-viewport';
|
|
4
5
|
|
|
5
6
|
const DEGREES_TO_RADIANS = Math.PI / 180;
|
|
6
7
|
|
|
@@ -9,7 +10,8 @@ const DEGREES_TO_RADIANS = Math.PI / 180;
|
|
|
9
10
|
* @param {WebMercatorViewport} viewport
|
|
10
11
|
* @param {Number} z - elevation in meters
|
|
11
12
|
*/
|
|
12
|
-
export default function getBounds(viewport, z = 0) {
|
|
13
|
+
export default function getBounds(viewport: WebMercatorViewport, z: number = 0): number[][] {
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
13
15
|
const {width, height, unproject} = viewport;
|
|
14
16
|
const unprojectOps = {targetZ: z};
|
|
15
17
|
const bottomLeft = unproject([0, height], unprojectOps);
|
|
@@ -41,7 +43,7 @@ export default function getBounds(viewport, z = 0) {
|
|
|
41
43
|
* @param {Number} x - projected x in screen space
|
|
42
44
|
* @param {Number} targetZ - the elevation of the point in meters
|
|
43
45
|
*/
|
|
44
|
-
function unprojectOnFarPlane(viewport, x, targetZ) {
|
|
46
|
+
function unprojectOnFarPlane(viewport: WebMercatorViewport, x: number, targetZ) {
|
|
45
47
|
const {pixelUnprojectionMatrix} = viewport;
|
|
46
48
|
const coord0 = transformVector(pixelUnprojectionMatrix, [x, 0, 1, 1]);
|
|
47
49
|
const coord1 = transformVector(pixelUnprojectionMatrix, [x, viewport.height, 1, 1]);
|
|
File without changes
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {transformMat4, scale} from 'gl-matrix/vec4';
|
|
2
2
|
|
|
3
3
|
// Helper, avoids low-precision 32 bit matrices from gl-matrix mat4.create()
|
|
4
|
-
export function createMat4() {
|
|
4
|
+
export function createMat4(): number[] {
|
|
5
5
|
return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
// Transforms a vec4 with a projection matrix
|
|
9
9
|
export function transformVector(matrix, vector) {
|
|
10
|
-
const result =
|
|
11
|
-
|
|
10
|
+
const result = transformMat4([], vector, matrix);
|
|
11
|
+
scale(result, result, 1 / result[3]);
|
|
12
12
|
return result;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export function mod(value, divisor) {
|
|
15
|
+
export function mod(value: number, divisor: number): number {
|
|
16
16
|
const modulus = value % divisor;
|
|
17
17
|
return modulus < 0 ? divisor + modulus : modulus;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export function lerp(start, end, step) {
|
|
20
|
+
export function lerp(start: number, end: number, step: number): number {
|
|
21
21
|
return step * end + (1 - step) * start;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
function ieLog2(x) {
|
|
24
|
+
function ieLog2(x: number): number {
|
|
25
25
|
return Math.log(x) * Math.LOG2E;
|
|
26
26
|
}
|
|
27
27
|
// Handle missing log2 in IE 11
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {worldToLngLat} from './web-mercator-utils';
|
|
2
|
+
import {mod, log2} from './math-utils';
|
|
3
|
+
|
|
4
|
+
// defined by mapbox-gl
|
|
5
|
+
const TILE_SIZE = 512;
|
|
6
|
+
|
|
7
|
+
/** Description of viewport */
|
|
8
|
+
type ViewportProps = {
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
longitude: number;
|
|
12
|
+
latitude: number;
|
|
13
|
+
zoom: number;
|
|
14
|
+
pitch?: number;
|
|
15
|
+
bearing?: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Apply mathematical constraints to viewport props
|
|
20
|
+
* @param props
|
|
21
|
+
*/
|
|
22
|
+
// eslint-disable-next-line complexity
|
|
23
|
+
export default function normalizeViewportProps(props: ViewportProps): ViewportProps {
|
|
24
|
+
const {width, height, pitch = 0} = props;
|
|
25
|
+
let {longitude, latitude, zoom, bearing = 0} = props;
|
|
26
|
+
|
|
27
|
+
// Normalize degrees
|
|
28
|
+
if (longitude < -180 || longitude > 180) {
|
|
29
|
+
longitude = mod(longitude + 180, 360) - 180;
|
|
30
|
+
}
|
|
31
|
+
if (bearing < -180 || bearing > 180) {
|
|
32
|
+
bearing = mod(bearing + 180, 360) - 180;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Constrain zoom and shift center at low zoom levels
|
|
36
|
+
const minZoom = log2(height / TILE_SIZE);
|
|
37
|
+
if (zoom <= minZoom) {
|
|
38
|
+
zoom = minZoom;
|
|
39
|
+
latitude = 0;
|
|
40
|
+
} else {
|
|
41
|
+
// Eliminate white space above and below the map
|
|
42
|
+
const halfHeightPixels = height / 2 / Math.pow(2, zoom);
|
|
43
|
+
const minLatitude = worldToLngLat([0, halfHeightPixels])[1];
|
|
44
|
+
if (latitude < minLatitude) {
|
|
45
|
+
latitude = minLatitude;
|
|
46
|
+
} else {
|
|
47
|
+
const maxLatitude = worldToLngLat([0, TILE_SIZE - halfHeightPixels])[1];
|
|
48
|
+
if (latitude > maxLatitude) {
|
|
49
|
+
latitude = maxLatitude;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {width, height, longitude, latitude, zoom, pitch, bearing};
|
|
55
|
+
}
|