Buffersize customizable through named maps' placeholders

This commit is contained in:
Mario de Frutos 2017-04-25 19:27:31 +02:00
parent 0577fa5308
commit 7ea7a991aa
3 changed files with 40 additions and 27 deletions

View File

@ -430,13 +430,15 @@ var _reNumber = /^([-+]?[\d\.]?\d+([eE][+-]?\d+)?)$/,
_reCSSColorVal = /^#[0-9a-fA-F]{3,6}$/; _reCSSColorVal = /^#[0-9a-fA-F]{3,6}$/;
function _replaceVars (str, params) { function _replaceVars (str, params) {
//return _.template(str, params); // lazy way, possibly dangerous
// Construct regular expressions for each param // Construct regular expressions for each param
Object.keys(params).forEach(function(k) { Object.keys(params).forEach(function(k) {
str = str.replace(new RegExp("<%=\\s*" + k + "\\s*%>", "g"), params[k]); str = str.replace(new RegExp("<%=\\s*" + k + "\\s*%>", "g"), params[k]);
}); });
return str; return str;
} }
function isDictionary(val) {
return ( _.isObject(val) && !_.isArray(val) && !_.isFunction(val));
}
TemplateMaps.prototype.instance = function(template, params) { TemplateMaps.prototype.instance = function(template, params) {
var all_params = {}; var all_params = {};
var phold = template.placeholders || {}; var phold = template.placeholders || {};
@ -465,6 +467,11 @@ TemplateMaps.prototype.instance = function(template, params) {
throw new Error("Invalid css_color value for template parameter '" + k + "': " + val); throw new Error("Invalid css_color value for template parameter '" + k + "': " + val);
} }
} }
else if ( type === 'dictionary' ) {
if (!isDictionary(val)) {
throw new Error("Invalid dictionary value for template parameter '" + k + "': " + val);
}
}
else { else {
// NOTE: should be checked at template create/update time // NOTE: should be checked at template create/update time
throw new Error("Invalid placeholder type '" + type + "'"); throw new Error("Invalid placeholder type '" + type + "'");
@ -475,8 +482,12 @@ TemplateMaps.prototype.instance = function(template, params) {
// NOTE: we're deep-cloning the layergroup here // NOTE: we're deep-cloning the layergroup here
var layergroup = JSON.parse(JSON.stringify(template.layergroup)); var layergroup = JSON.parse(JSON.stringify(template.layergroup));
if (layergroup.buffersize) { if (typeof layergroup.buffersize === 'string') {
layergroup.buffersize = _replaceVars(layergroup.buffersize, all_params); layergroup.buffersize = Number(_replaceVars(layergroup.buffersize, all_params));
} else if (isDictionary(layergroup.buffersize)) {
Object.keys(layergroup.buffersize).forEach(function(k) {
layergroup.buffersize[k] = Number(_replaceVars(layergroup.buffersize[k], all_params));
});
} }
for (var i=0; i<layergroup.layers.length; ++i) { for (var i=0; i<layergroup.layers.length; ++i) {

View File

@ -2,7 +2,7 @@ require('../support/test_helper');
var assert = require('../support/assert'); var assert = require('../support/assert');
var TestClient = require('../support/test-client'); var TestClient = require('../support/test-client');
var IMAGE_TOLERANCE_PER_MIL = 0; var IMAGE_TOLERANCE_PER_MIL = 5;
var mapnik = require('windshaft').mapnik; var mapnik = require('windshaft').mapnik;
var CARTOCSS_LABELS = [ var CARTOCSS_LABELS = [
@ -136,21 +136,21 @@ function createBufferSizeTemplate (name, buffersize, placeholders, cartocss) {
"placeholders": placeholders || { "placeholders": placeholders || {
"buffersize": { "buffersize": {
"type": "number", "type": "number",
"default": "0" "default": 0
} }
}, },
"layergroup": createMapConfig(buffersize) "layergroup": createMapConfig(buffersize)
} }
} }
describe.only('buffer size per format for named maps', function () { describe('buffer size per format for named maps', function () {
var testCases = [ var testCases = [
{ {
desc: 'should get png tile using buffer-size 0 (default value in template)', desc: 'should get png tile using buffer-size 0 (default value in template)',
coords: { z: 7, x: 64, y: 48 }, coords: { z: 7, x: 64, y: 48 },
format: 'png', format: 'png',
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.png', fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.png',
template: createBufferSizeTemplate('named-default-buffer-size'), template: createBufferSizeTemplate('named-default-buffer-size', '<%= buffersize %>'),
assert: function (tile, callback) { assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback); assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
} }
@ -161,7 +161,7 @@ describe.only('buffer size per format for named maps', function () {
format: 'png', format: 'png',
placeholders: { buffersize: 128 }, placeholders: { buffersize: 128 },
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png', fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png',
template: createBufferSizeTemplate('named-custom-buffer-size'), template: createBufferSizeTemplate('named-custom-buffer-size', '<%= buffersize %>'),
assert: function (tile, callback) { assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback); assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
} }
@ -170,6 +170,7 @@ describe.only('buffer size per format for named maps', function () {
desc: 'should get png tile using buffer-size 0 (default value in template by format)', desc: 'should get png tile using buffer-size 0 (default value in template by format)',
coords: { z: 7, x: 64, y: 48 }, coords: { z: 7, x: 64, y: 48 },
format: 'png', format: 'png',
placeholders: { buffersize_png: 0 },
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.png', fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.png',
template: createBufferSizeTemplate('named-default-buffer-size-by-format', { template: createBufferSizeTemplate('named-default-buffer-size-by-format', {
png: '<%= buffersize_png %>' png: '<%= buffersize_png %>'
@ -187,7 +188,7 @@ describe.only('buffer size per format for named maps', function () {
desc: 'should get png tile using buffer-size 128 (placehoder value in template by format)', desc: 'should get png tile using buffer-size 128 (placehoder value in template by format)',
coords: { z: 7, x: 64, y: 48 }, coords: { z: 7, x: 64, y: 48 },
format: 'png', format: 'png',
placeholders: { buffersize: 128 }, placeholders: { buffersize_png: 128 },
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png', fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png',
template: createBufferSizeTemplate('named-custom-buffer-size-by-format', { template: createBufferSizeTemplate('named-custom-buffer-size-by-format', {
png: '<%= buffersize_png %>' png: '<%= buffersize_png %>'
@ -210,12 +211,13 @@ describe.only('buffer size per format for named maps', function () {
var options = { var options = {
format: test.format, format: test.format,
placeholders: test.placeholders placeholders: test.placeholders
} };
testClient.getNamedTile(coords.z, coords.x, coords.y, options, function (err, res, tile) { testClient.getNamedTile(coords.z, coords.x, coords.y, options, function (err, res, tile) {
assert.ifError(err); assert.ifError(err);
// To generate images use: // To generate images use:
// tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-64.png'); //tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-0-test.png');
test.assert(tile, function () { test.assert(tile, function (err) {
assert.ifError(err);
testClient.drain(done); testClient.drain(done);
}); });
}); });

View File

@ -711,7 +711,7 @@ TestClient.prototype.getNamedTile = function(z, x, y, params, callback) {
} }
if (!params.placeholders) { if (!params.placeholders) {
params.placeholder = {}; params.placeholders = {};
} }
var urlNamed = '/api/v1/map/named'; var urlNamed = '/api/v1/map/named';