-
Notifications
You must be signed in to change notification settings - Fork 1
/
maptiler.js
172 lines (172 loc) · 5.11 KB
/
maptiler.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var maptiler;
maptiler = {
redis: {
on: false,
turnOn: function(){
var redis;
redis = require('redis').createClient();
redis.del('maptiler');
redis.quit();
this.on = true;
},
turnOff: function(){
this.on = false;
}
},
originShift: 2 * Math.PI * 6378137 / 2.0,
tileSize: 256,
initialResolution: 2 * Math.PI * 6378137 / 256,
latLonToMeters: function(lon, lat){
var x, y;
x = lon * this.originShift / 180.0;
y = Math.log(Math.tan((90 + lat) * Math.PI / 360.0)) / (Math.PI / 180.0);
y = y * this.originShift / 180.0;
return [x, y];
},
metersToLatLon: function(x, y){
var lon, lat;
lon = (x / this.originShift) * 180.0;
lat = (y / this.originShift) * 180.0;
lat = 180 / Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180.0)) - Math.PI / 2.0);
return [lon, lat];
},
res: function(it){
return this.initialResolution / Math.pow(2, it);
},
pixelsToMeters: function(x, y, z){
return [x * this.res(z) - this.originShift, y * this.res(z) - this.originShift];
},
metersToPixels: function(x, y, z){
var px, py;
px = (x + this.originShift) / this.res(z);
py = (y + this.originShift) / this.res(z);
return [(x + this.originShift) / this.res(z), (y + this.originShift) / this.res(z)];
},
pixelsToTile: function(x, y){
return [~~(Math.ceil(x / this.tileSize) - 1), ~~(Math.ceil(y / this.tileSize) - 1)];
},
pixelsToRaster: function(x, y, z){
return [x, (this.tileSize << z) - y];
},
metersToTile: function(x, y, z){
var pos;
pos = this.metersToPixels(x, y, z);
return this.pixelsToTile(pos[0], pos[1]);
},
tileBounds: function(x, y, z){
var min, max;
min = this.pixelsToMeters(x * this.tileSize, y * this.tileSize, z);
max = this.pixelsToMeters((x + 1) * this.tileSize, (y + 1) * this.tileSize, z);
return [min[0], min[1], max[0], max[1]];
},
tileLatLonBounds: function(x, y, z){
var bounds, min, max;
bounds = this.tileBounds(x, y, z);
min = this.metersToLatLon(bounds[0], bounds[1]);
max = this.metersToLatLon(bounds[2], bounds[3]);
return min.concat(max);
},
resolution: function(z){
return this.initialResolution / Math.pow(2, z);
},
zoomForPixelSize: function(size){
var i$, ref$, len$, i;
for (i$ = 0, len$ = (ref$ = [0, 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]).length; i$ < len$; ++i$) {
i = ref$[i$];
if (size > this.resolution(i) && i !== 0) {
return i - 1;
}
}
},
googleTile: function(x, y, z){
return [x, (Math.pow(2, z) - 1) - y];
},
quadTree: function(x, y, z){
var quadKey, i$, i, digit, mask;
y = (Math.pow(2, z) - 1) - y;
quadKey = '';
for (i$ = z; i$ > 0; --i$) {
i = i$;
digit = 0;
mask = 1 << i - 1;
if (x !== 0 && x !== 0) {
digit += 1;
}
if (y !== 0 && y !== 0) {
digit += 2;
}
}
return digit;
},
getTiles: function(left, bottom, right, top, zoom, tileCallback){
var async, mercPos1, mercPos2, westHem, northHem, tilePos1, tilePos2, redis, tiles, tx, ty, testX, testY, addTile, doRow, done, this$ = this;
async = require('async');
mercPos1 = this.latLonToMeters(left, bottom);
mercPos2 = this.latLonToMeters(right, top);
westHem = mercPos1[0] < 0 ? true : false;
northHem = mercPos1[1] > 0 ? true : false;
tilePos1 = this.metersToTile(mercPos1[0], mercPos1[1], zoom);
tilePos2 = this.metersToTile(mercPos2[0], mercPos2[1], zoom);
if (this.redis.on) {
redis = require('redis').createClient();
redis.on('error', function(it){
return Console.log("Error with Redis: " + it);
});
} else {
tiles = [];
}
tx = tilePos1[0];
ty = tilePos1[1];
testX = function(){
return tx > tilePos2[0];
};
testY = function(){
return ty > tilePos2[1];
};
addTile = function(callbackX){
var google, bounds3857, bounds4326, meta;
google = this$.googleTile(tx, ty, zoom);
bounds3857 = this$.tileBounds(tx, ty, zoom);
bounds4326 = this$.tileLatLonBounds(tx, ty, zoom);
meta = {
tms: [zoom, tx, ty],
google: [zoom, tx, google[1]],
extent3857: bounds3857,
extent4326: bounds4326
};
if (this$.redis.on) {
return redis.rpush('maptiler', JSON.stringify(meta), function(){
++tx;
return callbackX(null);
});
} else {
tiles.push(meta);
if (westHem) {
--tx;
} else {
++tx;
}
return setTimeout(callbackX, 1);
}
};
doRow = function(callbackY){
async.until(testX, addTile, function(){
ty++;
tx = tilePos1[0];
return callbackY(null);
});
};
done = function(){
if (this$.redis.on) {
redis.quit();
tileCallback('maptiler');
} else {
tileCallback(tiles);
}
};
return async.until(testY, doRow, done);
}
};
if ((typeof module != 'undefined' && module !== null ? module.exports : void 8) != null) {
module.exports = maptiler;
}