Buffersize customizable through named maps' placeholders
This commit is contained in:
parent
0577fa5308
commit
7ea7a991aa
@ -430,13 +430,15 @@ var _reNumber = /^([-+]?[\d\.]?\d+([eE][+-]?\d+)?)$/,
|
||||
_reCSSColorVal = /^#[0-9a-fA-F]{3,6}$/;
|
||||
|
||||
function _replaceVars (str, params) {
|
||||
//return _.template(str, params); // lazy way, possibly dangerous
|
||||
// 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]);
|
||||
});
|
||||
return str;
|
||||
}
|
||||
function isDictionary(val) {
|
||||
return ( _.isObject(val) && !_.isArray(val) && !_.isFunction(val));
|
||||
}
|
||||
TemplateMaps.prototype.instance = function(template, params) {
|
||||
var all_params = {};
|
||||
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);
|
||||
}
|
||||
}
|
||||
else if ( type === 'dictionary' ) {
|
||||
if (!isDictionary(val)) {
|
||||
throw new Error("Invalid dictionary value for template parameter '" + k + "': " + val);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// NOTE: should be checked at template create/update time
|
||||
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
|
||||
var layergroup = JSON.parse(JSON.stringify(template.layergroup));
|
||||
|
||||
if (layergroup.buffersize) {
|
||||
layergroup.buffersize = _replaceVars(layergroup.buffersize, all_params);
|
||||
if (typeof layergroup.buffersize === 'string') {
|
||||
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) {
|
||||
|
@ -2,7 +2,7 @@ require('../support/test_helper');
|
||||
|
||||
var assert = require('../support/assert');
|
||||
var TestClient = require('../support/test-client');
|
||||
var IMAGE_TOLERANCE_PER_MIL = 0;
|
||||
var IMAGE_TOLERANCE_PER_MIL = 5;
|
||||
var mapnik = require('windshaft').mapnik;
|
||||
|
||||
var CARTOCSS_LABELS = [
|
||||
@ -136,21 +136,21 @@ function createBufferSizeTemplate (name, buffersize, placeholders, cartocss) {
|
||||
"placeholders": placeholders || {
|
||||
"buffersize": {
|
||||
"type": "number",
|
||||
"default": "0"
|
||||
"default": 0
|
||||
}
|
||||
},
|
||||
"layergroup": createMapConfig(buffersize)
|
||||
}
|
||||
}
|
||||
|
||||
describe.only('buffer size per format for named maps', function () {
|
||||
describe('buffer size per format for named maps', function () {
|
||||
var testCases = [
|
||||
{
|
||||
desc: 'should get png tile using buffer-size 0 (default value in template)',
|
||||
coords: { z: 7, x: 64, y: 48 },
|
||||
format: '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.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',
|
||||
placeholders: { buffersize: 128 },
|
||||
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.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)',
|
||||
coords: { z: 7, x: 64, y: 48 },
|
||||
format: 'png',
|
||||
placeholders: { buffersize_png: 0 },
|
||||
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.png',
|
||||
template: createBufferSizeTemplate('named-default-buffer-size-by-format', {
|
||||
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)',
|
||||
coords: { z: 7, x: 64, y: 48 },
|
||||
format: 'png',
|
||||
placeholders: { buffersize: 128 },
|
||||
placeholders: { buffersize_png: 128 },
|
||||
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png',
|
||||
template: createBufferSizeTemplate('named-custom-buffer-size-by-format', {
|
||||
png: '<%= buffersize_png %>'
|
||||
@ -210,12 +211,13 @@ describe.only('buffer size per format for named maps', function () {
|
||||
var options = {
|
||||
format: test.format,
|
||||
placeholders: test.placeholders
|
||||
}
|
||||
};
|
||||
testClient.getNamedTile(coords.z, coords.x, coords.y, options, function (err, res, tile) {
|
||||
assert.ifError(err);
|
||||
// To generate images use:
|
||||
// tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-64.png');
|
||||
test.assert(tile, function () {
|
||||
//tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-0-test.png');
|
||||
test.assert(tile, function (err) {
|
||||
assert.ifError(err);
|
||||
testClient.drain(done);
|
||||
});
|
||||
});
|
||||
|
@ -711,7 +711,7 @@ TestClient.prototype.getNamedTile = function(z, x, y, params, callback) {
|
||||
}
|
||||
|
||||
if (!params.placeholders) {
|
||||
params.placeholder = {};
|
||||
params.placeholders = {};
|
||||
}
|
||||
|
||||
var urlNamed = '/api/v1/map/named';
|
||||
|
Loading…
Reference in New Issue
Block a user