-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
66 lines (61 loc) · 2.09 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// web mercator projection extent
var ProjExtent = {
left: -20037508.342789244,
right: 20037508.342789244,
bottom: -20037508.342789244,
top: 20037508.342789244
}
//tile seize
var TileSize = 256
// resolutions
var Resolutions = [
156543.03392804097, 78271.51696402048, 39135.75848201024,
19567.87924100512, 9783.93962050256, 4891.96981025128, 2445.98490512564,
1222.99245256282, 611.49622628141, 305.748113140705, 152.8740565703525,
76.43702828517625, 38.21851414258813, 19.109257071294063, 9.554628535647032,
4.777314267823516, 2.388657133911758, 1.194328566955879, 0.5971642834779395,
0.29858214173896974, 0.14929107086948487, 0.07464553543474244,
0.03732276771737122, 0.01866138385868561]
var getTiles = function(extent,z) {
//coordinated in pixel
lx = Math.floor((extent.left - ProjExtent.left)/Resolutions[z]),
rx = Math.floor((extent.right - ProjExtent.left)/Resolutions[z]),
by = Math.floor((ProjExtent.top - extent.bottom )/Resolutions[z]),
ty = Math.floor((ProjExtent.top - extent.top )/Resolutions[z]),
// tile numbers
lX = Math.floor(lx/TileSize),
rX = Math.floor(rx/TileSize),
bY = Math.floor(by/TileSize),
tY = Math.floor(ty/TileSize),
//top left tile position of top-left tile with respect to window/div
top = topStart = (tY * TileSize) - ty,
left = (lX * TileSize) - lx,
tiles = [];
for (var i=lX; i<=rX; i++) {
top = topStart;
for(var j=tY; j<=bY; j++) {
tiles.push({
X:i,
Y:j,
Z:z,
top: top,
left: left
});
top +=TileSize;
}
left +=TileSize;
}
return tiles;
};
// Static funtion to get tile extent in web mercator coordinates
// params: {z: zoom leve, x: x tile no, y: y tile no}
getTiles.TileExtent = function (tile) {
var right = ProjExtent.left + tile.x * TileSize * Resolutions[tile.z]
var left = right - TileSize * Resolutions[tile.z]
var bottom = ProjExtent.top - tile.y * TileSize * Resolutions[tile.z]
var top = bottom + TileSize * Resolutions[tile.z]
return {left: left, right: right, bottom: bottom, top: top,
res: Resolutions[tile.z]
}
}
module.exports = getTiles