diff --git a/src/serve_data.js b/src/serve_data.js index 523a89a..5c51cde 100644 --- a/src/serve_data.js +++ b/src/serve_data.js @@ -50,6 +50,10 @@ module.exports = function(options, repo, params, id, styles) { Object.assign(tileJSON, params.tilejson || {}); utils.fixTileJSONCenter(tileJSON); + + if (options.dataDecoratorFunc) { + tileJSON = options.dataDecoratorFunc(id, 'tilejson', tileJSON); + } resolve(); }); }); @@ -114,6 +118,13 @@ module.exports = function(options, repo, params, id, styles) { //console.log(shrinkers[style].getStats()); } } + if (options.dataDecoratorFunc) { + if (isGzipped) { + data = zlib.unzipSync(data); + isGzipped = false; + } + data = options.dataDecoratorFunc(id, 'data', data, z, x, y); + } } if (format == 'pbf') { headers['Content-Type'] = 'application/x-protobuf'; diff --git a/src/serve_rendered.js b/src/serve_rendered.js index 3c666aa..20a3db4 100644 --- a/src/serve_rendered.js +++ b/src/serve_rendered.js @@ -100,7 +100,8 @@ module.exports = function(options, repo, params, id, dataResolver) { }); } else if (protocol == 'mbtiles') { var parts = req.url.split('/'); - var source = map.sources[parts[2]]; + var sourceId = parts[2]; + var source = map.sources[sourceId]; var z = parts[3] | 0, x = parts[4] | 0, y = parts[5].split('.')[0] | 0, @@ -118,6 +119,10 @@ module.exports = function(options, repo, params, id, dataResolver) { if (format == 'pbf') { response.data = zlib.unzipSync(data); + if (options.dataDecoratorFunc) { + response.data = options.dataDecoratorFunc( + sourceId, 'data', response.data, z, x, y); + } } else { response.data = data; } @@ -258,6 +263,11 @@ module.exports = function(options, repo, params, id, dataResolver) { 'mbtiles://' + name + '/{z}/{x}/{y}.' + (info.format || 'pbf') ]; delete source.scheme; + + if (options.dataDecoratorFunc) { + source = options.dataDecoratorFunc(name, 'tilejson', source); + } + if (source.format == 'pbf') { map.sources[name].emptyTile = new Buffer(0); } else { diff --git a/src/server.js b/src/server.js index 04a9ea6..06164ee 100644 --- a/src/server.js +++ b/src/server.js @@ -89,6 +89,12 @@ function start(opts) { checkPath('sprites'); checkPath('mbtiles'); + if (options.dataDecorator) { + try { + options.dataDecoratorFunc = require(path.resolve(paths.root, options.dataDecorator)); + } catch (e) {} + } + var data = clone(config.data || {}); if (opts.cors) {