This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
L.TileLayer.PouchDBCached.js
352 lines (313 loc) · 9.02 KB
/
L.TileLayer.PouchDBCached.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
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
// HTMLCanvasElement.toBlob() polyfill
// copy-pasted off https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob
import PouchDB from 'pouchdb';
if (!HTMLCanvasElement.prototype.toBlob) {
Object.defineProperty(HTMLCanvasElement.prototype, "toBlob", {
value: function(callback, type, quality) {
var dataURL = this.toDataURL(type, quality).split(",")[1];
setTimeout(function() {
var binStr = atob(dataURL),
len = binStr.length,
arr = new Uint8Array(len);
for (var i = 0; i < len; i++) {
arr[i] = binStr.charCodeAt(i);
}
callback(new Blob([arr], { type: type || "image/png" }));
});
},
});
}
L.TileLayer.addInitHook(function() {
if (!this.options.useCache) {
this._db = null;
return;
}
this._db = new PouchDB("offline-tiles");
});
// 🍂namespace TileLayer
// 🍂section PouchDB tile caching options
// 🍂option useCache: Boolean = false
// Whether to use a PouchDB cache on this tile layer, or not
L.TileLayer.prototype.options.useCache = false;
// 🍂option saveToCache: Boolean = true
// When caching is enabled, whether to save new tiles to the cache or not
L.TileLayer.prototype.options.saveToCache = true;
// 🍂option useOnlyCache: Boolean = false
// When caching is enabled, whether to request new tiles from the network or not
L.TileLayer.prototype.options.useOnlyCache = false;
// 🍂option cacheFormat: String = 'image/png'
// The image format to be used when saving the tile images in the cache
L.TileLayer.prototype.options.cacheFormat = "image/png";
// 🍂option cacheMaxAge: Number = 24*3600*1000
// Maximum age of the cache, in milliseconds
L.TileLayer.prototype.options.cacheMaxAge = 24 * 3600 * 1000;
L.TileLayer.include({
// Overwrites L.TileLayer.prototype.createTile
createTile: function(coords, done) {
var tile = document.createElement("img");
tile.onerror = L.bind(this._tileOnError, this, done, tile);
if (this.options.crossOrigin) {
tile.crossOrigin = "";
}
/*
Alt tag is *set to empty string to keep screen readers from reading URL and for compliance reasons
http://www.w3.org/TR/WCAG20-TECHS/H67
*/
tile.alt = "";
var tileUrl = this.getTileUrl(coords);
var cacheName = this._maskURL(tileUrl);
if (this.options.useCache) {
this._db.get(
cacheName,
{ revs_info: true },
this._onCacheLookup(tile, tileUrl, done)
);
} else {
// Fall back to standard behaviour
tile.onload = L.bind(this._tileOnLoad, this, done, tile);
tile.src = tileUrl;
}
return tile;
},
_maskURL: function(tileUrl) {
// Mask the access token.
if (this.options.cacheURLMask) {
var cacheName = tileUrl.replace(this.options.cacheURLMask, "");
return cacheName;
}
return tileUrl;
},
// Returns a callback (closure over tile/key/originalSrc) to be run when the DB
// backend is finished with a fetch operation.
_onCacheLookup: function(tile, tileUrl, done) {
return function(err, data) {
if (data) {
return this._onCacheHit(tile, tileUrl, data, done);
} else {
return this._onCacheMiss(tile, tileUrl, done);
}
}.bind(this);
},
_onCacheHit: function(tile, tileUrl, data, done) {
this.fire("tilecachehit", {
tile: tile,
url: tileUrl,
});
// Read the attachment as blob
this._db.getAttachment(data._id, "tile").then(
function(blob) {
var url = URL.createObjectURL(blob);
if (
Date.now() > data.timestamp + this.options.cacheMaxAge &&
!this.options.useOnlyCache
) {
// Tile is too old, try to refresh it
console.log("Tile is too old: ", tileUrl);
if (this.options.saveToCache) {
tile.onload = L.bind(
this._saveTile,
this,
tile,
tileUrl,
data._revs_info[0].rev,
done
);
}
tile.crossOrigin = "Anonymous";
tile.src = tileUrl;
tile.onerror = function(ev) {
// If the tile is too old but couldn't be fetched from the network,
// serve the one still in cache.
this.src = url;
};
} else {
// Serve tile from cached data
//console.log('Tile is cached: ', tileUrl);
tile.onload = L.bind(this._tileOnLoad, this, done, tile);
tile.src = url;
}
}.bind(this)
);
},
_onCacheMiss: function(tile, tileUrl, done) {
this.fire("tilecachemiss", {
tile: tile,
url: tileUrl,
});
if (this.options.useOnlyCache) {
// Offline, not cached
// console.log('Tile not in cache', tileUrl);
tile.onload = L.Util.falseFn;
tile.src = L.Util.emptyImageUrl;
} else {
// Online, not cached, request the tile normally
// console.log('Requesting tile normally', tileUrl);
if (this.options.saveToCache) {
tile.onload = L.bind(
this._saveTile,
this,
tile,
tileUrl,
undefined,
done
);
} else {
tile.onload = L.bind(this._tileOnLoad, this, done, tile);
}
tile.crossOrigin = "Anonymous";
tile.src = tileUrl;
}
},
// Async'ly saves the tile as a PouchDB attachment
// Will run the done() callback (if any) when finished.
_saveTile: function(tile, tileUrl, existingRevision, done) {
if (!this.options.saveToCache) {
return;
}
var canvas = document.createElement("canvas");
canvas.width = tile.naturalWidth || tile.width;
canvas.height = tile.naturalHeight || tile.height;
var context = canvas.getContext("2d");
context.drawImage(tile, 0, 0);
var format = this.options.cacheFormat;
canvas.toBlob(
function(blob) {
var maskedId = this._maskURL(tileUrl);
this._db
.put({
_id: maskedId,
_rev: existingRevision,
timestamp: Date.now(),
})
.then(
function(status) {
return this._db.putAttachment(
maskedId,
"tile",
status.rev,
blob,
format
);
}.bind(this)
)
.then(function(resp) {
if (done) {
done();
}
})
.catch(function() {
// Saving the tile to the cache might have failed,
// but the tile itself has been loaded.
if (done) {
done();
}
});
}.bind(this),
format
);
},
// 🍂section PouchDB tile caching methods
// 🍂method seed(bbox: LatLngBounds, minZoom: Number, maxZoom: Number): this
// Starts seeding the cache given a bounding box and the minimum/maximum zoom levels
// Use with care! This can spawn thousands of requests and flood tileservers!
seed: function(bbox, minZoom, maxZoom) {
if (!this.options.useCache) return;
if (minZoom > maxZoom) return;
if (!this._map) return;
var queue = [];
for (var z = minZoom; z <= maxZoom; z++) {
// Geo bbox to pixel bbox (as per given zoom level)...
var northEastPoint = this._map.project(bbox.getNorthEast(), z);
var southWestPoint = this._map.project(bbox.getSouthWest(), z);
// Then to tile coords bounds, as per GridLayer
var tileBounds = this._pxBoundsToTileRange(
L.bounds([northEastPoint, southWestPoint])
);
for (var j = tileBounds.min.y; j <= tileBounds.max.y; j++) {
for (var i = tileBounds.min.x; i <= tileBounds.max.x; i++) {
var point = new L.Point(i, j);
point.z = z;
queue.push(this._getTileUrl(point));
}
}
}
var seedData = {
bbox: bbox,
minZoom: minZoom,
maxZoom: maxZoom,
queueLength: queue.length,
};
this.fire("seedstart", seedData);
var tile = this._createTile();
tile._layer = this;
this._seedOneTile(tile, queue, seedData);
return this;
},
_createTile: function() {
return document.createElement("img");
},
// Modified L.TileLayer.getTileUrl, this will use the zoom given by the parameter coords
// instead of the maps current zoomlevel.
_getTileUrl: function(coords) {
var zoom = coords.z;
if (this.options.zoomReverse) {
zoom = this.options.maxZoom - zoom;
}
zoom += this.options.zoomOffset;
return L.Util.template(
this._url,
L.extend(
{
r:
this.options.detectRetina &&
L.Browser.retina &&
this.options.maxZoom > 0
? "@2x"
: "",
s: this._getSubdomain(coords),
x: coords.x,
y: this.options.tms
? this._globalTileRange.max.y - coords.y
: coords.y,
z: this.options.maxNativeZoom
? Math.min(zoom, this.options.maxNativeZoom)
: zoom,
},
this.options
)
);
},
// Uses a defined tile to eat through one item in the queue and
// asynchronously recursively call itself when the tile has
// finished loading.
_seedOneTile: function(tile, remaining, seedData) {
if (!remaining.length) {
this.fire("seedend", seedData);
return;
}
this.fire("seedprogress", {
bbox: seedData.bbox,
minZoom: seedData.minZoom,
maxZoom: seedData.maxZoom,
queueLength: seedData.queueLength,
remainingLength: remaining.length,
});
var url = remaining.shift();
this._db.get(
url,
function(err, data) {
if (!data) {
/// FIXME: Do something on tile error!!
tile.onload = function(ev) {
this._saveTile(tile, url, null); //(ev)
this._seedOneTile(tile, remaining, seedData);
}.bind(this);
tile.crossOrigin = "Anonymous";
tile.src = url;
} else {
this._seedOneTile(tile, remaining, seedData);
}
}.bind(this)
);
},
});