-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
69 lines (59 loc) · 2.53 KB
/
index.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
var url = require('url');
var getUri = require('./lib/get-uri');
var tilelivecopy = require('./lib/tilelivecopy');
var serialtilescopy = require('./lib/serialtilescopy');
var s3urls = require('@mapbox/s3urls');
var tilelive = require('@mapbox/tilelive');
var Vector = require('@mapbox/tilelive-vector');
var MBTiles = require('@mapbox/mbtiles');
var Omnivore = require('@mapbox/tilelive-omnivore');
var TileJSON = require('@mapbox/tilejson');
var Mapbox = require('./lib/tilelive-mapbox');
var S3 = require('@mapbox/tilelive-s3');
var tl_file = require('tilelive-file');
var path = require('path');
// Note: tilelive-vector is needed for `tm2z` protocol (https://github.com/mapbox/tilelive-vector/issues/124)
Vector.registerProtocols(tilelive);
MBTiles.registerProtocols(tilelive);
Omnivore.registerProtocols(tilelive);
TileJSON.registerProtocols(tilelive);
Mapbox.registerProtocols(tilelive);
S3.registerProtocols(tilelive);
tl_file.registerProtocols(tilelive);
var mapnik = require('mapnik');
mapnik.Logger.setSeverity(mapnik.Logger.NONE);
mapnik.register_fonts(path.dirname(require.resolve('mapbox-studio-default-fonts')), { recurse: true });
mapnik.register_fonts(path.dirname(require.resolve('mapbox-studio-pro-fonts')), { recurse: true });
if (process.env.MapboxTileCopyFonts)
mapnik.register_fonts(process.env.MapboxTileCopyFonts, { recurse: true });
module.exports = function(filepath, dsturi, options, callback) {
if (s3urls.valid(dsturi)) {
// Make sure the s3url is of type s3://bucket/key
var query = url.parse(dsturi).query;
dsturi = s3urls.convert(dsturi, 's3');
if (query) dsturi += '?' + query;
if (dsturi.indexOf('{z}') == -1 ||
dsturi.indexOf('{x}') == -1 ||
dsturi.indexOf('{y}') == -1) return callback(new Error('Destination URL does not include a {z}/{x}/{y} template.'));
} else if (dsturi.indexOf('file://') == -1) {
return callback(new Error('Invalid output protocol: ' + dsturi));
}
if (options.bundle === true) {
tilelivecopy(filepath, dsturi, options, copied);
} else {
getUri(filepath, options.layerName, function(err, srcUri) {
if (err) return callback(err);
if (url.parse(srcUri).protocol === 'serialtiles:') {
serialtilescopy(srcUri, dsturi, options, copied);
} else {
tilelivecopy(srcUri, dsturi, options, copied);
}
});
}
function copied(err, stats) {
if (!err) return callback(err, stats);
var fatal = { SQLITE_CORRUPT: true, EINVALIDTILE: true };
if (fatal[err.code]) err.code = 'EINVALID';
return callback(err);
}
};