Applied approach similar to static image options

This commit is contained in:
Daniel García Aubert 2016-09-01 12:37:04 +02:00
parent e4a9f2d64c
commit e0d304b033

View File

@ -10,7 +10,7 @@ var cors = require('../middleware/cors');
var userMiddleware = require('../middleware/user'); var userMiddleware = require('../middleware/user');
function NamedMapsController(authApi, pgConnection, namedMapProviderCache, tileBackend, previewBackend, function NamedMapsController(authApi, pgConnection, namedMapProviderCache, tileBackend, previewBackend,
surrogateKeysCache, tablesExtentApi, metadataBackend) { surrogateKeysCache, tablesExtentApi, metadataBackend, templateMaps) {
BaseController.call(this, authApi, pgConnection); BaseController.call(this, authApi, pgConnection);
this.namedMapProviderCache = namedMapProviderCache; this.namedMapProviderCache = namedMapProviderCache;
@ -19,6 +19,7 @@ function NamedMapsController(authApi, pgConnection, namedMapProviderCache, tileB
this.surrogateKeysCache = surrogateKeysCache; this.surrogateKeysCache = surrogateKeysCache;
this.tablesExtentApi = tablesExtentApi; this.tablesExtentApi = tablesExtentApi;
this.metadataBackend = metadataBackend; this.metadataBackend = metadataBackend;
this.templateMaps = templateMaps;
} }
util.inherits(NamedMapsController, BaseController); util.inherits(NamedMapsController, BaseController);
@ -126,6 +127,23 @@ NamedMapsController.prototype.staticMap = function(req, res) {
function reqParams() { function reqParams() {
self.req2params(req, this); self.req2params(req, this);
}, },
function getLayerFilterFromPreviewLayers (err) {
assert.ifError(err);
var next = this;
self.getLayerFilterFromPreviewLayers(cdbUser, req.params.template_id, next);
},
function applyPreviewLayers (err, layerVisibilityFilter) {
assert.ifError(err);
if (layerVisibilityFilter) {
// overwrite 'all' default filter
req.params.layer = layerVisibilityFilter;
}
return true;
},
function getNamedMapProvider(err) { function getNamedMapProvider(err) {
assert.ifError(err); assert.ifError(err);
self.namedMapProviderCache.get( self.namedMapProviderCache.get(
@ -137,13 +155,9 @@ NamedMapsController.prototype.staticMap = function(req, res) {
this this
); );
}, },
function layerVisibility(err, namedMapProvider) { function prepareImageOptions(err, _namedMapProvider) {
assert.ifError(err); assert.ifError(err);
return decorateNamedMapProviderWithLayerVisibility(namedMapProvider); namedMapProvider = _namedMapProvider;
},
function prepareImageOptions(err, decoratedNamedMapProvider) {
assert.ifError(err);
namedMapProvider = decoratedNamedMapProvider;
self.getStaticImageOptions(cdbUser, req.params, namedMapProvider, this); self.getStaticImageOptions(cdbUser, req.params, namedMapProvider, this);
}, },
function getImage(err, imageOpts) { function getImage(err, imageOpts) {
@ -188,6 +202,53 @@ NamedMapsController.prototype.staticMap = function(req, res) {
); );
}; };
NamedMapsController.prototype.getLayerFilterFromPreviewLayers = function (cdbUser, template_id, callback) {
var self = this;
step(
function getTemplate () {
var next = this;
self.templateMaps.getTemplate(cdbUser, template_id, next);
},
function getLayerVisivilityFilter (err, template) {
assert.ifError(err);
var next = this;
if (!template || !template.view || !template.view.preview_layers) {
return next();
}
var previewLayers = template.view.preview_layers;
var layerVisibilityFilter = [];
template.layergroup.layers.forEach(function (layer, index) {
if (previewLayers[''+index] !== false && previewLayers[layer.id] !== false) {
layerVisibilityFilter.push(''+index);
}
});
if (!layerVisibilityFilter.length) {
return next();
}
next(null, layerVisibilityFilter);
},
function finish (err, layerVisibilityFilter) {
if (err) {
return callback(err);
}
if (!layerVisibilityFilter) {
return callback();
}
callback(null, layerVisibilityFilter.join(','));
}
);
};
var DEFAULT_ZOOM_CENTER = { var DEFAULT_ZOOM_CENTER = {
zoom: 1, zoom: 1,
center: { center: {
@ -200,46 +261,6 @@ function numMapper(n) {
return +n; return +n;
} }
function decorateNamedMapProviderWithLayerVisibility (mamedMapProvider) {
var orginalGetMapConfig = mamedMapProvider.getMapConfig.bind(mamedMapProvider);
mamedMapProvider.getMapConfig = function (callback) {
mamedMapProvider.getTemplate(function (err, template) {
if (err) {
return callback(err);
}
if (!template.preview_layers) {
return orginalGetMapConfig(callback);
}
var previewLayers = template.preview_layers;
orginalGetMapConfig(function (err, mapConfig, rendererParams, context) {
if (err) {
return callback(err);
}
var layerVisibilityFilter = [];
mapConfig.getLayers().forEach(function (layer, index) {
if (previewLayers[''+index] !== false && previewLayers[layer.id] !== false) {
layerVisibilityFilter.push(''+index);
}
});
if (layerVisibilityFilter.length) {
// overwrite 'all' default filter
rendererParams.layer = layerVisibilityFilter.join(',');
}
callback(null, mapConfig, rendererParams, context);
});
});
};
return mamedMapProvider;
}
NamedMapsController.prototype.getStaticImageOptions = function(cdbUser, params, namedMapProvider, callback) { NamedMapsController.prototype.getStaticImageOptions = function(cdbUser, params, namedMapProvider, callback) {
var self = this; var self = this;