@math.gl/web-mercator 4.0.0 → 4.1.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 +0 -1
- package/dist/assert.js +6 -4
- package/dist/fit-bounds.d.ts +17 -16
- package/dist/fit-bounds.js +56 -41
- package/dist/fly-to-viewport.d.ts +2 -3
- package/dist/fly-to-viewport.js +74 -97
- package/dist/get-bounds.d.ts +1 -2
- package/dist/get-bounds.js +44 -37
- package/dist/index.cjs +19 -57
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +9 -10
- package/dist/index.js +2 -1
- package/dist/math-utils.d.ts +0 -1
- package/dist/math-utils.js +12 -12
- package/dist/normalize-viewport-props.d.ts +1 -2
- package/dist/normalize-viewport-props.js +35 -49
- package/dist/web-mercator-utils.d.ts +2 -3
- package/dist/web-mercator-utils.js +273 -154
- package/dist/web-mercator-viewport.d.ts +3 -4
- package/dist/web-mercator-viewport.js +265 -256
- package/package.json +5 -5
- package/src/fit-bounds.ts +15 -14
- package/dist/assert.d.ts.map +0 -1
- package/dist/assert.js.map +0 -1
- package/dist/fit-bounds.d.ts.map +0 -1
- package/dist/fit-bounds.js.map +0 -1
- package/dist/fly-to-viewport.d.ts.map +0 -1
- package/dist/fly-to-viewport.js.map +0 -1
- package/dist/get-bounds.d.ts.map +0 -1
- package/dist/get-bounds.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/math-utils.d.ts.map +0 -1
- package/dist/math-utils.js.map +0 -1
- package/dist/normalize-viewport-props.d.ts.map +0 -1
- package/dist/normalize-viewport-props.js.map +0 -1
- package/dist/web-mercator-utils.d.ts.map +0 -1
- package/dist/web-mercator-utils.js.map +0 -1
- package/dist/web-mercator-viewport.d.ts.map +0 -1
- package/dist/web-mercator-viewport.js.map +0 -1
|
@@ -1,266 +1,275 @@
|
|
|
1
|
-
|
|
1
|
+
// View and Projection Matrix calculations for mapbox-js style map view properties
|
|
2
2
|
import { createMat4 } from "./math-utils.js";
|
|
3
3
|
import { zoomToScale, pixelsToWorld, lngLatToWorld, worldToLngLat, worldToPixels, altitudeToFovy, fovyToAltitude, DEFAULT_ALTITUDE, getProjectionMatrix, getDistanceScales, getViewMatrix } from "./web-mercator-utils.js";
|
|
4
4
|
import { fitBounds } from "./fit-bounds.js";
|
|
5
5
|
import { getBounds } from "./get-bounds.js";
|
|
6
6
|
import { mat4, vec2, vec3 } from '@math.gl/core';
|
|
7
|
+
/**
|
|
8
|
+
* The WebMercatorViewport class creates
|
|
9
|
+
* - view/projection matrices
|
|
10
|
+
* - "uniform values" (for shaders) from mercator params
|
|
11
|
+
*
|
|
12
|
+
* Note: Instances are immutable in the sense that they only have accessors.
|
|
13
|
+
* A new viewport instance should be created if any parameters have changed.
|
|
14
|
+
*/
|
|
7
15
|
export class WebMercatorViewport {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
16
|
+
/**
|
|
17
|
+
* @classdesc
|
|
18
|
+
* Creates view/projection matrices from mercator params
|
|
19
|
+
* Note: The Viewport is immutable in the sense that it only has accessors.
|
|
20
|
+
* A new viewport instance should be created if any parameters have changed.
|
|
21
|
+
*/
|
|
22
|
+
// eslint-disable-next-line max-statements
|
|
23
|
+
constructor(props = { width: 1, height: 1 }) {
|
|
24
|
+
/** Two viewports are equal if width and height are identical, and if
|
|
25
|
+
* their view and projection matrices are (approximately) equal.
|
|
26
|
+
*/
|
|
27
|
+
this.equals = (viewport) => {
|
|
28
|
+
if (!(viewport instanceof WebMercatorViewport)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return (viewport.width === this.width &&
|
|
32
|
+
viewport.height === this.height &&
|
|
33
|
+
mat4.equals(viewport.projectionMatrix, this.projectionMatrix) &&
|
|
34
|
+
mat4.equals(viewport.viewMatrix, this.viewMatrix));
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Projects xyz (possibly latitude and longitude) to pixel coordinates in window
|
|
38
|
+
* using viewport projection parameters
|
|
39
|
+
* - [longitude, latitude] to [x, y]
|
|
40
|
+
* - [longitude, latitude, Z] => [x, y, z]
|
|
41
|
+
* Note: By default, returns top-left coordinates for canvas/SVG type render
|
|
42
|
+
*
|
|
43
|
+
* @param lngLatZ - [lng, lat] or [lng, lat, Z]
|
|
44
|
+
* @param options - options
|
|
45
|
+
* @param options.topLeft=true - Whether projected coords are top left
|
|
46
|
+
* @return - screen coordinates [x, y] or [x, y, z], z as pixel depth
|
|
47
|
+
*/
|
|
48
|
+
this.project = (lngLatZ, options = {}) => {
|
|
49
|
+
const { topLeft = true } = options;
|
|
50
|
+
const worldPosition = this.projectPosition(lngLatZ);
|
|
51
|
+
const coord = worldToPixels(worldPosition, this.pixelProjectionMatrix);
|
|
52
|
+
const [x, y] = coord;
|
|
53
|
+
const y2 = topLeft ? y : this.height - y;
|
|
54
|
+
return lngLatZ.length === 2 ? [x, y2] : [x, y2, coord[2]];
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Unproject pixel coordinates on screen onto world coordinates, possibly `[lon, lat]` on map.
|
|
58
|
+
*
|
|
59
|
+
* - [x, y] => [lng, lat]
|
|
60
|
+
* - [x, y, z] => [lng, lat, Z]
|
|
61
|
+
*
|
|
62
|
+
* @param xyz - screen coordinates, z as pixel depth
|
|
63
|
+
* @param options - options
|
|
64
|
+
* @param options.topLeft=true - Whether projected coords are top left
|
|
65
|
+
* @param options.targetZ=0 - If pixel depth is unknown, targetZ is used as
|
|
66
|
+
* the elevation plane to unproject onto
|
|
67
|
+
* @return - [lng, lat, Z] or [X, Y, Z]
|
|
68
|
+
*/
|
|
69
|
+
this.unproject = (xyz, options = {}) => {
|
|
70
|
+
const { topLeft = true, targetZ = undefined } = options;
|
|
71
|
+
const [x, y, z] = xyz;
|
|
72
|
+
const y2 = topLeft ? y : this.height - y;
|
|
73
|
+
const targetZWorld = targetZ && targetZ * this.distanceScales.unitsPerMeter[2];
|
|
74
|
+
const coord = pixelsToWorld([x, y2, z], this.pixelUnprojectionMatrix, targetZWorld);
|
|
75
|
+
const [X, Y, Z] = this.unprojectPosition(coord);
|
|
76
|
+
if (Number.isFinite(z)) {
|
|
77
|
+
return [X, Y, Z];
|
|
78
|
+
}
|
|
79
|
+
return Number.isFinite(targetZ) ? [X, Y, targetZ] : [X, Y];
|
|
80
|
+
};
|
|
81
|
+
// NON_LINEAR PROJECTION HOOKS
|
|
82
|
+
// Used for web meractor projection
|
|
83
|
+
this.projectPosition = (xyz) => {
|
|
84
|
+
const [X, Y] = lngLatToWorld(xyz);
|
|
85
|
+
const Z = (xyz[2] || 0) * this.distanceScales.unitsPerMeter[2];
|
|
86
|
+
return [X, Y, Z];
|
|
87
|
+
};
|
|
88
|
+
this.unprojectPosition = (xyz) => {
|
|
89
|
+
const [X, Y] = worldToLngLat(xyz);
|
|
90
|
+
const Z = (xyz[2] || 0) * this.distanceScales.metersPerUnit[2];
|
|
91
|
+
return [X, Y, Z];
|
|
92
|
+
};
|
|
93
|
+
let {
|
|
94
|
+
// Map state
|
|
95
|
+
width, height, altitude = null, fovy = null } = props;
|
|
96
|
+
const { latitude = 0, longitude = 0, zoom = 0, pitch = 0, bearing = 0, position = null, nearZMultiplier = 0.02, farZMultiplier = 1.01 } = props;
|
|
97
|
+
// Silently allow apps to send in 0,0 to facilitate isomorphic render etc
|
|
98
|
+
width = width || 1;
|
|
99
|
+
height = height || 1;
|
|
100
|
+
// `fovy` & `altitude` are independent parameters, one for the
|
|
101
|
+
// projection and the latter for the view matrix. In the past,
|
|
102
|
+
// the `fovy` was always derived from the `altitude`
|
|
103
|
+
if (fovy === null && altitude === null) {
|
|
104
|
+
altitude = DEFAULT_ALTITUDE;
|
|
105
|
+
fovy = altitudeToFovy(altitude);
|
|
106
|
+
}
|
|
107
|
+
else if (fovy === null) {
|
|
108
|
+
fovy = altitudeToFovy(altitude);
|
|
109
|
+
}
|
|
110
|
+
else if (altitude === null) {
|
|
111
|
+
altitude = fovyToAltitude(fovy);
|
|
112
|
+
}
|
|
113
|
+
const scale = zoomToScale(zoom);
|
|
114
|
+
// Altitude - prevent division by 0
|
|
115
|
+
// TODO - just throw an Error instead?
|
|
116
|
+
altitude = Math.max(0.75, altitude);
|
|
117
|
+
const distanceScales = getDistanceScales({ longitude, latitude });
|
|
118
|
+
const center = lngLatToWorld([longitude, latitude]);
|
|
119
|
+
center.push(0);
|
|
120
|
+
if (position) {
|
|
121
|
+
vec3.add(center, center, vec3.mul([], position, distanceScales.unitsPerMeter));
|
|
122
|
+
}
|
|
123
|
+
this.projectionMatrix = getProjectionMatrix({
|
|
124
|
+
width,
|
|
125
|
+
height,
|
|
126
|
+
scale,
|
|
127
|
+
center,
|
|
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);
|
|
123
157
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
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;
|
|
136
187
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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 });
|
|
194
274
|
}
|
|
195
|
-
|
|
196
|
-
this.pixelProjectionMatrix = m;
|
|
197
|
-
this.pixelUnprojectionMatrix = mInverse;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
projectFlat(lngLat) {
|
|
201
|
-
return lngLatToWorld(lngLat);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
unprojectFlat(xy) {
|
|
205
|
-
return worldToLngLat(xy);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
getMapCenterByLngLatPosition({
|
|
209
|
-
lngLat,
|
|
210
|
-
pos
|
|
211
|
-
}) {
|
|
212
|
-
const fromLocation = pixelsToWorld(pos, this.pixelUnprojectionMatrix);
|
|
213
|
-
const toLocation = lngLatToWorld(lngLat);
|
|
214
|
-
const translate = vec2.add([], toLocation, vec2.negate([], fromLocation));
|
|
215
|
-
const newCenter = vec2.add([], this.center, translate);
|
|
216
|
-
return worldToLngLat(newCenter);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
fitBounds(bounds, options = {}) {
|
|
220
|
-
const {
|
|
221
|
-
width,
|
|
222
|
-
height
|
|
223
|
-
} = this;
|
|
224
|
-
const {
|
|
225
|
-
longitude,
|
|
226
|
-
latitude,
|
|
227
|
-
zoom
|
|
228
|
-
} = fitBounds(Object.assign({
|
|
229
|
-
width,
|
|
230
|
-
height,
|
|
231
|
-
bounds
|
|
232
|
-
}, options));
|
|
233
|
-
return new WebMercatorViewport({
|
|
234
|
-
width,
|
|
235
|
-
height,
|
|
236
|
-
longitude,
|
|
237
|
-
latitude,
|
|
238
|
-
zoom
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
getBounds(options) {
|
|
243
|
-
const corners = this.getBoundingRegion(options);
|
|
244
|
-
const west = Math.min(...corners.map(p => p[0]));
|
|
245
|
-
const east = Math.max(...corners.map(p => p[0]));
|
|
246
|
-
const south = Math.min(...corners.map(p => p[1]));
|
|
247
|
-
const north = Math.max(...corners.map(p => p[1]));
|
|
248
|
-
return [[west, south], [east, north]];
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
getBoundingRegion(options = {}) {
|
|
252
|
-
return getBounds(this, options.z || 0);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
getLocationAtPoint({
|
|
256
|
-
lngLat,
|
|
257
|
-
pos
|
|
258
|
-
}) {
|
|
259
|
-
return this.getMapCenterByLngLatPosition({
|
|
260
|
-
lngLat,
|
|
261
|
-
pos
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
|
|
265
275
|
}
|
|
266
|
-
//# sourceMappingURL=web-mercator-viewport.js.map
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"author": "Uber Technologies, Inc.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"version": "4.0.
|
|
7
|
+
"version": "4.1.0-alpha.1",
|
|
8
8
|
"keywords": [
|
|
9
9
|
"webgl",
|
|
10
10
|
"javascript",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"module": "dist/index.js",
|
|
25
25
|
"exports": {
|
|
26
26
|
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
27
28
|
"import": "./dist/index.js",
|
|
28
|
-
"require": "./dist/index.cjs"
|
|
29
|
-
"types": "./dist/index.d.ts"
|
|
29
|
+
"require": "./dist/index.cjs"
|
|
30
30
|
}
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
@@ -34,10 +34,10 @@
|
|
|
34
34
|
"src"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@
|
|
37
|
+
"@math.gl/core": "4.1.0-alpha.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"mock-browser": "^0.92.14"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "1a4dbbc6ab46271459d610411a7c644e45135c2c"
|
|
43
43
|
}
|
package/src/fit-bounds.ts
CHANGED
|
@@ -4,37 +4,38 @@ import {MAX_LATITUDE, lngLatToWorld, worldToLngLat} from './web-mercator-utils';
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
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
7
|
*/
|
|
16
8
|
export type FitBoundsOptions = {
|
|
9
|
+
/** viewport width */
|
|
17
10
|
width: number;
|
|
11
|
+
/** viewport height */
|
|
18
12
|
height: number;
|
|
13
|
+
/** [[lon, lat], [lon, lat]] */
|
|
19
14
|
bounds: [[number, number], [number, number]];
|
|
20
|
-
|
|
15
|
+
/** The width/height of the bounded area will never be smaller than this. 0.01 would be about 1000 meters (degree is ~110KM) */
|
|
16
|
+
minExtent?: number;
|
|
17
|
+
/** The maximum zoom level to fit the bounds within. */
|
|
21
18
|
maxZoom?: number; // ~x4,000,000 => About 10 meter extents
|
|
22
|
-
|
|
19
|
+
/**
|
|
20
|
+
* padding - The amount of padding in pixels to add to the given bounds.
|
|
21
|
+
* Can also be an object with top, bottom, left and right properties defining the padding.
|
|
22
|
+
*/
|
|
23
23
|
padding?: number | Padding;
|
|
24
|
+
/** The center of the given bounds relative to the map's center, */
|
|
24
25
|
offset?: number[];
|
|
25
26
|
};
|
|
26
27
|
|
|
27
28
|
/**
|
|
28
29
|
* 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
30
|
*/
|
|
34
31
|
export type Padding = {
|
|
32
|
+
/** Padding from top in pixels to add to the given bounds */
|
|
35
33
|
top: number;
|
|
34
|
+
/** Padding from bottom in pixels to add to the given bounds */
|
|
36
35
|
bottom: number;
|
|
36
|
+
/** Padding from left in pixels to add to the given bounds */
|
|
37
37
|
left: number;
|
|
38
|
+
/** Padding from right in pixels to add to the given bounds */
|
|
38
39
|
right: number;
|
|
39
40
|
};
|
|
40
41
|
|
package/dist/assert.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAGA,wBAAgB,MAAM,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAIjE"}
|
package/dist/assert.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/assert.ts"],"names":["assert","condition","message","Error"],"mappings":"AAGA,OAAO,SAASA,MAAT,CAAgBC,SAAhB,EAAoCC,OAApC,EAA4D;AACjE,MAAI,CAACD,SAAL,EAAgB;AACd,UAAM,IAAIE,KAAJ,CAAUD,OAAO,IAAI,0CAArB,CAAN;AACD;AACF","sourcesContent":["// Replacement for the external assert method to reduce bundle size\n// Note: We don't use the second \"message\" argument in calling code,\n// so no need to support it here\nexport function assert(condition: unknown, message?: string): void {\n if (!condition) {\n throw new Error(message || '@math.gl/web-mercator: assertion failed.');\n }\n}\n"],"file":"assert.js"}
|
package/dist/fit-bounds.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fit-bounds.d.ts","sourceRoot":"","sources":["../src/fit-bounds.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;GAUG;AACH,oBAAY,gBAAgB,GAAG;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IAC7C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IAC3B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB,CAAC;AAEF;;;;;;GAMG;AACH,oBAAY,OAAO,GAAG;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,aAAK,aAAa,GAAG;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,aAAa,CAiDlE"}
|
package/dist/fit-bounds.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/fit-bounds.ts"],"names":["assert","log2","clamp","MAX_LATITUDE","lngLatToWorld","worldToLngLat","fitBounds","options","width","height","bounds","minExtent","maxZoom","offset","west","south","east","north","padding","getPaddingObject","nw","se","size","Math","max","abs","targetSize","left","right","top","bottom","scaleX","scaleY","offsetX","offsetY","center","centerLngLat","zoom","min","Number","isFinite","longitude","latitude"],"mappings":"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","sourcesContent":["import {assert} from './assert';\nimport {log2, clamp} from './math-utils';\nimport {MAX_LATITUDE, lngLatToWorld, worldToLngLat} from './web-mercator-utils';\n\n/**\n * Options for fitBounds\n * @param width - viewport width\n * @param height - viewport height\n * @param bounds - [[lon, lat], [lon, lat]]\n * @param minExtent - The width/height of the bounded area will never be smaller than this\n * @param padding - The amount of padding in pixels\n * to add to the given bounds. Can also be an object with top, bottom, left and right\n * properties defining the padding.\n * @param options.offset= - The center of the given bounds relative to the map's center,\n */\nexport type FitBoundsOptions = {\n width: number;\n height: number;\n bounds: [[number, number], [number, number]];\n minExtent?: number; // 0.01 would be about 1000 meters (degree is ~110KM)\n maxZoom?: number; // ~x4,000,000 => About 10 meter extents\n // options\n padding?: number | Padding;\n offset?: number[];\n};\n\n/**\n * An object describing the padding to add to the bounds.\n * @property top - Padding from top in pixels to add to the given bounds\n * @property bottom - Padding from bottom in pixels to add to the given bounds\n * @property left - Padding from left in pixels to add to the given bounds\n * @property right - Padding from right in pixels to add to the given bounds\n */\nexport type Padding = {\n top: number;\n bottom: number;\n left: number;\n right: number;\n};\n\ntype ViewportProps = {\n longitude: number;\n latitude: number;\n zoom: number;\n};\n\n/**\n * Returns map settings {latitude, longitude, zoom}\n * that will contain the provided corners within the provided width.\n *\n * > _Note: Only supports non-perspective mode._\n *\n * @param options fit bounds parameters\n * @returns - latitude, longitude and zoom\n */\nexport function fitBounds(options: FitBoundsOptions): ViewportProps {\n const {\n width,\n height,\n bounds,\n minExtent = 0, // 0.01 would be about 1000 meters (degree is ~110KM)\n maxZoom = 24, // ~x4,000,000 => About 10 meter extents\n offset = [0, 0]\n } = options;\n\n const [[west, south], [east, north]] = bounds;\n const padding = getPaddingObject(options.padding);\n\n const nw = lngLatToWorld([west, clamp(north, -MAX_LATITUDE, MAX_LATITUDE)]);\n const se = lngLatToWorld([east, clamp(south, -MAX_LATITUDE, MAX_LATITUDE)]);\n\n // width/height on the Web Mercator plane\n const size = [\n Math.max(Math.abs(se[0] - nw[0]), minExtent),\n Math.max(Math.abs(se[1] - nw[1]), minExtent)\n ];\n\n const targetSize = [\n width - padding.left - padding.right - Math.abs(offset[0]) * 2,\n height - padding.top - padding.bottom - Math.abs(offset[1]) * 2\n ];\n\n assert(targetSize[0] > 0 && targetSize[1] > 0);\n\n // scale = screen pixels per unit on the Web Mercator plane\n const scaleX = targetSize[0] / size[0];\n const scaleY = targetSize[1] / size[1];\n\n // Find how much we need to shift the center\n const offsetX = (padding.right - padding.left) / 2 / scaleX;\n const offsetY = (padding.top - padding.bottom) / 2 / scaleY;\n\n const center = [(se[0] + nw[0]) / 2 + offsetX, (se[1] + nw[1]) / 2 + offsetY];\n\n const centerLngLat = worldToLngLat(center);\n const zoom = Math.min(maxZoom, log2(Math.abs(Math.min(scaleX, scaleY))));\n\n assert(Number.isFinite(zoom));\n\n return {\n longitude: centerLngLat[0],\n latitude: centerLngLat[1],\n zoom\n };\n}\n\n// Helpers\nfunction getPaddingObject(padding: number | Padding = 0): Padding {\n if (typeof padding === 'number') {\n return {\n top: padding,\n bottom: padding,\n left: padding,\n right: padding\n };\n }\n\n // Make sure all the required properties are set\n assert(\n Number.isFinite(padding.top) &&\n Number.isFinite(padding.bottom) &&\n Number.isFinite(padding.left) &&\n Number.isFinite(padding.right)\n );\n\n return padding;\n}\n"],"file":"fit-bounds.js"}
|