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