-
Notifications
You must be signed in to change notification settings - Fork 20
/
tile-set.js
81 lines (73 loc) · 2.13 KB
/
tile-set.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
var extend = require('extend'),
LRU = require('lru-cache'),
loadTile = require('./load-tile'),
ImagicoElevationDownloader = require('./imagico'),
_latLng = require('./latlng'),
tileKey = require('./tile-key');
function TileSet(tileDir, options) {
this.options = extend({}, {
loadTile: loadTile,
downloader: new ImagicoElevationDownloader(tileDir)
}, options);
if (options && options.downloader === undefined) {
this.options.downloader = undefined;
}
this._tileDir = tileDir;
this._tileCache = LRU({
max: 1000,
dispose: function (key, n) {
if(n) {
n.destroy();
}
}
});
this._loadingTiles = {};
}
TileSet.prototype.destroy = function() {
this._tileCache.reset();
delete this._tileCache;
};
TileSet.prototype.getElevation = function(latLng, cb) {
var getTileElevation = function(tile, ll) {
cb(undefined, tile.getElevation(ll));
},
ll = _latLng(latLng),
key = tileKey(ll),
tile = this._tileCache.get(key);
if (tile) {
setImmediate(function() {
getTileElevation(tile, ll);
});
} else {
this._loadTile(key, ll, function(err, tile) {
if (!err) {
getTileElevation(tile, ll);
} else {
cb(err);
}
});
}
};
TileSet.prototype._loadTile = function(tileKey, latLng, cb) {
var loadQueue = this._loadingTiles[tileKey];
if (!loadQueue) {
loadQueue = [];
this._loadingTiles[tileKey] = loadQueue;
this.options.loadTile.call(this, this._tileDir, latLng, function(err, tile) {
var q = this._loadingTiles[tileKey];
if(!err) {
this._tileCache.set(tileKey, tile);
}
q.forEach(function(cb) {
if (err) {
cb(err);
} else {
cb(undefined, tile);
}
});
delete this._loadingTiles[tileKey];
}.bind(this));
}
loadQueue.push(cb);
};
module.exports = TileSet;