-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathleaflet.utfgrid.js
More file actions
403 lines (349 loc) · 9.91 KB
/
leaflet.utfgrid.js
File metadata and controls
403 lines (349 loc) · 9.91 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
L.ajax = function (url, success, error) {
// the following is from JavaScript: The Definitive Guide
// and https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest_in_IE6
if (window.XMLHttpRequest === undefined) {
window.XMLHttpRequest = function () {
/*global ActiveXObject:true */
try {
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
throw new Error("XMLHttpRequest is not supported");
}
};
}
var response, request = new XMLHttpRequest();
request.open("GET", url);
request.onreadystatechange = function () {
/*jshint evil: true */
if (request.readyState === 4) {
if (request.status === 200) {
if (window.JSON) {
response = JSON.parse(request.responseText);
} else {
response = eval("(" + request.responseText + ")");
}
success(response);
} else if (request.status !== 0 && error !== undefined) {
error(request.status);
}
}
};
request.ontimeout = function () { error('timeout'); };
request.send();
return request;
};
L.UtfGrid = (L.Layer || L.Class).extend({
includes: L.Evented,
options: {
subdomains: 'abc',
minZoom: 0,
maxZoom: 18,
tileSize: 256,
resolution: 4,
useJsonP: true,
pointerCursor: true,
maxRequests: 4,
requestTimeout: 60000
},
//The thing the mouse is currently on
_mouseOn: null,
initialize: function (url, options) {
L.Util.setOptions(this, options);
// The requests
this._requests = {};
this._request_queue = [];
this._requests_in_process = [];
this._url = url;
this._cache = {};
//Find a unique id in window we can use for our callbacks
//Required for jsonP
var i = 0;
while (window['lu' + i]) {
i++;
}
this._windowKey = 'lu' + i;
window[this._windowKey] = {};
var subdomains = this.options.subdomains;
if (typeof this.options.subdomains === 'string') {
this.options.subdomains = subdomains.split('');
}
},
onAdd: function (map) {
this._map = map;
this._container = this._map._container;
this._update();
var zoom = Math.round(this._map.getZoom());
if (zoom > this.options.maxZoom || zoom < this.options.minZoom) {
return;
}
map.on('click', this._click, this);
map.on('mousemove', this._move, this);
map.on('moveend', this._update, this);
},
onRemove: function () {
var map = this._map;
map.off('click', this._click, this);
map.off('mousemove', this._move, this);
map.off('moveend', this._update, this);
if (this.options.pointerCursor) {
this._container.style.cursor = '';
}
},
setUrl: function (url, noRedraw) {
this._url = url;
if (!noRedraw) {
this.redraw();
}
return this;
},
redraw: function () {
// Clear cache to force all tiles to reload
this._request_queue = [];
for (var req_key in this._requests) {
if (this._requests.hasOwnProperty(req_key)) {
this._abort_request(req_key);
}
}
this._cache = {};
this._update();
},
_click: function (e) {
this.fire('click', this._objectForEvent(e));
},
_move: function (e) {
var on = this._objectForEvent(e);
if (on.data !== this._mouseOn) {
if (this._mouseOn) {
this.fire('mouseout', { latlng: e.latlng, data: this._mouseOn });
if (this.options.pointerCursor) {
this._container.style.cursor = '';
}
}
if (on.data) {
this.fire('mouseover', on);
if (this.options.pointerCursor) {
this._container.style.cursor = 'pointer';
}
}
this._mouseOn = on.data;
} else if (on.data) {
this.fire('mousemove', on);
}
},
_objectForEvent: function (e) {
var map = this._map,
point = map.project(e.latlng),
tileSize = this.options.tileSize,
resolution = this.options.resolution,
x = Math.floor(point.x / tileSize),
y = Math.floor(point.y / tileSize),
gridX = Math.floor((point.x - (x * tileSize)) / resolution),
gridY = Math.floor((point.y - (y * tileSize)) / resolution),
max = map.options.crs.scale(map.getZoom()) / tileSize;
x = (x + max) % max;
y = (y + max) % max;
var data = this._cache[map.getZoom() + '_' + x + '_' + y];
var result = null;
if (data && data.grid) {
var idx = this._utfDecode(data.grid[gridY].charCodeAt(gridX)),
key = data.keys[idx];
if (data.data.hasOwnProperty(key)) {
result = data.data[key];
}
}
return L.extend({ latlng: e.latlng, data: result }, e);
},
//Load up all required json grid files
//TODO: Load from center etc
_update: function () {
var bounds = this._map.getPixelBounds(),
zoom = Math.round(this._map.getZoom()),
tileSize = this.options.tileSize;
if (zoom > this.options.maxZoom || zoom < this.options.minZoom) {
return;
}
var nwTilePoint = new L.Point(
Math.floor(bounds.min.x / tileSize),
Math.floor(bounds.min.y / tileSize)),
seTilePoint = new L.Point(
Math.floor(bounds.max.x / tileSize),
Math.floor(bounds.max.y / tileSize)),
max = this._map.options.crs.scale(zoom) / tileSize;
//Load all required ones
var visible_tiles = [];
for (var x = nwTilePoint.x; x <= seTilePoint.x; x++) {
for (var y = nwTilePoint.y; y <= seTilePoint.y; y++) {
var xw = (x + max) % max, yw = (y + max) % max;
var key = zoom + '_' + xw + '_' + yw;
visible_tiles.push(key);
if (!this._cache.hasOwnProperty(key)) {
this._cache[key] = null;
if (this.options.useJsonP) {
this._loadTileP(zoom, xw, yw);
} else {
this._loadTile(zoom, xw, yw);
}
}
}
}
// If we still have requests for tiles that have now gone out of sight, attempt to abort them.
for (var req_key in this._requests) {
if (visible_tiles.indexOf(req_key) < 0) {
this._abort_request(req_key);
}
}
},
_loadTileP: function (zoom, x, y) {
var head = document.getElementsByTagName('head')[0],
key = zoom + '_' + x + '_' + y,
functionName = 'lu_' + key,
wk = this._windowKey,
self = this;
var url = L.Util.template(this._url, L.Util.extend({
s: L.TileLayer.prototype._getSubdomain.call(this, { x: x, y: y }),
z: zoom,
x: x,
y: y,
cb: wk + '.' + functionName
}, this.options));
var script = document.createElement('script');
script.setAttribute("type", "text/javascript");
script.setAttribute("src", url);
window[wk][functionName] = function (data) {
self._cache[key] = data;
delete window[wk][functionName];
if (script.parentElement===head) {
head.removeChild(script);
}
self._finish_request(key);
};
this._queue_request(key, url, function () {
head.appendChild(script);
return {
abort: function () {
head.removeChild(script);
}
};
});
},
_loadTile: function (zoom, x, y) {
var url = L.Util.template(this._url, L.Util.extend({
s: L.TileLayer.prototype._getSubdomain.call(this, { x: x, y: y }),
z: zoom,
x: x,
y: y
}, this.options));
var key = zoom + '_' + x + '_' + y;
this._queue_request(key, url, this._ajaxRequestFactory(key, url));
},
_ajaxRequestFactory: function (key, url) {
var successCallback = this._successCallbackFactory(key);
var errorCallback = this._errorCallbackFactory(url);
return function () {
var request = L.ajax(url, successCallback, errorCallback);
request.timeout = this.options.requestTimeout;
return request;
}.bind(this);
},
_successCallbackFactory: function (key) {
return function (data) {
this._cache[key] = data;
this._finish_request(key);
}.bind(this);
},
_errorCallbackFactory: function (tileurl) {
return function (statuscode) {
this.fire('tileerror', {
url: tileurl,
code: statuscode
});
}.bind(this);
},
_queue_request: function (key, url, callback) {
this._requests[key] = {
callback: callback,
timeout: null,
handler: null,
url: url
};
this._request_queue.push(key);
this._process_queued_requests();
},
_finish_request: function (key) {
// Remove from requests in process
var pos = this._requests_in_process.indexOf(key);
if (pos >= 0) {
this._requests_in_process.splice(pos, 1);
}
// Remove from request queue
pos = this._request_queue.indexOf(key);
if (pos >= 0) {
this._request_queue.splice(pos, 1);
}
// Remove the request entry
if (this._requests[key]) {
if (this._requests[key].timeout) {
window.clearTimeout(this._requests[key].timeout);
}
delete this._requests[key];
}
// Recurse
this._process_queued_requests();
// Fire 'load' event if all tiles have been loaded
if (this._requests_in_process.length === 0) {
this.fire('load');
}
},
_abort_request: function (key) {
// Abort the request if possible
if (this._requests[key] && this._requests[key].handler) {
if (typeof this._requests[key].handler.abort === 'function') {
this._requests[key].handler.abort();
}
}
// Ensure we don't keep a false copy of the data in the cache
if (this._cache[key] === null) {
delete this._cache[key];
}
// And remove the request
this._finish_request(key);
},
_process_queued_requests: function () {
while (this._request_queue.length > 0 && (this.options.maxRequests === 0 ||
this._requests_in_process.length < this.options.maxRequests)) {
this._process_request(this._request_queue.pop());
}
},
_process_request: function (key) {
this._requests_in_process.push(key);
// The callback might call _finish_request, so don't assume _requests[key] still exists.
var handler = this._requests[key].callback();
if (this._requests[key]) {
this._requests[key].handler = handler;
if (handler.timeout === undefined) {
var timeoutCallback = this._timeoutCallbackFactory(key);
this._requests[key].timeout = window.setTimeout(timeoutCallback, this.options.requestTimeout);
}
}
},
_timeoutCallbackFactory: function (key) {
var tileurl = this._requests[key].url;
return function () {
this.fire('tileerror', { url: tileurl, code: 'timeout' });
this._abort_request(key);
}.bind(this);
},
_utfDecode: function (c) {
if (c >= 93) {
c--;
}
if (c >= 35) {
c--;
}
return c - 32;
}
});
L.utfGrid = function (url, options) {
return new L.UtfGrid(url, options);
};