New approach: decorates named_map_provider to return the with visible layers

This commit is contained in:
Daniel García Aubert 2016-08-31 19:40:00 +02:00
parent ab27886460
commit 30c4b00f33

View File

@ -137,9 +137,13 @@ NamedMapsController.prototype.staticMap = function(req, res) {
this
);
},
function prepareImageOptions(err, _namedMapProvider) {
function layerVisibility(err, namedMapProvider) {
assert.ifError(err);
namedMapProvider = _namedMapProvider;
return decorateNamedMapProviderWithLayerVisibility(namedMapProvider);
},
function prepareImageOptions(err, decoratedNamedMapProvider) {
assert.ifError(err);
namedMapProvider = decoratedNamedMapProvider;
self.getStaticImageOptions(cdbUser, req.params, namedMapProvider, this);
},
function getImage(err, imageOpts) {
@ -196,6 +200,42 @@ function numMapper(n) {
return +n;
}
function decorateNamedMapProviderWithLayerVisibility (mamedMapProvider) {
var MapConfig = require('windshaft').model.MapConfig;
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 rawMapConfig = JSON.parse(JSON.stringify(mapConfig.obj()));
rawMapConfig.layers = rawMapConfig.layers.filter(function (layer, index) {
return previewLayers[''+index] !== false && previewLayers[layer.id] !== false;
});
var mapConfigWithVisibleLayers = new MapConfig.create(rawMapConfig);
callback(null, mapConfigWithVisibleLayers, rendererParams, context);
});
});
};
return mamedMapProvider;
}
NamedMapsController.prototype.getStaticImageOptions = function(cdbUser, params, namedMapProvider, callback) {
var self = this;