Windshaft-cartodb/test/acceptance/buffer-size-format-test.js

515 lines
19 KiB
JavaScript
Raw Normal View History

'use strict';
require('../support/test-helper');
var fs = require('fs');
var assert = require('../support/assert');
var TestClient = require('../support/test-client');
var serverOptions = require('../../lib/server-options');
var mapnik = require('windshaft').mapnik;
var IMAGE_TOLERANCE_PER_MIL = 5;
var CARTOCSS_LABELS = [
'#layer {',
' polygon-fill: #374C70;',
' polygon-opacity: 0.9;',
' line-width: 1;',
' line-color: #FFF;',
' line-opacity: 0.5;',
'}',
'#layer::labels {',
' text-name: [name];',
' text-face-name: \'DejaVu Sans Book\';',
' text-size: 20;',
' text-fill: #FFFFFF;',
' text-label-position-tolerance: 0;',
' text-halo-radius: 1;',
' text-halo-fill: #6F808D;',
' text-dy: -10;',
' text-allow-overlap: true;',
' text-placement: point;',
' text-placement-type: dummy;',
'}'
2017-04-25 23:54:31 +08:00
].join('\n');
function createMapConfig (bufferSize, cartocss) {
cartocss = cartocss || CARTOCSS_LABELS;
return {
version: '1.6.0',
buffersize: bufferSize,
layers: [{
2019-10-22 01:07:24 +08:00
type: 'cartodb',
options: {
sql: [
'select',
' *',
'from',
2019-10-22 01:07:24 +08:00
' populated_places_simple_reduced'
].join('\n'),
cartocss: cartocss,
cartocss_version: '2.3.0',
interactivity: 'cartodb_id'
}
}]
};
}
2017-04-25 00:57:20 +08:00
describe('buffer size per format', function () {
2018-10-17 16:06:48 +08:00
let testClient;
var testCases = [
{
2017-04-25 16:43:07 +08:00
desc: 'should get png tile using buffer-size 0',
coords: { z: 7, x: 64, y: 48 },
format: 'png',
2017-04-25 16:43:07 +08:00
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.png',
mapConfig: createMapConfig({ png: 0, 'grid.json': 0 }),
assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
}
},
{
desc: 'should get png tile using buffer-size 128',
coords: { z: 7, x: 64, y: 48 },
format: 'png',
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png',
mapConfig: createMapConfig({ png: 128, 'grid.json': 128 }),
assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
}
},
{
2017-04-25 16:43:07 +08:00
desc: 'should get mvt tile using buffer-size 0',
coords: { z: 7, x: 64, y: 48 },
format: 'mvt',
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.mvt',
2017-04-25 16:43:07 +08:00
mapConfig: createMapConfig({ mvt: 0 }),
assert: function (tile, callback) {
var tileJSON = tile.toJSON();
var features = tileJSON[0].features;
assert.strictEqual(features.length, 1);
2017-04-26 02:40:17 +08:00
callback();
}
},
{
desc: 'should get mvt tile using buffer-size 128',
coords: { z: 7, x: 64, y: 48 },
format: 'mvt',
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.mvt',
mapConfig: createMapConfig({ mvt: 128 }),
assert: function (tile, callback) {
var tileJSON = tile.toJSON();
2017-04-25 23:54:31 +08:00
var features = tileJSON[0].features;
assert.strictEqual(features.length, 9);
2017-04-26 02:40:17 +08:00
callback();
}
},
{
desc: 'should get grid.json tile using buffer-size 0 overriden by template params',
coords: { z: 7, x: 64, y: 48 },
format: 'grid.json',
layers: [0],
fixturePath: './test/fixtures/buffer-size/tile-grid.json.7.64.48-buffer-size-0.grid.json',
mapConfig: createMapConfig({ 'grid.json': 0 }),
assert: function (tile, callback) {
2019-10-22 01:07:24 +08:00
assert.utfgridEqualsFile(tile, this.fixturePath, 2, callback);
}
},
{
desc: 'should get grid.json tile using buffer-size 128 overriden by template params',
coords: { z: 7, x: 64, y: 48 },
format: 'grid.json',
layers: [0],
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.grid.json',
mapConfig: createMapConfig({ 'grid.json': 128 }),
assert: function (tile, callback) {
assert.utfgridEqualsFile(tile, this.fixturePath, 2, callback);
}
}
];
2019-10-22 01:07:24 +08:00
afterEach(function (done) {
2018-10-17 16:06:48 +08:00
if (testClient) {
return testClient.drain(done);
2017-10-06 23:53:47 +08:00
}
return done();
});
const originalUsePostGIS = serverOptions.renderer.mvt.usePostGIS;
2018-10-17 16:06:48 +08:00
after(function () {
serverOptions.renderer.mvt.usePostGIS = originalUsePostGIS;
});
2018-10-17 16:08:55 +08:00
var testFn = (test) => {
it(test.desc, function (done) {
testClient = new TestClient(test.mapConfig, 1234);
var coords = test.coords;
var options = {
format: test.format,
layers: test.layers
};
testClient.getTile(coords.z, coords.x, coords.y, options, function (err, res, tile) {
assert.ifError(err);
// To generate images use:
// tile.save(test.fixturePath);
test.assert(tile, done);
});
2018-10-17 16:08:55 +08:00
});
};
2018-10-17 16:25:18 +08:00
testCases.filter(test => test.format !== 'mvt').forEach(function (test) {
testFn(test);
});
2019-10-22 01:07:24 +08:00
describe('using mapnik mvt renderer', function () {
2018-10-17 16:25:18 +08:00
before(function () {
serverOptions.renderer.mvt.usePostGIS = false;
});
testCases.filter(test => test.format === 'mvt').forEach(function (test) {
2018-10-17 16:08:55 +08:00
testFn(test);
2018-10-17 16:25:18 +08:00
});
});
2019-10-22 01:07:24 +08:00
describe('using postgis mvt renderer', function () {
2018-10-17 16:25:18 +08:00
before(function () {
serverOptions.renderer.mvt.usePostGIS = true;
});
testCases.filter(test => test.format === 'mvt').forEach(function (test) {
testFn(test);
});
});
});
2017-04-25 23:54:31 +08:00
function createBufferSizeTemplate (name, buffersize, placeholders, cartocss) {
cartocss = cartocss || CARTOCSS_LABELS;
2017-04-25 23:54:31 +08:00
return {
2019-10-22 01:07:24 +08:00
version: '0.0.1',
name: name,
placeholders: placeholders || {
buffersize: {
type: 'number',
default: 0
}
},
2019-10-22 01:07:24 +08:00
layergroup: createMapConfig(buffersize)
2017-04-26 01:41:30 +08:00
};
}
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',
2019-10-22 01:07:24 +08:00
template: createBufferSizeTemplate('named-default-buffer-size', { png: '<%= buffersize %>' }),
assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
}
},
{
desc: 'should get png tile using buffer-size 128 (placehoder value)',
coords: { z: 7, x: 64, y: 48 },
format: 'png',
placeholders: { buffersize: 128 },
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png',
2019-10-22 01:07:24 +08:00
template: createBufferSizeTemplate('named-custom-buffer-size', { png: '<%= buffersize %>' }),
assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
}
2017-04-25 23:54:31 +08:00
},
{
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 },
2017-04-25 23:54:31 +08:00
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.png',
template: createBufferSizeTemplate('named-default-buffer-size-by-format', {
png: '<%= buffersize_png %>'
2017-04-25 23:54:31 +08:00
}, {
2019-10-22 01:07:24 +08:00
buffersize_png: {
type: 'number',
default: '0'
2017-04-25 23:54:31 +08:00
}
}),
assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
}
},
{
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_png: 128 },
2017-04-25 23:54:31 +08:00
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png',
template: createBufferSizeTemplate('named-custom-buffer-size-by-format', {
png: '<%= buffersize_png %>'
2017-04-25 23:54:31 +08:00
}, {
2019-10-22 01:07:24 +08:00
buffersize_png: {
type: 'number',
default: '0'
2017-04-25 23:54:31 +08:00
}
}),
assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
}
},
{
desc: 'should get grid.json tile using buffer-size 0 overriden by template params',
coords: { z: 7, x: 64, y: 48 },
format: 'grid.json',
layers: [0],
placeholders: { buffersize_gridjson: 0 },
fixturePath: './test/fixtures/buffer-size/tile-grid.json.7.64.48-buffer-size-0.grid.json',
template: createBufferSizeTemplate('named-default-buffer-size-by-format-gridjson', {
'grid.json': '<%= buffersize_gridjson %>'
}, {
2019-10-22 01:07:24 +08:00
buffersize_gridjson: {
type: 'number',
default: '0'
}
}),
assert: function (tile, callback) {
2019-10-22 01:07:24 +08:00
assert.utfgridEqualsFile(tile, this.fixturePath, 2, callback);
}
},
{
desc: 'should get grid.json tile using buffer-size 128 overriden by template params',
coords: { z: 7, x: 64, y: 48 },
format: 'grid.json',
layers: [0],
placeholders: { buffersize_gridjson: 128 },
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.grid.json',
template: createBufferSizeTemplate('named-custom-buffer-size-by-format-gridjson', {
'grid.json': '<%= buffersize_gridjson %>'
}, {
2019-10-22 01:07:24 +08:00
buffersize_gridjson: {
type: 'number',
default: '0'
}
}),
assert: function (tile, callback) {
assert.utfgridEqualsFile(tile, this.fixturePath, 2, callback);
}
}
];
2019-10-22 01:07:24 +08:00
afterEach(function (done) {
2017-10-07 00:17:26 +08:00
if (this.testClient) {
return this.testClient.drain(done);
}
return done();
});
testCases.forEach(function (test) {
it(test.desc, function (done) {
2017-10-07 00:17:26 +08:00
this.testClient = new TestClient(test.template, 1234);
var coords = test.coords;
var options = {
format: test.format,
placeholders: test.placeholders,
layers: test.layers
};
2017-10-07 00:17:26 +08:00
this.testClient.getTile(coords.z, coords.x, coords.y, options, function (err, res, tile) {
assert.ifError(err);
// To generate images use:
2019-10-22 01:07:24 +08:00
// tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-0-test.png');
2017-10-07 00:17:26 +08:00
test.assert(tile, done);
});
});
});
});
describe('buffer size per format for named maps w/o placeholders', function () {
2018-10-17 16:33:12 +08:00
let testClient;
var testCases = [
{
desc: 'should get png tile using buffer-size 0 overriden by template params',
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-no-buffer-size-png-0', {}, {}),
assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
}
},
{
desc: 'should get png tile using buffer-size 128 overriden by template params',
coords: { z: 7, x: 64, y: 48 },
format: 'png',
placeholders: {
buffersize: {
png: 128
}
},
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png',
template: createBufferSizeTemplate('named-no-buffer-size-png-128', {}, {}),
assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
}
},
{
desc: 'should get mvt tile using buffer-size 0 overriden by template params',
coords: { z: 7, x: 64, y: 48 },
format: 'mvt',
placeholders: {
buffersize: {
mvt: 0
}
},
fixturePath: './test/fixtures/buffer-size/tile-mvt-7.64.48-buffer-size-0.mvt',
template: createBufferSizeTemplate('named-no-buffer-size-mvt', {}, {}),
assert: function (tile, callback) {
var tileJSON = tile.toJSON();
var features = tileJSON[0].features;
var dataFixture = fs.readFileSync(this.fixturePath);
var vtile = new mapnik.VectorTile(this.coords.z, this.coords.x, this.coords.y);
vtile.setDataSync(dataFixture);
var vtileJSON = vtile.toJSON();
var vtileFeatures = vtileJSON[0].features;
assert.strictEqual(features.length, vtileFeatures.length);
callback();
}
},
{
desc: 'should get mvt tile using buffer-size 128 overriden by template params',
coords: { z: 7, x: 64, y: 48 },
format: 'mvt',
placeholders: {
buffersize: {
mvt: 128
}
},
fixturePath: './test/fixtures/buffer-size/tile-mvt-7.64.48-buffer-size-128.mvt',
template: createBufferSizeTemplate('named-no-buffer-size-mvt-128', {}, {}),
assert: function (tile, callback) {
var tileJSON = tile.toJSON();
var features = tileJSON[0].features;
var dataFixture = fs.readFileSync(this.fixturePath);
var vtile = new mapnik.VectorTile(this.coords.z, this.coords.x, this.coords.y);
vtile.setDataSync(dataFixture);
var vtileJSON = vtile.toJSON();
var vtileFeatures = vtileJSON[0].features;
assert.strictEqual(features.length, vtileFeatures.length);
callback();
}
},
{
desc: 'should get grid.json tile using buffer-size 0 overriden by template params',
coords: { z: 7, x: 64, y: 48 },
format: 'grid.json',
layers: [0],
placeholders: {
buffersize: {
'grid.json': 0
}
},
fixturePath: './test/fixtures/buffer-size/tile-grid.json.7.64.48-buffer-size-0.grid.json',
template: createBufferSizeTemplate('named-no-buffer-size-grid-json-0', {}, {}),
assert: function (tile, callback) {
2019-10-22 01:07:24 +08:00
assert.utfgridEqualsFile(tile, this.fixturePath, 2, callback);
}
},
{
desc: 'should get grid.json tile using buffer-size 128 overriden by template params',
coords: { z: 7, x: 64, y: 48 },
format: 'grid.json',
layers: [0],
placeholders: {
buffersize: {
'grid.json': 128
}
},
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.grid.json',
template: createBufferSizeTemplate('named-no-buffer-size-grid-json-128', {}, {}),
assert: function (tile, callback) {
assert.utfgridEqualsFile(tile, this.fixturePath, 2, callback);
}
},
{
desc: 'should get png tile using buffer-size 0' +
' overriden by template params with no buffersize in mapconfig',
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-no-buffer-size-mapconfig-png-0', undefined, {}),
assert: function (tile, callback) {
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
}
2019-10-22 01:07:24 +08:00
}
];
2019-10-22 01:07:24 +08:00
afterEach(function (done) {
2018-10-17 16:33:12 +08:00
if (testClient) {
return testClient.drain(done);
2017-10-07 00:17:26 +08:00
}
return done();
});
const originalUsePostGIS = serverOptions.renderer.mvt.usePostGIS;
2018-10-17 16:33:12 +08:00
after(function () {
serverOptions.renderer.mvt.usePostGIS = originalUsePostGIS;
});
var testFn = (test) => {
it(test.desc, function (done) {
test.template.name += '_1';
testClient = new TestClient(test.template, 1234);
var coords = test.coords;
var options = {
format: test.format,
placeholders: test.placeholders,
layers: test.layers
};
testClient.getTile(coords.z, coords.x, coords.y, options, function (err, res, tile) {
assert.ifError(err);
// To generate images use:
2019-10-22 01:07:24 +08:00
// tile.save(test.fixturePath);
2018-10-17 16:33:12 +08:00
// require('fs').writeFileSync(test.fixturePath, JSON.stringify(tile));
// require('fs').writeFileSync(test.fixturePath, tile.getDataSync());
test.assert(tile, done);
});
});
};
testCases.filter(test => test.format !== 'mvt').forEach(function (test) {
testFn(test);
});
2019-10-22 01:07:24 +08:00
describe('using mapnik mvt renderer', function () {
2018-10-17 16:33:12 +08:00
before(function () {
serverOptions.renderer.mvt.usePostGIS = false;
});
testCases.filter(test => test.format === 'mvt').forEach(function (test) {
testFn(test);
});
});
2019-10-22 01:07:24 +08:00
describe('using postgis mvt renderer', function () {
2018-10-17 16:33:12 +08:00
before(function () {
serverOptions.renderer.mvt.usePostGIS = true;
});
testCases.filter(test => test.format === 'mvt').forEach(function (test) {
testFn(test);
});
});
});