2017-04-25 00:56:15 +08:00
|
|
|
require('../support/test_helper');
|
|
|
|
|
|
|
|
var assert = require('../support/assert');
|
|
|
|
var TestClient = require('../support/test-client');
|
2017-04-26 01:27:31 +08:00
|
|
|
var IMAGE_TOLERANCE_PER_MIL = 5;
|
2017-04-25 00:56:15 +08:00
|
|
|
var mapnik = require('windshaft').mapnik;
|
|
|
|
|
2017-04-25 20:34:17 +08:00
|
|
|
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');
|
2017-04-25 20:34:17 +08:00
|
|
|
|
|
|
|
function createMapConfig (bufferSize, cartocss) {
|
|
|
|
cartocss = cartocss || CARTOCSS_LABELS;
|
|
|
|
|
2017-04-25 00:56:15 +08:00
|
|
|
return {
|
|
|
|
version: '1.6.0',
|
|
|
|
buffersize: bufferSize,
|
|
|
|
layers: [{
|
|
|
|
type: "cartodb",
|
|
|
|
options: {
|
|
|
|
sql: [
|
|
|
|
'select',
|
|
|
|
' *',
|
|
|
|
'from',
|
|
|
|
' populated_places_simple_reduced',
|
|
|
|
].join('\n'),
|
2017-04-25 20:34:17 +08:00
|
|
|
cartocss: cartocss,
|
2017-04-25 00:56:15 +08:00
|
|
|
cartocss_version: '2.3.0',
|
|
|
|
interactivity: 'cartodb_id'
|
2017-04-26 01:27:31 +08:00
|
|
|
}
|
2017-04-25 00:56:15 +08:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-25 00:57:20 +08:00
|
|
|
describe('buffer size per format', function () {
|
2017-04-25 00:56:15 +08:00
|
|
|
var testCases = [
|
|
|
|
{
|
2017-04-25 16:43:07 +08:00
|
|
|
desc: 'should get png tile using buffer-size 0',
|
2017-04-25 00:56:15 +08:00
|
|
|
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 }),
|
2017-04-25 00:56:15 +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',
|
|
|
|
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',
|
2017-04-25 00:56:15 +08:00
|
|
|
coords: { z: 7, x: 64, y: 48 },
|
|
|
|
format: 'mvt',
|
2017-04-25 16:43:07 +08:00
|
|
|
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.png',
|
|
|
|
mapConfig: createMapConfig({ mvt: 0 }),
|
2017-04-25 00:56:15 +08:00
|
|
|
assert: function (tile, callback) {
|
|
|
|
var tileJSON = tile.toJSON();
|
|
|
|
var features = tileJSON[0].features;
|
|
|
|
assert.equal(features.length, 1);
|
|
|
|
|
|
|
|
var map = new mapnik.Map(256, 256);
|
|
|
|
tile.render(map, new mapnik.Image(256, 256), function (err, image) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.imageIsSimilarToFile(image, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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.png',
|
|
|
|
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;
|
2017-04-25 00:56:15 +08:00
|
|
|
assert.equal(features.length, 9);
|
|
|
|
|
|
|
|
var map = new mapnik.Map(256, 256);
|
|
|
|
tile.render(map, new mapnik.Image(256, 256), function (err, image) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.imageIsSimilarToFile(image, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
testCases.forEach(function (test) {
|
|
|
|
it(test.desc, function (done) {
|
|
|
|
var testClient = new TestClient(test.mapConfig, 1234);
|
|
|
|
var coords = test.coords;
|
|
|
|
testClient.getTile(coords.z, coords.x, coords.y, { format: test.format }, function (err, res, tile) {
|
|
|
|
assert.ifError(err);
|
|
|
|
// To generate images use:
|
2017-04-25 21:48:23 +08:00
|
|
|
// tile.save(test.fixturePath);
|
2017-04-25 20:34:17 +08:00
|
|
|
test.assert(tile, function () {
|
|
|
|
testClient.drain(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-25 23:54:31 +08:00
|
|
|
function createBufferSizeTemplate (name, buffersize, placeholders, cartocss) {
|
2017-04-25 20:34:17 +08:00
|
|
|
cartocss = cartocss || CARTOCSS_LABELS;
|
2017-04-25 23:54:31 +08:00
|
|
|
|
2017-04-25 20:34:17 +08:00
|
|
|
return {
|
|
|
|
"version": "0.0.1",
|
|
|
|
"name": name,
|
2017-04-25 23:54:31 +08:00
|
|
|
"placeholders": placeholders || {
|
2017-04-25 20:34:17 +08:00
|
|
|
"buffersize": {
|
|
|
|
"type": "number",
|
2017-04-26 01:27:31 +08:00
|
|
|
"default": 0
|
2017-04-25 20:34:17 +08:00
|
|
|
}
|
|
|
|
},
|
2017-04-25 23:54:31 +08:00
|
|
|
"layergroup": createMapConfig(buffersize)
|
2017-04-26 01:41:30 +08:00
|
|
|
};
|
2017-04-25 20:34:17 +08:00
|
|
|
}
|
|
|
|
|
2017-04-26 01:27:31 +08:00
|
|
|
describe('buffer size per format for named maps', function () {
|
2017-04-25 20:34:17 +08:00
|
|
|
var testCases = [
|
|
|
|
{
|
2017-04-25 21:48:23 +08:00
|
|
|
desc: 'should get png tile using buffer-size 0 (default value in template)',
|
2017-04-25 20:34:17 +08:00
|
|
|
coords: { z: 7, x: 64, y: 48 },
|
|
|
|
format: 'png',
|
|
|
|
fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.png',
|
2017-04-26 01:27:31 +08:00
|
|
|
template: createBufferSizeTemplate('named-default-buffer-size', '<%= buffersize %>'),
|
2017-04-25 21:48:23 +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)',
|
|
|
|
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',
|
2017-04-26 01:27:31 +08:00
|
|
|
template: createBufferSizeTemplate('named-custom-buffer-size', '<%= buffersize %>'),
|
2017-04-25 20:34:17 +08:00
|
|
|
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',
|
2017-04-26 01:27:31 +08:00
|
|
|
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',
|
2017-04-26 01:27:31 +08:00
|
|
|
template: createBufferSizeTemplate('named-default-buffer-size-by-format', {
|
|
|
|
png: '<%= buffersize_png %>'
|
2017-04-25 23:54:31 +08:00
|
|
|
}, {
|
|
|
|
"buffersize_png": {
|
|
|
|
"type": "number",
|
|
|
|
"default": "0"
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
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',
|
2017-04-26 01:27:31 +08:00
|
|
|
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',
|
2017-04-26 01:27:31 +08:00
|
|
|
template: createBufferSizeTemplate('named-custom-buffer-size-by-format', {
|
|
|
|
png: '<%= buffersize_png %>'
|
2017-04-25 23:54:31 +08:00
|
|
|
}, {
|
|
|
|
"buffersize_png": {
|
|
|
|
"type": "number",
|
|
|
|
"default": "0"
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
assert: function (tile, callback) {
|
|
|
|
assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback);
|
|
|
|
}
|
2017-04-25 20:34:17 +08:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
testCases.forEach(function (test) {
|
|
|
|
it(test.desc, function (done) {
|
|
|
|
var testClient = new TestClient(undefined, 1234, test.template);
|
|
|
|
var coords = test.coords;
|
2017-04-26 01:27:31 +08:00
|
|
|
var options = {
|
|
|
|
format: test.format,
|
|
|
|
placeholders: test.placeholders
|
|
|
|
};
|
2017-04-25 21:48:23 +08:00
|
|
|
testClient.getNamedTile(coords.z, coords.x, coords.y, options, function (err, res, tile) {
|
2017-04-25 20:34:17 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
// To generate images use:
|
2017-04-26 01:27:31 +08:00
|
|
|
//tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-0-test.png');
|
|
|
|
test.assert(tile, function (err) {
|
|
|
|
assert.ifError(err);
|
2017-04-25 00:56:15 +08:00
|
|
|
testClient.drain(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-04-25 20:34:17 +08:00
|
|
|
});
|