forked from mapbox/geo-viewport
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
46 lines (40 loc) · 1.39 KB
/
index.js
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
36
37
38
39
40
41
42
43
44
45
46
var merc = new (require('sphericalmercator'))();
module.exports.viewport = viewport;
module.exports.bounds = bounds;
function viewport(bounds, dimensions, minzoom, maxzoom) {
minzoom = (minzoom === undefined) ? 0 : minzoom;
maxzoom = (maxzoom === undefined) ? 20 : maxzoom;
var base = maxzoom,
bl = merc.px([bounds[0], bounds[1]], base),
tr = merc.px([bounds[2], bounds[3]], base),
width = tr[0] - bl[0],
height = bl[1] - tr[1],
ratios = [width / dimensions[0], height / dimensions[1]],
center = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2],
adjusted = Math.floor(Math.min(
base - (Math.log(ratios[0]) / Math.log(2)),
base - (Math.log(ratios[1]) / Math.log(2)))),
zoom = Math.max(minzoom, Math.min(maxzoom, adjusted));
return { center: center, zoom: zoom };
}
function bounds(viewport, zoom, dimensions) {
if (viewport.lon !== undefined) {
viewport = [
viewport.lon,
viewport.lat
];
}
var px = merc.px(viewport, zoom);
var tl = merc.ll([
px[0] - (dimensions[0] / 2),
px[1] - (dimensions[1] / 2)
], zoom);
var br = merc.ll([
px[0] + (dimensions[0] / 2),
px[1] + (dimensions[1] / 2)
], zoom);
return [tl[0], br[1], br[0], tl[1]];
}
function baseLog(x, y) {
return Math.log(y) / Math.log(x);
}