-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprojection.ts
More file actions
35 lines (31 loc) · 907 Bytes
/
projection.ts
File metadata and controls
35 lines (31 loc) · 907 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { BBOX, Pos2 } from "./types";
const D2R = Math.PI / 180;
/**
* Project a lon/lat point into projected mercator space.
*/
export function proj(ll: Pos2, size = 524288): Pos2 {
// Arbitrary.
var d = size / 2;
var bc = size / 360;
var cc = size / (2 * Math.PI);
var ac = size;
var f = Math.min(Math.max(Math.sin(D2R * ll[1]), -0.9999), 0.9999);
var x = d + ll[0] * bc;
var y = d + 0.5 * Math.log((1 + f) / (1 - f)) * -cc;
y > ac && (y = ac);
return [x, y];
}
/**
* Get an interpolation vector from a lon/lat BBOX to
* a pixel-sized box.
*/
export function getLerp(bbox: BBOX, [width, height]: Pos2, [dx, dy]: Pos2) {
const sw = proj([bbox[0], bbox[1]]);
const ne = proj([bbox[2], bbox[3]]);
return ([lon, lat]: Pos2) => {
return [
(width * (lon - sw[0])) / (ne[0] - sw[0]) + dx,
height - (height * (lat - sw[1])) / (ne[1] - sw[1]) + dy,
];
};
}