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