-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilepin.js
More file actions
216 lines (206 loc) · 7.1 KB
/
Copy pathtilepin.js
File metadata and controls
216 lines (206 loc) · 7.1 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
var _ = require('underscore');
var Path = require('path')
var Url = require('url');
var FS = require('fs');
var Crypto = require('crypto');
var Commons = require('./tilepin-commons');
var P = Commons.P;
var LRU = require('lru-cache');
var TileSourceManager = require('./tilepin-provider');
function TilesProvider(options) {
this.options = options || {};
this.cache = options.cache || new MemCache();
this.tileSourceManager = new TileSourceManager(this.options);
var eventManager = this._getEventManager();
Commons.addEventTracing(this, [ 'invalidate', 'loadTile', 'loadInfo' ],
eventManager);
}
_.extend(TilesProvider.prototype, Commons.Events, {
invalidate : function(params) {
var that = this;
var sourceKey = that._getSourceKey(params);
var resetAll = true;
var cacheKeys = [];
return P().then(function() {
return that.tileSourceManager //
.loadTileSourceProvider(params) //
.then(function(provider) {
return that._getFormats(params).then(function(formats) {
return P.all(_.map(formats, function(format) {
var cacheKey = provider.getCacheKey(params, format);
cacheKeys.push(cacheKey);
}));
});
});
}).then(function() {
return P.all([ invalidateCache(), clearTileSourceProvider() ]);
}, function(err) {
return P.all([ invalidateCache(), clearTileSourceProvider() ]);
throw err;
});
function invalidateCache() {
if (cacheKeys.length) {
return P.all(_.map(cacheKeys, function(cacheKey) {
return that.cache.reset(sourceKey, cacheKey);
}));
} else {
return that.cache.reset(sourceKey);
}
}
function clearTileSourceProvider() {
return that.tileSourceManager.clearTileSourceProvider(params);
}
},
loadTile : function(params) {
var that = this;
function loadNonCached(provider, params) {
return provider.prepareTileSource(params) //
.then(function(tileSource) {
return that._readTile(tileSource, params);
})
}
function loadCached(provider, params) {
var sourceKey = that._getSourceKey(params);
var format = params.format;
var cacheKey = provider.getCacheKey(params, format);
return that.cache.get(sourceKey, cacheKey).then(function(result) {
if (result && !that._forceTileInvalidation(params))
return result;
return loadNonCached(provider, params).then(function(result) {
return that.cache.set(sourceKey, cacheKey, result) //
.then(function() {
return result;
});
});
})
}
return that.tileSourceManager.loadTileSourceProvider(params).then(
function(provider) {
// TODO: replace it by caching timeout
if (!provider.isDynamicSource()) {
return loadCached(provider, params);
} else {
return loadNonCached(provider, params);
}
});
},
loadInfo : function(params) {
var that = this;
return that.tileSourceManager.loadTileSourceProvider(params) //
.then(function(provider) {
return provider.prepareTileSource(params);
}).then(function(tileSource) {
return P.ninvoke(tileSource, 'getInfo');
});
},
_getFormats : function(params) {
var result = [ 'grid.json', 'png', 'vtile' ];
if (params && _.isArray(params.formats)) {
result = _.filter(result, function(format) {
return _.contains(params.formats, format);
})
}
return P(result);
},
_getSourceKey : function(params, suffix) {
var key = params.source || '';
if (suffix) {
key += suffix;
}
return key;
},
_getEventManager : function() {
var eventManager = this.options.eventManager || this;
return eventManager;
},
_forceTileInvalidation : function(params) {
return !!params.reload;
},
_prepareUtfGrid : function(tile) {
function toObject(value){
if (_.isString(value) && value[0] == '{') {
try {
value = JSON.parse(value);
} catch (e) {
}
}
return value;
}
_.each(tile.data, function(obj) {
_.each(_.keys(obj), function(key) {
var value = obj[key];
if (_.isString(value)) {
obj[key] = value.replace(/\s/gim, function(match) {
if (match == '\n' || match == '\r' || match == ' ')
return match;
return ' ';
});
}
})
obj.properties = toObject(obj.properties);
obj.geometry = toObject(obj.geometry);
})
return tile;
},
_readTile : function(tileSource, params) {
var deferred = P.defer();
try {
var format = params.format;
var z = params.z;
var x = +params.x;
var y = +params.y;
var fn = format === 'grid.json' ? 'getGrid' : 'getTile';
var that = this;
tileSource[fn](z, x, y, function(err, tile, headers) {
if (err) {
deferred.reject(err);
} else {
if (format === 'grid.json') {
tile = that._prepareUtfGrid(tile);
}
deferred.resolve({
tile : tile,
headers : headers
})
}
})
} catch (e) {
deferred.reject(e);
}
return deferred.promise;
},
});
function MemCache(options) {
this.options = options || {};
this.cache = new LRU(this.options);
}
_.extend(MemCache.prototype, {
reset : function(sourceKey, tileKey) {
if (tileKey && tileKey != '') {
var cache = this.cache.get(sourceKey);
if (cache) {
cache.del(tileKey);
}
} else {
this.cache.del(sourceKey);
}
return P();
},
get : function(sourceKey, tileKey) {
var cache = this.cache.get(sourceKey);
var result = cache ? cache.get(tileKey) : undefined;
return P(result);
},
set : function(sourceKey, tileKey, tile) {
var cache = this.cache.get(sourceKey);
if (!cache) {
cache = new LRU(this.options);
this.cache.set(sourceKey, cache);
}
return P(cache.set(tileKey, tile));
},
})
module.exports = {
TilesProvider : TilesProvider,
MemCache : MemCache
}