@math.gl/web-mercator 4.0.0 → 4.0.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 +3 -4
- 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 +4 -7
- 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,195 +1,314 @@
|
|
|
1
|
+
// TODO - THE UTILITIES IN THIS FILE SHOULD BE IMPORTED FROM WEB-MERCATOR-VIEWPORT MODULE
|
|
1
2
|
import { createMat4, transformVector, clamp, log2 } from "./math-utils.js";
|
|
2
3
|
import { mat4, vec2, vec3 } from '@math.gl/core';
|
|
3
4
|
import { assert } from "./assert.js";
|
|
5
|
+
// CONSTANTS
|
|
4
6
|
const PI = Math.PI;
|
|
5
7
|
const PI_4 = PI / 4;
|
|
6
8
|
const DEGREES_TO_RADIANS = PI / 180;
|
|
7
9
|
const RADIANS_TO_DEGREES = 180 / PI;
|
|
8
10
|
const TILE_SIZE = 512;
|
|
11
|
+
// Average circumference (40075 km equatorial, 40007 km meridional)
|
|
9
12
|
const EARTH_CIRCUMFERENCE = 40.03e6;
|
|
13
|
+
// Latitude that makes a square world, 2 * atan(E ** PI) - PI / 2
|
|
10
14
|
export const MAX_LATITUDE = 85.051129;
|
|
15
|
+
// Mapbox default altitude
|
|
11
16
|
export const DEFAULT_ALTITUDE = 1.5;
|
|
17
|
+
/** Logarithimic zoom to linear scale **/
|
|
12
18
|
export function zoomToScale(zoom) {
|
|
13
|
-
|
|
19
|
+
return Math.pow(2, zoom);
|
|
14
20
|
}
|
|
21
|
+
/** Linear scale to logarithimic zoom **/
|
|
15
22
|
export function scaleToZoom(scale) {
|
|
16
|
-
|
|
23
|
+
return log2(scale);
|
|
17
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Project [lng,lat] on sphere onto [x,y] on 512*512 Mercator Zoom 0 tile.
|
|
27
|
+
* Performs the nonlinear part of the web mercator projection.
|
|
28
|
+
* Remaining projection is done with 4x4 matrices which also handles
|
|
29
|
+
* perspective.
|
|
30
|
+
*
|
|
31
|
+
* @param lngLat - [lng, lat] coordinates
|
|
32
|
+
* Specifies a point on the sphere to project onto the map.
|
|
33
|
+
* @return [x,y] coordinates.
|
|
34
|
+
*/
|
|
18
35
|
export function lngLatToWorld(lngLat) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
36
|
+
const [lng, lat] = lngLat;
|
|
37
|
+
assert(Number.isFinite(lng));
|
|
38
|
+
assert(Number.isFinite(lat) && lat >= -90 && lat <= 90, 'invalid latitude');
|
|
39
|
+
const lambda2 = lng * DEGREES_TO_RADIANS;
|
|
40
|
+
const phi2 = lat * DEGREES_TO_RADIANS;
|
|
41
|
+
const x = (TILE_SIZE * (lambda2 + PI)) / (2 * PI);
|
|
42
|
+
const y = (TILE_SIZE * (PI + Math.log(Math.tan(PI_4 + phi2 * 0.5)))) / (2 * PI);
|
|
43
|
+
return [x, y];
|
|
27
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Unproject world point [x,y] on map onto {lat, lon} on sphere
|
|
47
|
+
*
|
|
48
|
+
* @param xy - array with [x,y] members
|
|
49
|
+
* representing point on projected map plane
|
|
50
|
+
* @return - array with [x,y] of point on sphere.
|
|
51
|
+
* Has toArray method if you need a GeoJSON Array.
|
|
52
|
+
* Per cartographic tradition, lat and lon are specified as degrees.
|
|
53
|
+
*/
|
|
28
54
|
export function worldToLngLat(xy) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
55
|
+
const [x, y] = xy;
|
|
56
|
+
const lambda2 = (x / TILE_SIZE) * (2 * PI) - PI;
|
|
57
|
+
const phi2 = 2 * (Math.atan(Math.exp((y / TILE_SIZE) * (2 * PI) - PI)) - PI_4);
|
|
58
|
+
return [lambda2 * RADIANS_TO_DEGREES, phi2 * RADIANS_TO_DEGREES];
|
|
33
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Returns the zoom level that gives a 1 meter pixel at a certain latitude
|
|
62
|
+
* 1 = C*cos(y)/2^z/TILE_SIZE = C*cos(y)/2^(z+9)
|
|
63
|
+
*/
|
|
34
64
|
export function getMeterZoom(options) {
|
|
35
|
-
|
|
36
|
-
latitude
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const latCosine = Math.cos(latitude * DEGREES_TO_RADIANS);
|
|
40
|
-
return scaleToZoom(EARTH_CIRCUMFERENCE * latCosine) - 9;
|
|
65
|
+
const { latitude } = options;
|
|
66
|
+
assert(Number.isFinite(latitude));
|
|
67
|
+
const latCosine = Math.cos(latitude * DEGREES_TO_RADIANS);
|
|
68
|
+
return scaleToZoom(EARTH_CIRCUMFERENCE * latCosine) - 9;
|
|
41
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Calculate the conversion from meter to common units at a given latitude
|
|
72
|
+
* This is a cheaper version of `getDistanceScales`
|
|
73
|
+
* @param latitude center latitude in degrees
|
|
74
|
+
* @returns common units per meter
|
|
75
|
+
*/
|
|
42
76
|
export function unitsPerMeter(latitude) {
|
|
43
|
-
|
|
44
|
-
|
|
77
|
+
const latCosine = Math.cos(latitude * DEGREES_TO_RADIANS);
|
|
78
|
+
return TILE_SIZE / EARTH_CIRCUMFERENCE / latCosine;
|
|
45
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Calculate distance scales in meters around current lat/lon, both for
|
|
82
|
+
* degrees and pixels.
|
|
83
|
+
* In mercator projection mode, the distance scales vary significantly
|
|
84
|
+
* with latitude.
|
|
85
|
+
*/
|
|
46
86
|
export function getDistanceScales(options) {
|
|
47
|
-
|
|
48
|
-
latitude
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
result
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
87
|
+
const { latitude, longitude, highPrecision = false } = options;
|
|
88
|
+
assert(Number.isFinite(latitude) && Number.isFinite(longitude));
|
|
89
|
+
const worldSize = TILE_SIZE;
|
|
90
|
+
const latCosine = Math.cos(latitude * DEGREES_TO_RADIANS);
|
|
91
|
+
/**
|
|
92
|
+
* Number of pixels occupied by one degree longitude around current lat/lon:
|
|
93
|
+
unitsPerDegreeX = d(lngLatToWorld([lng, lat])[0])/d(lng)
|
|
94
|
+
= scale * TILE_SIZE * DEGREES_TO_RADIANS / (2 * PI)
|
|
95
|
+
unitsPerDegreeY = d(lngLatToWorld([lng, lat])[1])/d(lat)
|
|
96
|
+
= -scale * TILE_SIZE * DEGREES_TO_RADIANS / cos(lat * DEGREES_TO_RADIANS) / (2 * PI)
|
|
97
|
+
*/
|
|
98
|
+
const unitsPerDegreeX = worldSize / 360;
|
|
99
|
+
const unitsPerDegreeY = unitsPerDegreeX / latCosine;
|
|
100
|
+
/**
|
|
101
|
+
* Number of pixels occupied by one meter around current lat/lon:
|
|
102
|
+
*/
|
|
103
|
+
const altUnitsPerMeter = worldSize / EARTH_CIRCUMFERENCE / latCosine;
|
|
104
|
+
/**
|
|
105
|
+
* LngLat: longitude -> east and latitude -> north (bottom left)
|
|
106
|
+
* UTM meter offset: x -> east and y -> north (bottom left)
|
|
107
|
+
* World space: x -> east and y -> south (top left)
|
|
108
|
+
*
|
|
109
|
+
* Y needs to be flipped when converting delta degree/meter to delta pixels
|
|
110
|
+
*/
|
|
111
|
+
const result = {
|
|
112
|
+
unitsPerMeter: [altUnitsPerMeter, altUnitsPerMeter, altUnitsPerMeter],
|
|
113
|
+
metersPerUnit: [1 / altUnitsPerMeter, 1 / altUnitsPerMeter, 1 / altUnitsPerMeter],
|
|
114
|
+
unitsPerDegree: [unitsPerDegreeX, unitsPerDegreeY, altUnitsPerMeter],
|
|
115
|
+
degreesPerUnit: [1 / unitsPerDegreeX, 1 / unitsPerDegreeY, 1 / altUnitsPerMeter]
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Taylor series 2nd order for 1/latCosine
|
|
119
|
+
f'(a) * (x - a)
|
|
120
|
+
= d(1/cos(lat * DEGREES_TO_RADIANS))/d(lat) * dLat
|
|
121
|
+
= DEGREES_TO_RADIANS * tan(lat * DEGREES_TO_RADIANS) / cos(lat * DEGREES_TO_RADIANS) * dLat
|
|
122
|
+
*/
|
|
123
|
+
if (highPrecision) {
|
|
124
|
+
const latCosine2 = (DEGREES_TO_RADIANS * Math.tan(latitude * DEGREES_TO_RADIANS)) / latCosine;
|
|
125
|
+
const unitsPerDegreeY2 = (unitsPerDegreeX * latCosine2) / 2;
|
|
126
|
+
const altUnitsPerDegree2 = (worldSize / EARTH_CIRCUMFERENCE) * latCosine2;
|
|
127
|
+
const altUnitsPerMeter2 = (altUnitsPerDegree2 / unitsPerDegreeY) * altUnitsPerMeter;
|
|
128
|
+
result.unitsPerDegree2 = [0, unitsPerDegreeY2, altUnitsPerDegree2];
|
|
129
|
+
result.unitsPerMeter2 = [altUnitsPerMeter2, 0, altUnitsPerMeter2];
|
|
130
|
+
}
|
|
131
|
+
// Main results, used for converting meters to latlng deltas and scaling offsets
|
|
132
|
+
return result;
|
|
75
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Offset a lng/lat position by meterOffset (northing, easting)
|
|
136
|
+
*/
|
|
76
137
|
export function addMetersToLngLat(lngLatZ, xyz) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const newLngLat = worldToLngLat(worldspace);
|
|
91
|
-
const newZ = (z0 || 0) + (z || 0);
|
|
92
|
-
return Number.isFinite(z0) || Number.isFinite(z) ? [newLngLat[0], newLngLat[1], newZ] : newLngLat;
|
|
138
|
+
const [longitude, latitude, z0] = lngLatZ;
|
|
139
|
+
const [x, y, z] = xyz;
|
|
140
|
+
const { unitsPerMeter, unitsPerMeter2 } = getDistanceScales({
|
|
141
|
+
longitude,
|
|
142
|
+
latitude,
|
|
143
|
+
highPrecision: true
|
|
144
|
+
});
|
|
145
|
+
const worldspace = lngLatToWorld(lngLatZ);
|
|
146
|
+
worldspace[0] += x * (unitsPerMeter[0] + unitsPerMeter2[0] * y);
|
|
147
|
+
worldspace[1] += y * (unitsPerMeter[1] + unitsPerMeter2[1] * y);
|
|
148
|
+
const newLngLat = worldToLngLat(worldspace);
|
|
149
|
+
const newZ = (z0 || 0) + (z || 0);
|
|
150
|
+
return Number.isFinite(z0) || Number.isFinite(z) ? [newLngLat[0], newLngLat[1], newZ] : newLngLat;
|
|
93
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
*
|
|
154
|
+
* view and projection matrix creation is intentionally kept compatible with
|
|
155
|
+
* mapbox-gl's implementation to ensure that seamless interoperation
|
|
156
|
+
* with mapbox and react-map-gl. See: https://github.com/mapbox/mapbox-gl-js
|
|
157
|
+
*/
|
|
94
158
|
export function getViewMatrix(options) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
pitch,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
159
|
+
const {
|
|
160
|
+
// Viewport props
|
|
161
|
+
height, pitch, bearing, altitude,
|
|
162
|
+
// Pre-calculated parameters
|
|
163
|
+
scale, center } = options;
|
|
164
|
+
// VIEW MATRIX: PROJECTS MERCATOR WORLD COORDINATES
|
|
165
|
+
// Note that mercator world coordinates typically need to be flipped
|
|
166
|
+
//
|
|
167
|
+
// Note: As usual, matrix operation orders should be read in reverse
|
|
168
|
+
// since vectors will be multiplied from the right during transformation
|
|
169
|
+
const vm = createMat4();
|
|
170
|
+
// Move camera to altitude (along the pitch & bearing direction)
|
|
171
|
+
mat4.translate(vm, vm, [0, 0, -altitude]);
|
|
172
|
+
// Rotate by bearing, and then by pitch (which tilts the view)
|
|
173
|
+
mat4.rotateX(vm, vm, -pitch * DEGREES_TO_RADIANS);
|
|
174
|
+
mat4.rotateZ(vm, vm, bearing * DEGREES_TO_RADIANS);
|
|
175
|
+
const relativeScale = scale / height;
|
|
176
|
+
mat4.scale(vm, vm, [relativeScale, relativeScale, relativeScale]);
|
|
177
|
+
if (center) {
|
|
178
|
+
mat4.translate(vm, vm, vec3.negate([], center));
|
|
179
|
+
}
|
|
180
|
+
return vm;
|
|
115
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Calculates mapbox compatible projection matrix from parameters
|
|
184
|
+
*
|
|
185
|
+
* @param options.width Width of "viewport" or window
|
|
186
|
+
* @param options.height Height of "viewport" or window
|
|
187
|
+
* @param options.scale Scale at the current zoom
|
|
188
|
+
* @param options.center Offset of the target, vec3 in world space
|
|
189
|
+
* @param options.offset Offset of the focal point, vec2 in screen space
|
|
190
|
+
* @param options.pitch Camera angle in degrees (0 is straight down)
|
|
191
|
+
* @param options.fovy field of view in degrees
|
|
192
|
+
* @param options.altitude if provided, field of view is calculated using `altitudeToFovy()`
|
|
193
|
+
* @param options.nearZMultiplier control z buffer
|
|
194
|
+
* @param options.farZMultiplier control z buffer
|
|
195
|
+
* @returns project parameters object
|
|
196
|
+
*/
|
|
116
197
|
export function getProjectionParameters(options) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
altitude
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
return {
|
|
151
|
-
fov: fovRadians,
|
|
152
|
-
aspect: width / height,
|
|
153
|
-
focalDistance,
|
|
154
|
-
near: nearZMultiplier,
|
|
155
|
-
far: farZ
|
|
156
|
-
};
|
|
198
|
+
const { width, height, altitude, pitch = 0, offset, center, scale, nearZMultiplier = 1, farZMultiplier = 1 } = options;
|
|
199
|
+
let { fovy = altitudeToFovy(DEFAULT_ALTITUDE) } = options;
|
|
200
|
+
// For back-compatibility allow field of view to be
|
|
201
|
+
// derived from altitude
|
|
202
|
+
if (altitude !== undefined) {
|
|
203
|
+
fovy = altitudeToFovy(altitude);
|
|
204
|
+
}
|
|
205
|
+
const fovRadians = fovy * DEGREES_TO_RADIANS;
|
|
206
|
+
const pitchRadians = pitch * DEGREES_TO_RADIANS;
|
|
207
|
+
// Distance from camera to the target
|
|
208
|
+
const focalDistance = fovyToAltitude(fovy);
|
|
209
|
+
let cameraToSeaLevelDistance = focalDistance;
|
|
210
|
+
if (center) {
|
|
211
|
+
cameraToSeaLevelDistance += (center[2] * scale) / Math.cos(pitchRadians) / height;
|
|
212
|
+
}
|
|
213
|
+
const fovAboveCenter = fovRadians * (0.5 + (offset ? offset[1] : 0) / height);
|
|
214
|
+
// Find the distance from the center point to the center top
|
|
215
|
+
// in focal distance units using law of sines.
|
|
216
|
+
const topHalfSurfaceDistance = (Math.sin(fovAboveCenter) * cameraToSeaLevelDistance) /
|
|
217
|
+
Math.sin(clamp(Math.PI / 2 - pitchRadians - fovAboveCenter, 0.01, Math.PI - 0.01));
|
|
218
|
+
// Calculate z distance of the farthest fragment that should be rendered.
|
|
219
|
+
const furthestDistance = Math.sin(pitchRadians) * topHalfSurfaceDistance + cameraToSeaLevelDistance;
|
|
220
|
+
// Matches mapbox limit
|
|
221
|
+
const horizonDistance = cameraToSeaLevelDistance * 10;
|
|
222
|
+
// Calculate z value of the farthest fragment that should be rendered.
|
|
223
|
+
const farZ = Math.min(furthestDistance * farZMultiplier, horizonDistance);
|
|
224
|
+
return {
|
|
225
|
+
fov: fovRadians,
|
|
226
|
+
aspect: width / height,
|
|
227
|
+
focalDistance,
|
|
228
|
+
near: nearZMultiplier,
|
|
229
|
+
far: farZ
|
|
230
|
+
};
|
|
157
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* CALCULATE PROJECTION MATRIX: PROJECTS FROM CAMERA (VIEW) SPACE TO CLIPSPACE
|
|
234
|
+
*
|
|
235
|
+
* To match mapbox's z buffer:
|
|
236
|
+
* - \<= 0.28: nearZMultiplier: 0.1, farZmultiplier: 1
|
|
237
|
+
* - \>= 0.29: nearZMultiplier: 1 / height, farZMultiplier: 1.01
|
|
238
|
+
*
|
|
239
|
+
* @param options Viewport options
|
|
240
|
+
* @param options.width Width of "viewport" or window
|
|
241
|
+
* @param options.height Height of "viewport" or window
|
|
242
|
+
* @param options.scale Scale at the current zoom
|
|
243
|
+
* @param options.center Offset of the target, vec3 in world space
|
|
244
|
+
* @param options.offset Offset of the focal point, vec2 in screen space
|
|
245
|
+
* @param options.pitch Camera angle in degrees (0 is straight down)
|
|
246
|
+
* @param options.fovy field of view in degrees
|
|
247
|
+
* @param options.altitude if provided, field of view is calculated using `altitudeToFovy()`
|
|
248
|
+
* @param options.nearZMultiplier control z buffer
|
|
249
|
+
* @param options.farZMultiplier control z buffer
|
|
250
|
+
* @returns 4x4 projection matrix
|
|
251
|
+
*/
|
|
158
252
|
export function getProjectionMatrix(options) {
|
|
159
|
-
|
|
160
|
-
fov,
|
|
161
|
-
aspect,
|
|
162
|
-
near,
|
|
163
|
-
far
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return projectionMatrix;
|
|
253
|
+
const { fov, aspect, near, far } = getProjectionParameters(options);
|
|
254
|
+
const projectionMatrix = mat4.perspective([], fov, // fov in radians
|
|
255
|
+
aspect, // aspect ratio
|
|
256
|
+
near, // near plane
|
|
257
|
+
far // far plane
|
|
258
|
+
);
|
|
259
|
+
return projectionMatrix;
|
|
167
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
*
|
|
263
|
+
* Convert an altitude to field of view such that the
|
|
264
|
+
* focal distance is equal to the altitude
|
|
265
|
+
*
|
|
266
|
+
* @param altitude - altitude of camera in screen units
|
|
267
|
+
* @return fovy field of view in degrees
|
|
268
|
+
*/
|
|
168
269
|
export function altitudeToFovy(altitude) {
|
|
169
|
-
|
|
270
|
+
return 2 * Math.atan(0.5 / altitude) * RADIANS_TO_DEGREES;
|
|
170
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
*
|
|
274
|
+
* Convert an field of view such that the
|
|
275
|
+
* focal distance is equal to the altitude
|
|
276
|
+
*
|
|
277
|
+
* @param fovy - field of view in degrees
|
|
278
|
+
* @return altitude altitude of camera in screen units
|
|
279
|
+
*/
|
|
171
280
|
export function fovyToAltitude(fovy) {
|
|
172
|
-
|
|
281
|
+
return 0.5 / Math.tan(0.5 * fovy * DEGREES_TO_RADIANS);
|
|
173
282
|
}
|
|
283
|
+
// Project flat coordinates to pixels on screen.
|
|
174
284
|
export function worldToPixels(xyz, pixelProjectionMatrix) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
285
|
+
const [x, y, z = 0] = xyz;
|
|
286
|
+
assert(Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(z));
|
|
287
|
+
return transformVector(pixelProjectionMatrix, [x, y, z, 1]);
|
|
178
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* Unproject pixels on screen to flat coordinates.
|
|
291
|
+
*
|
|
292
|
+
* @param xyz - pixel coordinate on screen.
|
|
293
|
+
* @param pixelUnprojectionMatrix - unprojection matrix 4x4
|
|
294
|
+
* @param targetZ - if pixel coordinate does not have a 3rd component (depth),
|
|
295
|
+
* targetZ is used as the elevation plane to unproject onto
|
|
296
|
+
* @return [x, y, Z] flat coordinates on 512*512 Mercator Zoom 0 tile.
|
|
297
|
+
*/
|
|
179
298
|
export function pixelsToWorld(xyz, pixelUnprojectionMatrix, targetZ = 0) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
299
|
+
const [x, y, z] = xyz;
|
|
300
|
+
assert(Number.isFinite(x) && Number.isFinite(y), 'invalid pixel coordinate');
|
|
301
|
+
if (Number.isFinite(z)) {
|
|
302
|
+
// Has depth component
|
|
303
|
+
const coord = transformVector(pixelUnprojectionMatrix, [x, y, z, 1]);
|
|
304
|
+
return coord;
|
|
305
|
+
}
|
|
306
|
+
// since we don't know the correct projected z value for the point,
|
|
307
|
+
// unproject two points to get a line and then find the point on that line with z=0
|
|
308
|
+
const coord0 = transformVector(pixelUnprojectionMatrix, [x, y, 0, 1]);
|
|
309
|
+
const coord1 = transformVector(pixelUnprojectionMatrix, [x, y, 1, 1]);
|
|
310
|
+
const z0 = coord0[2];
|
|
311
|
+
const z1 = coord1[2];
|
|
312
|
+
const t = z0 === z1 ? 0 : ((targetZ || 0) - z0) / (z1 - z0);
|
|
313
|
+
return vec2.lerp([], coord0, coord1, t);
|
|
194
314
|
}
|
|
195
|
-
//# sourceMappingURL=web-mercator-utils.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DistanceScales } from
|
|
2
|
-
import type { FitBoundsOptions } from
|
|
1
|
+
import { DistanceScales } from "./web-mercator-utils.js";
|
|
2
|
+
import type { FitBoundsOptions } from "./fit-bounds.js";
|
|
3
3
|
/**
|
|
4
4
|
* @param width=1 - Width of "viewport" or window
|
|
5
5
|
* @param height=1 - Height of "viewport" or window
|
|
@@ -23,7 +23,7 @@ import type { FitBoundsOptions } from './fit-bounds';
|
|
|
23
23
|
* division by zero. This is intended to reduce the burden of apps to
|
|
24
24
|
* to check values before instantiating a Viewport.
|
|
25
25
|
*/
|
|
26
|
-
export
|
|
26
|
+
export type WebMercatorViewportProps = {
|
|
27
27
|
width: number;
|
|
28
28
|
height: number;
|
|
29
29
|
latitude?: number;
|
|
@@ -179,4 +179,3 @@ export declare class WebMercatorViewport {
|
|
|
179
179
|
pos: number[];
|
|
180
180
|
}): number[];
|
|
181
181
|
}
|
|
182
|
-
//# sourceMappingURL=web-mercator-viewport.d.ts.map
|