@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
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { DistanceScales } from './web-mercator-utils';
|
|
2
|
+
import type { FitBoundsOptions } from './fit-bounds';
|
|
3
|
+
/**
|
|
4
|
+
* @param width=1 - Width of "viewport" or window
|
|
5
|
+
* @param height=1 - Height of "viewport" or window
|
|
6
|
+
* @param scale=1 - Either use scale or zoom
|
|
7
|
+
* @param pitch=0 - Camera angle in degrees (0 is straight down)
|
|
8
|
+
* @param bearing=0 - Map rotation in degrees (0 means north is up)
|
|
9
|
+
* @param fovy= - Field of view of camera in degrees
|
|
10
|
+
* @param altitude= - Altitude of camera in screen units
|
|
11
|
+
*
|
|
12
|
+
* Web mercator projection short-hand parameters
|
|
13
|
+
* @param latitude - Center of viewport on map
|
|
14
|
+
* @param longitude - Center of viewport on map
|
|
15
|
+
* @param zoom - Scale = Math.pow(2,zoom) on map
|
|
16
|
+
|
|
17
|
+
* Notes:
|
|
18
|
+
* - Only one of center or [latitude, longitude] can be specified
|
|
19
|
+
* - [latitude, longitude] can only be specified when "mercator" is true
|
|
20
|
+
* - Altitude has a default value that matches assumptions in mapbox-gl
|
|
21
|
+
* - Field of view is independent from altitude, provide `altitudeToFovy(1.5)` (default value) to match assumptions in mapbox-gl
|
|
22
|
+
* - width and height are forced to 1 if supplied as 0, to avoid
|
|
23
|
+
* division by zero. This is intended to reduce the burden of apps to
|
|
24
|
+
* to check values before instantiating a Viewport.
|
|
25
|
+
*/
|
|
26
|
+
export declare type WebMercatorViewportProps = {
|
|
27
|
+
width: number;
|
|
28
|
+
height: number;
|
|
29
|
+
latitude?: number;
|
|
30
|
+
longitude?: number;
|
|
31
|
+
position?: number[];
|
|
32
|
+
zoom?: number;
|
|
33
|
+
pitch?: number;
|
|
34
|
+
bearing?: number;
|
|
35
|
+
altitude?: number;
|
|
36
|
+
fovy?: number;
|
|
37
|
+
nearZMultiplier?: number;
|
|
38
|
+
farZMultiplier?: number;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* The WebMercatorViewport class creates
|
|
42
|
+
* - view/projection matrices
|
|
43
|
+
* - "uniform values" (for shaders) from mercator params
|
|
44
|
+
*
|
|
45
|
+
* Note: Instances are immutable in the sense that they only have accessors.
|
|
46
|
+
* A new viewport instance should be created if any parameters have changed.
|
|
47
|
+
*/
|
|
48
|
+
export default class WebMercatorViewport {
|
|
49
|
+
readonly latitude: number;
|
|
50
|
+
readonly longitude: number;
|
|
51
|
+
readonly zoom: number;
|
|
52
|
+
readonly pitch: number;
|
|
53
|
+
readonly bearing: number;
|
|
54
|
+
readonly altitude: number;
|
|
55
|
+
readonly fovy: number;
|
|
56
|
+
readonly meterOffset: number[];
|
|
57
|
+
readonly center: number[];
|
|
58
|
+
readonly width: number;
|
|
59
|
+
readonly height: number;
|
|
60
|
+
readonly scale: number;
|
|
61
|
+
readonly distanceScales: DistanceScales;
|
|
62
|
+
readonly viewMatrix: number[];
|
|
63
|
+
readonly projectionMatrix: number[];
|
|
64
|
+
viewProjectionMatrix: number[];
|
|
65
|
+
pixelProjectionMatrix: number[];
|
|
66
|
+
pixelUnprojectionMatrix: number[];
|
|
67
|
+
/**
|
|
68
|
+
* @classdesc
|
|
69
|
+
* Creates view/projection matrices from mercator params
|
|
70
|
+
* Note: The Viewport is immutable in the sense that it only has accessors.
|
|
71
|
+
* A new viewport instance should be created if any parameters have changed.
|
|
72
|
+
*/
|
|
73
|
+
constructor(props?: WebMercatorViewportProps);
|
|
74
|
+
_initMatrices(): void;
|
|
75
|
+
/** Two viewports are equal if width and height are identical, and if
|
|
76
|
+
* their view and projection matrices are (approximately) equal.
|
|
77
|
+
*/
|
|
78
|
+
equals(viewport: WebMercatorViewport | null): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Projects xyz (possibly latitude and longitude) to pixel coordinates in window
|
|
81
|
+
* using viewport projection parameters
|
|
82
|
+
* - [longitude, latitude] to [x, y]
|
|
83
|
+
* - [longitude, latitude, Z] => [x, y, z]
|
|
84
|
+
* Note: By default, returns top-left coordinates for canvas/SVG type render
|
|
85
|
+
*
|
|
86
|
+
* @param lngLatZ - [lng, lat] or [lng, lat, Z]
|
|
87
|
+
* @param options - options
|
|
88
|
+
* @param options.topLeft=true - Whether projected coords are top left
|
|
89
|
+
* @return - screen coordinates [x, y] or [x, y, z], z as pixel depth
|
|
90
|
+
*/
|
|
91
|
+
project(lngLatZ: number[], options?: {
|
|
92
|
+
topLeft?: boolean;
|
|
93
|
+
}): number[];
|
|
94
|
+
/**
|
|
95
|
+
* Unproject pixel coordinates on screen onto world coordinates, possibly `[lon, lat]` on map.
|
|
96
|
+
*
|
|
97
|
+
* - [x, y] => [lng, lat]
|
|
98
|
+
* - [x, y, z] => [lng, lat, Z]
|
|
99
|
+
*
|
|
100
|
+
* @param xyz - screen coordinates, z as pixel depth
|
|
101
|
+
* @param options - options
|
|
102
|
+
* @param options.topLeft=true - Whether projected coords are top left
|
|
103
|
+
* @param options.targetZ=0 - If pixel depth is unknown, targetZ is used as
|
|
104
|
+
* the elevation plane to unproject onto
|
|
105
|
+
* @return - [lng, lat, Z] or [X, Y, Z]
|
|
106
|
+
*/
|
|
107
|
+
unproject(xyz: number[], options?: {
|
|
108
|
+
topLeft?: boolean;
|
|
109
|
+
targetZ?: number;
|
|
110
|
+
}): number[];
|
|
111
|
+
projectPosition(xyz: number[]): [number, number, number];
|
|
112
|
+
unprojectPosition(xyz: number[]): [number, number, number];
|
|
113
|
+
/**
|
|
114
|
+
* Project [lng,lat] on sphere onto [x,y] on 512*512 Mercator Zoom 0 tile.
|
|
115
|
+
* Performs the nonlinear part of the web mercator projection.
|
|
116
|
+
* Remaining projection is done with 4x4 matrices which also handles
|
|
117
|
+
* perspective.
|
|
118
|
+
*
|
|
119
|
+
* @param lngLat - [lng, lat] coordinates
|
|
120
|
+
* Specifies a point on the sphere to project onto the map.
|
|
121
|
+
* @return [x,y] coordinates.
|
|
122
|
+
*/
|
|
123
|
+
projectFlat(lngLat: number[]): number[];
|
|
124
|
+
/**
|
|
125
|
+
* Unproject world point [x,y] on map onto {lat, lon} on sphere
|
|
126
|
+
*
|
|
127
|
+
* @param xy - array with [x,y] members
|
|
128
|
+
* representing point on projected map plane
|
|
129
|
+
* @return - array with [lat,lon] of point on sphere.
|
|
130
|
+
* Has toArray method if you need a GeoJSON Array.
|
|
131
|
+
* Per cartographic tradition, lat and lon are specified as degrees.
|
|
132
|
+
*/
|
|
133
|
+
unprojectFlat(xy: number[]): number[];
|
|
134
|
+
/**
|
|
135
|
+
* Get the map center that place a given [lng, lat] coordinate at screen point [x, y]
|
|
136
|
+
* @param opt
|
|
137
|
+
* @param opt.lngLat - [lng,lat] coordinates
|
|
138
|
+
* Specifies a point on the sphere.
|
|
139
|
+
* @param opt.pos - [x,y] coordinates
|
|
140
|
+
* Specifies a point on the screen.
|
|
141
|
+
* @return [lng,lat] new map center.
|
|
142
|
+
*/
|
|
143
|
+
getMapCenterByLngLatPosition({ lngLat, pos }: {
|
|
144
|
+
lngLat: number[];
|
|
145
|
+
pos: number[];
|
|
146
|
+
}): number[];
|
|
147
|
+
/**
|
|
148
|
+
* Returns a new viewport that fit around the given rectangle.
|
|
149
|
+
* Only supports non-perspective mode.
|
|
150
|
+
* @param bounds - [[lon, lat], [lon, lat]]
|
|
151
|
+
* @param [options]
|
|
152
|
+
* @param [options.padding] - The amount of padding in pixels to add to the given bounds.
|
|
153
|
+
* @param [options.offset] - The center of the given bounds relative to the map's center,
|
|
154
|
+
* [x, y] measured in pixels.
|
|
155
|
+
* @returns {WebMercatorViewport}
|
|
156
|
+
*/
|
|
157
|
+
fitBounds(bounds: [[number, number], [number, number]], options?: Omit<FitBoundsOptions, 'width' | 'height' | 'bounds'>): WebMercatorViewport;
|
|
158
|
+
/**
|
|
159
|
+
* Returns the bounding box of the viewport.
|
|
160
|
+
* @param [options]
|
|
161
|
+
* @param [options.z] - The altitude at which the bounds should be calculated.
|
|
162
|
+
* @returns {Array} bounds - [[lon, lat], [lon, lat]]
|
|
163
|
+
*/
|
|
164
|
+
getBounds(options?: {
|
|
165
|
+
z?: number;
|
|
166
|
+
}): number[][];
|
|
167
|
+
/**
|
|
168
|
+
* Returns the bounding box of the viewport.
|
|
169
|
+
* @param [options]
|
|
170
|
+
* @param [options.z] - The altitude at which the bounds should be calculated.
|
|
171
|
+
* @returns {Array} an array of 4 points that define the visible region
|
|
172
|
+
*/
|
|
173
|
+
getBoundingRegion(options?: {
|
|
174
|
+
z?: number;
|
|
175
|
+
}): number[][];
|
|
176
|
+
/** @deprecated Legacy method name */
|
|
177
|
+
getLocationAtPoint({ lngLat, pos }: {
|
|
178
|
+
lngLat: number[];
|
|
179
|
+
pos: number[];
|
|
180
|
+
}): number[];
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=web-mercator-viewport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-mercator-viewport.d.ts","sourceRoot":"","sources":["../src/web-mercator-viewport.ts"],"names":[],"mappings":"AAGA,OAAO,EAYL,cAAc,EACf,MAAM,sBAAsB,CAAC;AAG9B,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAMnD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,oBAAY,wBAAwB,GAAG;IAErC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAA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A;KAAC,GAAG,MAAM,EAAE,EAAE;IAa7C;;;;;OAKG;IACH,iBAAiB,CAAC,OAAO,GAAE;QAAC,CAAC,CAAC,EAAE,MAAM,CAAA;KAAM,GAAG,MAAM,EAAE,EAAE;IAMzD,qCAAqC;IACrC,kBAAkB,CAAC,EAAC,MAAM,EAAE,GAAG,EAAC,EAAE;QAAC,MAAM,EAAE,MAAM,EAAE,CAAC;QAAC,GAAG,EAAE,MAAM,EAAE,CAAA;KAAC,GAAG,MAAM,EAAE;CAG/E"}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// View and Projection Matrix calculations for mapbox-js style map view properties
|
|
2
|
+
import { createMat4 } from './math-utils';
|
|
3
|
+
import { zoomToScale, pixelsToWorld, lngLatToWorld, worldToLngLat, worldToPixels, altitudeToFovy, fovyToAltitude, DEFAULT_ALTITUDE, getProjectionMatrix, getDistanceScales, getViewMatrix } from './web-mercator-utils';
|
|
4
|
+
import fitBounds from './fit-bounds';
|
|
5
|
+
import getBounds from './get-bounds';
|
|
6
|
+
import * as mat4 from 'gl-matrix/mat4';
|
|
7
|
+
import * as vec2 from 'gl-matrix/vec2';
|
|
8
|
+
import * as vec3 from 'gl-matrix/vec3';
|
|
9
|
+
/**
|
|
10
|
+
* The WebMercatorViewport class creates
|
|
11
|
+
* - view/projection matrices
|
|
12
|
+
* - "uniform values" (for shaders) from mercator params
|
|
13
|
+
*
|
|
14
|
+
* Note: Instances are immutable in the sense that they only have accessors.
|
|
15
|
+
* A new viewport instance should be created if any parameters have changed.
|
|
16
|
+
*/
|
|
17
|
+
export default class WebMercatorViewport {
|
|
18
|
+
/**
|
|
19
|
+
* @classdesc
|
|
20
|
+
* Creates view/projection matrices from mercator params
|
|
21
|
+
* Note: The Viewport is immutable in the sense that it only has accessors.
|
|
22
|
+
* A new viewport instance should be created if any parameters have changed.
|
|
23
|
+
*/
|
|
24
|
+
// eslint-disable-next-line max-statements
|
|
25
|
+
constructor(props = { width: 1, height: 1 }) {
|
|
26
|
+
let {
|
|
27
|
+
// Map state
|
|
28
|
+
width, height, altitude = null, fovy = null } = props;
|
|
29
|
+
const { latitude = 0, longitude = 0, zoom = 0, pitch = 0, bearing = 0, position = null, nearZMultiplier = 0.02, farZMultiplier = 1.01 } = props;
|
|
30
|
+
// Silently allow apps to send in 0,0 to facilitate isomorphic render etc
|
|
31
|
+
width = width || 1;
|
|
32
|
+
height = height || 1;
|
|
33
|
+
// `fovy` & `altitude` are independent parameters, one for the
|
|
34
|
+
// projection and the latter for the view matrix. In the past,
|
|
35
|
+
// the `fovy` was always derived from the `altitude`
|
|
36
|
+
if (fovy === null && altitude === null) {
|
|
37
|
+
altitude = DEFAULT_ALTITUDE;
|
|
38
|
+
fovy = altitudeToFovy(altitude);
|
|
39
|
+
}
|
|
40
|
+
else if (fovy === null) {
|
|
41
|
+
fovy = altitudeToFovy(altitude);
|
|
42
|
+
}
|
|
43
|
+
else if (altitude === null) {
|
|
44
|
+
altitude = fovyToAltitude(fovy);
|
|
45
|
+
}
|
|
46
|
+
const scale = zoomToScale(zoom);
|
|
47
|
+
// Altitude - prevent division by 0
|
|
48
|
+
// TODO - just throw an Error instead?
|
|
49
|
+
altitude = Math.max(0.75, altitude);
|
|
50
|
+
const distanceScales = getDistanceScales({ longitude, latitude });
|
|
51
|
+
const center = lngLatToWorld([longitude, latitude]);
|
|
52
|
+
center.push(0);
|
|
53
|
+
if (position) {
|
|
54
|
+
vec3.add(center, center, vec3.mul([], position, distanceScales.unitsPerMeter));
|
|
55
|
+
}
|
|
56
|
+
this.projectionMatrix = getProjectionMatrix({
|
|
57
|
+
width,
|
|
58
|
+
height,
|
|
59
|
+
pitch,
|
|
60
|
+
fovy,
|
|
61
|
+
nearZMultiplier,
|
|
62
|
+
farZMultiplier
|
|
63
|
+
});
|
|
64
|
+
this.viewMatrix = getViewMatrix({
|
|
65
|
+
height,
|
|
66
|
+
scale,
|
|
67
|
+
center,
|
|
68
|
+
pitch,
|
|
69
|
+
bearing,
|
|
70
|
+
altitude
|
|
71
|
+
});
|
|
72
|
+
// Save parameters
|
|
73
|
+
this.width = width;
|
|
74
|
+
this.height = height;
|
|
75
|
+
this.scale = scale;
|
|
76
|
+
this.latitude = latitude;
|
|
77
|
+
this.longitude = longitude;
|
|
78
|
+
this.zoom = zoom;
|
|
79
|
+
this.pitch = pitch;
|
|
80
|
+
this.bearing = bearing;
|
|
81
|
+
this.altitude = altitude;
|
|
82
|
+
this.fovy = fovy;
|
|
83
|
+
this.center = center;
|
|
84
|
+
this.meterOffset = position || [0, 0, 0];
|
|
85
|
+
this.distanceScales = distanceScales;
|
|
86
|
+
this._initMatrices();
|
|
87
|
+
// Bind methods for easy access
|
|
88
|
+
this.equals = this.equals.bind(this);
|
|
89
|
+
this.project = this.project.bind(this);
|
|
90
|
+
this.unproject = this.unproject.bind(this);
|
|
91
|
+
this.projectPosition = this.projectPosition.bind(this);
|
|
92
|
+
this.unprojectPosition = this.unprojectPosition.bind(this);
|
|
93
|
+
Object.freeze(this);
|
|
94
|
+
}
|
|
95
|
+
_initMatrices() {
|
|
96
|
+
const { width, height, projectionMatrix, viewMatrix } = this;
|
|
97
|
+
// Note: As usual, matrix operations should be applied in "reverse" order
|
|
98
|
+
// since vectors will be multiplied in from the right during transformation
|
|
99
|
+
const vpm = createMat4();
|
|
100
|
+
mat4.multiply(vpm, vpm, projectionMatrix);
|
|
101
|
+
mat4.multiply(vpm, vpm, viewMatrix);
|
|
102
|
+
this.viewProjectionMatrix = vpm;
|
|
103
|
+
// Calculate matrices and scales needed for projection
|
|
104
|
+
/**
|
|
105
|
+
* Builds matrices that converts preprojected lngLats to screen pixels
|
|
106
|
+
* and vice versa.
|
|
107
|
+
* Note: Currently returns bottom-left coordinates!
|
|
108
|
+
* Note: Starts with the GL projection matrix and adds steps to the
|
|
109
|
+
* scale and translate that matrix onto the window.
|
|
110
|
+
* Note: WebGL controls clip space to screen projection with gl.viewport
|
|
111
|
+
* and does not need this step.
|
|
112
|
+
*/
|
|
113
|
+
const m = createMat4();
|
|
114
|
+
// matrix for conversion from location to screen coordinates
|
|
115
|
+
mat4.scale(m, m, [width / 2, -height / 2, 1]);
|
|
116
|
+
mat4.translate(m, m, [1, -1, 0]);
|
|
117
|
+
mat4.multiply(m, m, vpm);
|
|
118
|
+
const mInverse = mat4.invert(createMat4(), m);
|
|
119
|
+
if (!mInverse) {
|
|
120
|
+
throw new Error('Pixel project matrix not invertible');
|
|
121
|
+
}
|
|
122
|
+
this.pixelProjectionMatrix = m;
|
|
123
|
+
this.pixelUnprojectionMatrix = mInverse;
|
|
124
|
+
}
|
|
125
|
+
/** Two viewports are equal if width and height are identical, and if
|
|
126
|
+
* their view and projection matrices are (approximately) equal.
|
|
127
|
+
*/
|
|
128
|
+
equals(viewport) {
|
|
129
|
+
if (!(viewport instanceof WebMercatorViewport)) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
return (viewport.width === this.width &&
|
|
133
|
+
viewport.height === this.height &&
|
|
134
|
+
mat4.equals(viewport.projectionMatrix, this.projectionMatrix) &&
|
|
135
|
+
mat4.equals(viewport.viewMatrix, this.viewMatrix));
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Projects xyz (possibly latitude and longitude) to pixel coordinates in window
|
|
139
|
+
* using viewport projection parameters
|
|
140
|
+
* - [longitude, latitude] to [x, y]
|
|
141
|
+
* - [longitude, latitude, Z] => [x, y, z]
|
|
142
|
+
* Note: By default, returns top-left coordinates for canvas/SVG type render
|
|
143
|
+
*
|
|
144
|
+
* @param lngLatZ - [lng, lat] or [lng, lat, Z]
|
|
145
|
+
* @param options - options
|
|
146
|
+
* @param options.topLeft=true - Whether projected coords are top left
|
|
147
|
+
* @return - screen coordinates [x, y] or [x, y, z], z as pixel depth
|
|
148
|
+
*/
|
|
149
|
+
project(lngLatZ, options = {}) {
|
|
150
|
+
const { topLeft = true } = options;
|
|
151
|
+
const worldPosition = this.projectPosition(lngLatZ);
|
|
152
|
+
const coord = worldToPixels(worldPosition, this.pixelProjectionMatrix);
|
|
153
|
+
const [x, y] = coord;
|
|
154
|
+
const y2 = topLeft ? y : this.height - y;
|
|
155
|
+
return lngLatZ.length === 2 ? [x, y2] : [x, y2, coord[2]];
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Unproject pixel coordinates on screen onto world coordinates, possibly `[lon, lat]` on map.
|
|
159
|
+
*
|
|
160
|
+
* - [x, y] => [lng, lat]
|
|
161
|
+
* - [x, y, z] => [lng, lat, Z]
|
|
162
|
+
*
|
|
163
|
+
* @param xyz - screen coordinates, z as pixel depth
|
|
164
|
+
* @param options - options
|
|
165
|
+
* @param options.topLeft=true - Whether projected coords are top left
|
|
166
|
+
* @param options.targetZ=0 - If pixel depth is unknown, targetZ is used as
|
|
167
|
+
* the elevation plane to unproject onto
|
|
168
|
+
* @return - [lng, lat, Z] or [X, Y, Z]
|
|
169
|
+
*/
|
|
170
|
+
unproject(xyz, options = {}) {
|
|
171
|
+
const { topLeft = true, targetZ = undefined } = options;
|
|
172
|
+
const [x, y, z] = xyz;
|
|
173
|
+
const y2 = topLeft ? y : this.height - y;
|
|
174
|
+
const targetZWorld = targetZ && targetZ * this.distanceScales.unitsPerMeter[2];
|
|
175
|
+
const coord = pixelsToWorld([x, y2, z], this.pixelUnprojectionMatrix, targetZWorld);
|
|
176
|
+
const [X, Y, Z] = this.unprojectPosition(coord);
|
|
177
|
+
if (Number.isFinite(z)) {
|
|
178
|
+
return [X, Y, Z];
|
|
179
|
+
}
|
|
180
|
+
return Number.isFinite(targetZ) ? [X, Y, targetZ] : [X, Y];
|
|
181
|
+
}
|
|
182
|
+
// NON_LINEAR PROJECTION HOOKS
|
|
183
|
+
// Used for web meractor projection
|
|
184
|
+
projectPosition(xyz) {
|
|
185
|
+
const [X, Y] = lngLatToWorld(xyz);
|
|
186
|
+
const Z = (xyz[2] || 0) * this.distanceScales.unitsPerMeter[2];
|
|
187
|
+
return [X, Y, Z];
|
|
188
|
+
}
|
|
189
|
+
unprojectPosition(xyz) {
|
|
190
|
+
const [X, Y] = worldToLngLat(xyz);
|
|
191
|
+
const Z = (xyz[2] || 0) * this.distanceScales.metersPerUnit[2];
|
|
192
|
+
return [X, Y, Z];
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Project [lng,lat] on sphere onto [x,y] on 512*512 Mercator Zoom 0 tile.
|
|
196
|
+
* Performs the nonlinear part of the web mercator projection.
|
|
197
|
+
* Remaining projection is done with 4x4 matrices which also handles
|
|
198
|
+
* perspective.
|
|
199
|
+
*
|
|
200
|
+
* @param lngLat - [lng, lat] coordinates
|
|
201
|
+
* Specifies a point on the sphere to project onto the map.
|
|
202
|
+
* @return [x,y] coordinates.
|
|
203
|
+
*/
|
|
204
|
+
projectFlat(lngLat) {
|
|
205
|
+
return lngLatToWorld(lngLat);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Unproject world point [x,y] on map onto {lat, lon} on sphere
|
|
209
|
+
*
|
|
210
|
+
* @param xy - array with [x,y] members
|
|
211
|
+
* representing point on projected map plane
|
|
212
|
+
* @return - array with [lat,lon] of point on sphere.
|
|
213
|
+
* Has toArray method if you need a GeoJSON Array.
|
|
214
|
+
* Per cartographic tradition, lat and lon are specified as degrees.
|
|
215
|
+
*/
|
|
216
|
+
unprojectFlat(xy) {
|
|
217
|
+
return worldToLngLat(xy);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Get the map center that place a given [lng, lat] coordinate at screen point [x, y]
|
|
221
|
+
* @param opt
|
|
222
|
+
* @param opt.lngLat - [lng,lat] coordinates
|
|
223
|
+
* Specifies a point on the sphere.
|
|
224
|
+
* @param opt.pos - [x,y] coordinates
|
|
225
|
+
* Specifies a point on the screen.
|
|
226
|
+
* @return [lng,lat] new map center.
|
|
227
|
+
*/
|
|
228
|
+
getMapCenterByLngLatPosition({ lngLat, pos }) {
|
|
229
|
+
const fromLocation = pixelsToWorld(pos, this.pixelUnprojectionMatrix);
|
|
230
|
+
const toLocation = lngLatToWorld(lngLat);
|
|
231
|
+
const translate = vec2.add([], toLocation, vec2.negate([], fromLocation));
|
|
232
|
+
const newCenter = vec2.add([], this.center, translate);
|
|
233
|
+
return worldToLngLat(newCenter);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Returns a new viewport that fit around the given rectangle.
|
|
237
|
+
* Only supports non-perspective mode.
|
|
238
|
+
* @param bounds - [[lon, lat], [lon, lat]]
|
|
239
|
+
* @param [options]
|
|
240
|
+
* @param [options.padding] - The amount of padding in pixels to add to the given bounds.
|
|
241
|
+
* @param [options.offset] - The center of the given bounds relative to the map's center,
|
|
242
|
+
* [x, y] measured in pixels.
|
|
243
|
+
* @returns {WebMercatorViewport}
|
|
244
|
+
*/
|
|
245
|
+
fitBounds(bounds, options = {}) {
|
|
246
|
+
const { width, height } = this;
|
|
247
|
+
const { longitude, latitude, zoom } = fitBounds(Object.assign({ width, height, bounds }, options));
|
|
248
|
+
return new WebMercatorViewport({ width, height, longitude, latitude, zoom });
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Returns the bounding box of the viewport.
|
|
252
|
+
* @param [options]
|
|
253
|
+
* @param [options.z] - The altitude at which the bounds should be calculated.
|
|
254
|
+
* @returns {Array} bounds - [[lon, lat], [lon, lat]]
|
|
255
|
+
*/
|
|
256
|
+
getBounds(options) {
|
|
257
|
+
const corners = this.getBoundingRegion(options);
|
|
258
|
+
const west = Math.min(...corners.map((p) => p[0]));
|
|
259
|
+
const east = Math.max(...corners.map((p) => p[0]));
|
|
260
|
+
const south = Math.min(...corners.map((p) => p[1]));
|
|
261
|
+
const north = Math.max(...corners.map((p) => p[1]));
|
|
262
|
+
return [
|
|
263
|
+
[west, south],
|
|
264
|
+
[east, north]
|
|
265
|
+
];
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Returns the bounding box of the viewport.
|
|
269
|
+
* @param [options]
|
|
270
|
+
* @param [options.z] - The altitude at which the bounds should be calculated.
|
|
271
|
+
* @returns {Array} an array of 4 points that define the visible region
|
|
272
|
+
*/
|
|
273
|
+
getBoundingRegion(options = {}) {
|
|
274
|
+
return getBounds(this, options.z || 0);
|
|
275
|
+
}
|
|
276
|
+
// DEPRECATED
|
|
277
|
+
/** @deprecated Legacy method name */
|
|
278
|
+
getLocationAtPoint({ lngLat, pos }) {
|
|
279
|
+
return this.getMapCenterByLngLatPosition({ lngLat, pos });
|
|
280
|
+
}
|
|
281
|
+
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Utilities for perspective-enabled Web Mercator projections",
|
|
4
4
|
"author": "Uber Technologies, Inc.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "3.
|
|
6
|
+
"version": "3.6.0-alpha.3",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"webgl",
|
|
9
9
|
"javascript",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"type": "git",
|
|
19
19
|
"url": "https://github.com/uber-web/math.gl.git"
|
|
20
20
|
},
|
|
21
|
-
"types": "
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
22
22
|
"main": "dist/es5/index.js",
|
|
23
23
|
"module": "dist/esm/index.js",
|
|
24
24
|
"files": [
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@babel/runtime": "^7.12.0",
|
|
30
|
-
"gl-matrix": "
|
|
30
|
+
"gl-matrix": "^3.4.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"mapbox-gl": "^1.0.0",
|
|
34
34
|
"mock-browser": "^0.92.14"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "ec2a4af3e64b97f0354a75c370d23d3de0aa3b43"
|
|
37
37
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Replacement for the external assert method to reduce bundle size
|
|
2
2
|
// Note: We don't use the second "message" argument in calling code,
|
|
3
3
|
// so no need to support it here
|
|
4
|
-
export default function assert(condition, message) {
|
|
4
|
+
export default function assert(condition: unknown, message?: string): void {
|
|
5
5
|
if (!condition) {
|
|
6
6
|
throw new Error(message || '@math.gl/web-mercator: assertion failed.');
|
|
7
7
|
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import assert from './assert';
|
|
2
|
+
import {log2, clamp} from './math-utils';
|
|
3
|
+
import {MAX_LATITUDE, lngLatToWorld, worldToLngLat} from './web-mercator-utils';
|
|
4
|
+
|
|
5
|
+
/**
|
|
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
|
+
*/
|
|
16
|
+
export type FitBoundsOptions = {
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
bounds: [[number, number], [number, number]];
|
|
20
|
+
minExtent?: number; // 0.01 would be about 1000 meters (degree is ~110KM)
|
|
21
|
+
maxZoom?: number; // ~x4,000,000 => About 10 meter extents
|
|
22
|
+
// options
|
|
23
|
+
padding?: number | Padding;
|
|
24
|
+
offset?: number[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 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
|
+
*/
|
|
34
|
+
export type Padding = {
|
|
35
|
+
top: number;
|
|
36
|
+
bottom: number;
|
|
37
|
+
left: number;
|
|
38
|
+
right: number;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type ViewportProps = {
|
|
42
|
+
longitude: number;
|
|
43
|
+
latitude: number;
|
|
44
|
+
zoom: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns map settings {latitude, longitude, zoom}
|
|
49
|
+
* that will contain the provided corners within the provided width.
|
|
50
|
+
*
|
|
51
|
+
* > _Note: Only supports non-perspective mode._
|
|
52
|
+
*
|
|
53
|
+
* @param options fit bounds parameters
|
|
54
|
+
* @returns - latitude, longitude and zoom
|
|
55
|
+
*/
|
|
56
|
+
export default function fitBounds(options: FitBoundsOptions): ViewportProps {
|
|
57
|
+
const {
|
|
58
|
+
width,
|
|
59
|
+
height,
|
|
60
|
+
bounds,
|
|
61
|
+
minExtent = 0, // 0.01 would be about 1000 meters (degree is ~110KM)
|
|
62
|
+
maxZoom = 24, // ~x4,000,000 => About 10 meter extents
|
|
63
|
+
offset = [0, 0]
|
|
64
|
+
} = options;
|
|
65
|
+
|
|
66
|
+
const [[west, south], [east, north]] = bounds;
|
|
67
|
+
const padding = getPaddingObject(options.padding);
|
|
68
|
+
|
|
69
|
+
const nw = lngLatToWorld([west, clamp(north, -MAX_LATITUDE, MAX_LATITUDE)]);
|
|
70
|
+
const se = lngLatToWorld([east, clamp(south, -MAX_LATITUDE, MAX_LATITUDE)]);
|
|
71
|
+
|
|
72
|
+
// width/height on the Web Mercator plane
|
|
73
|
+
const size = [
|
|
74
|
+
Math.max(Math.abs(se[0] - nw[0]), minExtent),
|
|
75
|
+
Math.max(Math.abs(se[1] - nw[1]), minExtent)
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
const targetSize = [
|
|
79
|
+
width - padding.left - padding.right - Math.abs(offset[0]) * 2,
|
|
80
|
+
height - padding.top - padding.bottom - Math.abs(offset[1]) * 2
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
assert(targetSize[0] > 0 && targetSize[1] > 0);
|
|
84
|
+
|
|
85
|
+
// scale = screen pixels per unit on the Web Mercator plane
|
|
86
|
+
const scaleX = targetSize[0] / size[0];
|
|
87
|
+
const scaleY = targetSize[1] / size[1];
|
|
88
|
+
|
|
89
|
+
// Find how much we need to shift the center
|
|
90
|
+
const offsetX = (padding.right - padding.left) / 2 / scaleX;
|
|
91
|
+
const offsetY = (padding.bottom - padding.top) / 2 / scaleY;
|
|
92
|
+
|
|
93
|
+
const center = [(se[0] + nw[0]) / 2 + offsetX, (se[1] + nw[1]) / 2 + offsetY];
|
|
94
|
+
|
|
95
|
+
const centerLngLat = worldToLngLat(center);
|
|
96
|
+
const zoom = Math.min(maxZoom, log2(Math.abs(Math.min(scaleX, scaleY))));
|
|
97
|
+
|
|
98
|
+
assert(Number.isFinite(zoom));
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
longitude: centerLngLat[0],
|
|
102
|
+
latitude: centerLngLat[1],
|
|
103
|
+
zoom
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Helpers
|
|
108
|
+
function getPaddingObject(padding: number | Padding = 0): Padding {
|
|
109
|
+
if (typeof padding === 'number') {
|
|
110
|
+
return {
|
|
111
|
+
top: padding,
|
|
112
|
+
bottom: padding,
|
|
113
|
+
left: padding,
|
|
114
|
+
right: padding
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Make sure all the required properties are set
|
|
119
|
+
assert(
|
|
120
|
+
Number.isFinite(padding.top) &&
|
|
121
|
+
Number.isFinite(padding.bottom) &&
|
|
122
|
+
Number.isFinite(padding.left) &&
|
|
123
|
+
Number.isFinite(padding.right)
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
return padding;
|
|
127
|
+
}
|