From 5bdabbd921b679edebaf68beef30b0d030557d24 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 2 Jun 2016 01:42:45 +0200 Subject: [PATCH] Add support for different tile sizes --- lib/torque/renderer/point.js | 2 +- test/acceptance/renderer/point.js | 64 +++++++++++++++++++------------ test/support/image.js | 7 +++- test/support/point_renderer.js | 13 ++++--- 4 files changed, 55 insertions(+), 31 deletions(-) diff --git a/lib/torque/renderer/point.js b/lib/torque/renderer/point.js index 2a5fad1b..f4e784c8 100644 --- a/lib/torque/renderer/point.js +++ b/lib/torque/renderer/point.js @@ -70,7 +70,7 @@ var Filters = require('./torque_filters'); this._iconsToLoad = 0; this._filters = new Filters(this._canvas, {canvasClass: options.canvasClass}); this.setCartoCSS(this.options.cartocss || DEFAULT_CARTOCSS); - this.TILE_SIZE = 256; + this.TILE_SIZE = options.tileSize || 256; this._style = null; this._gradients = {}; diff --git a/test/acceptance/renderer/point.js b/test/acceptance/renderer/point.js index ece82877..6eee3c7a 100644 --- a/test/acceptance/renderer/point.js +++ b/test/acceptance/renderer/point.js @@ -10,31 +10,31 @@ var IMAGE_DIFF_TOLERANCE = 4 / 100; // Once you have a valid canvas and no errors, it's possible to write to disk the canvas buffer as a png image with: // require('fs').writeFileSync('/tmp/torque-acceptance-test-tile.png', canvas.toBuffer(), {encoding: null}); -asyncTest('navy example', function(assert) { - var cartocss = [ - 'Map {', - ' -torque-time-attribute: "date";', - ' -torque-aggregation-function: "count(cartodb_id)";', - ' -torque-frame-count: 760;', - ' -torque-animation-duration: 15;', - ' -torque-resolution: 2', - '}', - '#layer {', - ' marker-width: 3;', - ' marker-fill-opacity: 0.8;', - ' marker-fill: #FEE391; ', - ' comp-op: "lighten";', - ' [value > 2] { marker-fill: #FEC44F; }', - ' [value > 3] { marker-fill: #FE9929; }', - ' [value > 4] { marker-fill: #EC7014; }', - ' [value > 5] { marker-fill: #CC4C02; }', - ' [value > 6] { marker-fill: #993404; }', - ' [value > 7] { marker-fill: #662506; }', - ' [frame-offset = 1] { marker-width: 10; marker-fill-opacity: 0.05;}', - ' [frame-offset = 2] { marker-width: 15; marker-fill-opacity: 0.02;}', - '}' - ].join('\n'); +var cartocss = [ + 'Map {', + ' -torque-time-attribute: "date";', + ' -torque-aggregation-function: "count(cartodb_id)";', + ' -torque-frame-count: 760;', + ' -torque-animation-duration: 15;', + ' -torque-resolution: 2', + '}', + '#layer {', + ' marker-width: 3;', + ' marker-fill-opacity: 0.8;', + ' marker-fill: #FEE391; ', + ' comp-op: "lighten";', + ' [value > 2] { marker-fill: #FEC44F; }', + ' [value > 3] { marker-fill: #FE9929; }', + ' [value > 4] { marker-fill: #EC7014; }', + ' [value > 5] { marker-fill: #CC4C02; }', + ' [value > 6] { marker-fill: #993404; }', + ' [value > 7] { marker-fill: #662506; }', + ' [frame-offset = 1] { marker-width: 10; marker-fill-opacity: 0.05;}', + ' [frame-offset = 2] { marker-width: 15; marker-fill-opacity: 0.02;}', + '}' +].join('\n'); +asyncTest('navy example', function(assert) { var step = 300; pointRenderer.getTile('default_navy_3-3-2.torque.json', cartocss, 3, 3, 2, step, function(err, canvas) { @@ -45,6 +45,22 @@ asyncTest('navy example', function(assert) { }); }); +asyncTest('tileSize = 512', function(assert) { + var step = 300; + var tileSize = 512; + var options = { + tileSize: tileSize + }; + + pointRenderer.getTile('default_navy_3-3-2.torque.json', cartocss, 3, 3, 2, step, options, function(err, canvas) { + assert.ok(!err, 'no error while getting tile'); + var img = image.getImage(canvas.toBuffer()); + assert.equal(img.width(), tileSize); + assert.equal(img.height(), tileSize); + QUnit.start(); + }); +}); + asyncTest('basic heatmap', function(assert) { var cartocss = [ 'Map {', diff --git a/test/support/image.js b/test/support/image.js index 062508d4..e9ead3ac 100644 --- a/test/support/image.js +++ b/test/support/image.js @@ -1,9 +1,13 @@ var mapnik = require('mapnik'); +function getImage(buffer) { + return new mapnik.Image.fromBytesSync(buffer); +} + function compare(buffer, fixtureRelPath) { save(__dirname + '/../results/' + fixtureRelPath, buffer); - var img = new mapnik.Image.fromBytesSync(buffer); + var img = getImage(buffer); var reference = new mapnik.Image.openSync(__dirname + '/../fixtures/image/' + fixtureRelPath); return img.compare(reference) / (reference.width() * reference.height()); } @@ -14,6 +18,7 @@ function save(path, buffer) { } module.exports = { + getImage: getImage, compare: compare, save: save }; diff --git a/test/support/point_renderer.js b/test/support/point_renderer.js index d5af2a5e..7a6df0f6 100644 --- a/test/support/point_renderer.js +++ b/test/support/point_renderer.js @@ -6,13 +6,15 @@ var fs = require('fs'); var torque = require('../../lib/torque/index'); -function getTile(jsonRelPath, cartocss, z, x, y, step, callback) { - step = step || 0; - +function getTile(jsonRelPath, cartocss, z, x, y, step, options, callback) { + if (!callback) { + callback = options; + options = {}; + } var cartoCssOptions = torque.common.TorqueLayer.optionsFromCartoCSS(cartocss); var provider = new torque.providers.windshaft(_.extend({ no_fetch_map: true }, cartoCssOptions)); - var rendererOptions = _.extend({cartocss: cartocss}, cartoCssOptions, { + var rendererOptions = _.extend(options, {cartocss: cartocss}, cartoCssOptions, { canvasClass: Canvas, imageClass: Canvas.Image, setImageSrc: function(img, url, callback) { @@ -42,7 +44,8 @@ function getTile(jsonRelPath, cartocss, z, x, y, step, callback) { var rows = JSON.parse(fs.readFileSync(__dirname + '/../fixtures/json/' + jsonRelPath)); - var canvas = new Canvas(256, 256); + var tileSize = options.tileSize || 256; + var canvas = new Canvas(tileSize, tileSize); var pointRenderer = new torque.renderer.Point(canvas, rendererOptions); pointRenderer.renderTile(provider.proccessTile(rows, {x: x, y: y}, z), step, function(err) {