This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autogen.js
67 lines (60 loc) · 2.12 KB
/
autogen.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
'use strict';
let Promise = require('bluebird'),
checkType = require('@kartotherian/input-validator'),
core;
function Autogen(uri, callback) {
let self = this,
query;
Promise.try(() => {
query = checkType.normalizeUrl(uri).query;
checkType(query, 'mingen', 'zoom');
self.mingen = query.mingen;
checkType(query, 'maxgen', 'zoom');
self.maxgen = query.maxgen;
checkType(query, 'minstore', 'zoom');
self.minstore = query.minstore;
checkType(query, 'maxstore', 'zoom');
self.maxstore = query.maxstore;
checkType(query, 'storage', 'string', true);
checkType(query, 'generator', 'string', true);
return core.loadSource(query.storage);
}).then(storage => {
self.storage = storage;
return core.loadSource(query.generator);
}).then(generator => {
self.generator = generator;
}).return(this).nodeify(callback);
}
Autogen.prototype.getTile = function(z, x, y, callback) {
let self = this;
return self.storage
.getTileAsync(z, x, y)
.catch(err => {
if ((self.mingen !== undefined && z < self.mingen) ||
(self.maxgen !== undefined && z > self.maxgen) ||
!core.isNoTileError(err)
) {
throw err;
}
let p = self.generator.getTileAsync(z, x, y);
if (
(self.minstore === undefined || z >= self.minstore) &&
(self.maxstore === undefined || z <= self.maxstore)
) {
// on error, log and ignore
p = p.spread((tile, headers) =>
self.storage.putTileAsync(z, x, y, tile)
.catch(err => core.log('error', err))
.return([tile, headers]));
}
return p;
}).nodeify(callback, { spread: true });
};
Autogen.prototype.getInfo = function(callback) {
return this.storage.getInfo(callback);
};
Autogen.initKartotherian = function(cor) {
core = cor;
core.tilelive.protocols['autogen:'] = Autogen;
};
module.exports = Autogen;