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