From a88f6223cc40b3543c5cd29975f7d4a1f049dd94 Mon Sep 17 00:00:00 2001 From: jingsam Date: Tue, 19 Jul 2016 20:04:24 +0800 Subject: [PATCH 01/10] fix size enlarged twice --- index.js | 4 ++-- test/test.js | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 39a1993..4b22958 100644 --- a/index.js +++ b/index.js @@ -47,8 +47,8 @@ abaculus.coordsFromBbox = function(z, s, bbox, limit, tileSize) { var origin = [topRight[0] - center.w / 2, topRight[1] + center.h / 2]; center.x = origin[0]; center.y = origin[1]; - center.w = Math.round(center.w * s); - center.h = Math.round(center.h * s); + center.w = Math.round(center.w); + center.h = Math.round(center.h); if (center.w >= limit || center.h >= limit) throw new Error('Desired image is too large.'); return center; diff --git a/test/test.js b/test/test.js index 46a36dd..1c9515d 100644 --- a/test/test.js +++ b/test/test.js @@ -11,7 +11,8 @@ var zoom = 5, y = 4096, quality = 256, format = 'png', - limit = 19008; + limit = 19008, + tileSize = 256; // fixtures @@ -26,7 +27,7 @@ describe('Get center from bbox', function() { var bbox = [0, 0, 0, 0]; assert.throws( function() { - printer.coordsFromBbox(zoom, scale, bbox, limit); + printer.coordsFromBbox(zoom, scale, bbox, limit, tileSize); }, /Incorrect coordinates/); done(); }); @@ -34,18 +35,18 @@ describe('Get center from bbox', function() { var bbox = [-60, -60, 60, 60]; assert.throws( function() { - printer.coordsFromBbox(7, 2, bbox, limit); + printer.coordsFromBbox(7, 2, bbox, limit, tileSize); }, /Desired image is too large./); done(); }); it('should return the correct coordinates', function(done) { var bbox = [-60, -60, 60, 60]; - var center = printer.coordsFromBbox(zoom, scale, bbox, limit); - assert.deepEqual(center.w, 10920); + var center = printer.coordsFromBbox(zoom, scale, bbox, limit, tileSize);console.log(center) + assert.deepEqual(center.w, 10922); assert.deepEqual(center.h, 13736); - assert.deepEqual(center.x, x); - assert.deepEqual(center.y, y); + assert.deepEqual(center.x, 16384); + assert.deepEqual(center.y, 16384); done(); }); }); @@ -59,7 +60,7 @@ describe('get coordinates from center', function() { h: 4752 }; assert.throws( function() { - printer.coordsFromCenter(zoom, scale, center, limit); + printer.coordsFromCenter(zoom, scale, center, limit, tileSize); }, /Desired image is too large./); done(); }); @@ -70,9 +71,9 @@ describe('get coordinates from center', function() { w: 800, h: 800 }; - center = printer.coordsFromCenter(zoom, scale, center, limit); - assert.equal(center.x, x); - assert.equal(center.y, 3631); + var center2 = printer.coordsFromCenter(zoom, scale, center, limit, tileSize); + assert.equal(center2.x, 16384); + assert.equal(center2.y, 14525); done(); }); }); From 7de31e956513169f3f4d66adc9740f7d478809e8 Mon Sep 17 00:00:00 2001 From: jingsam Date: Tue, 19 Jul 2016 20:25:23 +0800 Subject: [PATCH 02/10] fix size enlarged twice --- test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 1c9515d..4c7ae2d 100644 --- a/test/test.js +++ b/test/test.js @@ -42,7 +42,7 @@ describe('Get center from bbox', function() { it('should return the correct coordinates', function(done) { var bbox = [-60, -60, 60, 60]; - var center = printer.coordsFromBbox(zoom, scale, bbox, limit, tileSize);console.log(center) + var center = printer.coordsFromBbox(zoom, scale, bbox, limit, tileSize); assert.deepEqual(center.w, 10922); assert.deepEqual(center.h, 13736); assert.deepEqual(center.x, 16384); From 303dd3c1de89b0e9fdcef5d7f2440905de0a66b4 Mon Sep 17 00:00:00 2001 From: jingsam Date: Tue, 19 Jul 2016 21:09:42 +0800 Subject: [PATCH 03/10] Set tileSize default to 256 --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4b22958..ea1f337 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,7 @@ function abaculus(arg, callback) { } abaculus.coordsFromBbox = function(z, s, bbox, limit, tileSize) { - var sm = new SphericalMercator({ size: tileSize * s }); + var sm = new SphericalMercator({ size: (tileSize || 256) * s }); var topRight = sm.px([bbox[2], bbox[3]], z), bottomLeft = sm.px([bbox[0], bbox[1]], z); var center = {}; @@ -55,7 +55,7 @@ abaculus.coordsFromBbox = function(z, s, bbox, limit, tileSize) { }; abaculus.coordsFromCenter = function(z, s, center, limit, tileSize) { - var sm = new SphericalMercator({ size: tileSize * s }); + var sm = new SphericalMercator({ size: (tileSize || 256) * s }); var origin = sm.px([center.x, center.y], z); center.x = origin[0]; center.y = origin[1]; From b11d1f210b0ea8bc4acb6522e2710f850b90f670 Mon Sep 17 00:00:00 2001 From: jingsam Date: Wed, 20 Jul 2016 14:57:46 +0800 Subject: [PATCH 04/10] correct centerCoordinate --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ea1f337..af1537e 100644 --- a/index.js +++ b/index.js @@ -78,8 +78,8 @@ abaculus.tileList = function(z, s, center, tileSize) { var ts = Math.round(size * s); var centerCoordinate = { - column: x / size, - row: y / size, + column: x / ts, + row: y / ts, zoom: z }; From 67dd5b70ba8a50ea3c13a71ac9a8e51283adee5e Mon Sep 17 00:00:00 2001 From: jingsam Date: Wed, 20 Jul 2016 14:59:23 +0800 Subject: [PATCH 05/10] Correct the bottom-right coords of image --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index af1537e..133a156 100644 --- a/index.js +++ b/index.js @@ -112,7 +112,7 @@ abaculus.tileList = function(z, s, center, tileSize) { } var tl = floorObj(pointCoordinate({x: 0, y:0})); - var br = floorObj(pointCoordinate(dimensions)); + var br = floorObj(pointCoordinate({ x: dimensions.x - 1, y: dimensions.y - 1 })); var coords = {}; coords.tiles = []; var tileCount = (br.column - tl.column + 1) * (br.row - tl.row + 1); From 8a3424acea6c792f6aed65ac6ca5505646b7efba Mon Sep 17 00:00:00 2001 From: jingsam Date: Wed, 20 Jul 2016 15:00:57 +0800 Subject: [PATCH 06/10] Row also need to be wrapped --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 133a156..2f863ac 100644 --- a/index.js +++ b/index.js @@ -131,7 +131,10 @@ abaculus.tileList = function(z, s, center, tileSize) { Math.pow(2,c.zoom) + c.column : c.column % Math.pow(2,c.zoom); - if (c.row < 0) continue; + c.row = c.row < 0 ? + Math.pow(2, c.zoom) + c.row : + c.row % Math.pow(2, c.zoom); + coords.tiles.push({ z: c.zoom, x: c.column, From 2f3b6f88d93456f614b103ada7a6da437d9b0fa0 Mon Sep 17 00:00:00 2001 From: jingsam Date: Wed, 20 Jul 2016 15:17:17 +0800 Subject: [PATCH 07/10] fix test --- test/test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test.js b/test/test.js index 4c7ae2d..ce9e4e0 100644 --- a/test/test.js +++ b/test/test.js @@ -83,13 +83,13 @@ describe('create list of tile coordinates', function() { var expectedCoords = { tiles: [ - { z: 5, x: 15, y: 15, px: -112, py: -108 }, - { z: 5, x: 15, y: 16, px: -112, py: 916 }, - { z: 5, x: 16, y: 15, px: 912, py: -108 }, - { z: 5, x: 16, y: 16, px: 912, py: 916 } + { z: 5, x: 3, y: 3, px: -112, py: -108 }, + { z: 5, x: 3, y: 4, px: -112, py: 916 }, + { z: 5, x: 4, y: 3, px: 912, py: -108 }, + { z: 5, x: 4, y: 4, px: 912, py: 916 } ], dimensions: { x: 1824, y: 1832 }, - center: { row: 16, column: 16, zoom: 5 }, + center: { row: 4, column: 4, zoom: 5 }, scale: 4 }; it('should return a tiles object with correct coords', function(done) { From 068a98babdfaa99474ed733054e68c0c7355b16a Mon Sep 17 00:00:00 2001 From: jingsam Date: Wed, 20 Jul 2016 16:27:36 +0800 Subject: [PATCH 08/10] set zoom default to zero --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 2f863ac..000508a 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ var crypto = require('crypto'); module.exports = abaculus; function abaculus(arg, callback) { - var z = arg.zoom || 1, + var z = arg.zoom || 0, s = arg.scale || 1, center = arg.center || null, bbox = arg.bbox || null, From add4330d114be4edec8bf41426994e796a433929 Mon Sep 17 00:00:00 2001 From: jingsam Date: Wed, 20 Jul 2016 16:46:44 +0800 Subject: [PATCH 09/10] Do not throw errors --- index.js | 16 ++++++++-------- test/test.js | 23 ++++++++++++----------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 000508a..34fdf1b 100644 --- a/index.js +++ b/index.js @@ -20,13 +20,17 @@ function abaculus(arg, callback) { if (center) { // get center coordinates in px from lng,lat - center = abaculus.coordsFromCenter(z, s, center, limit, tileSize); + center = abaculus.coordsFromCenter(z, s, center, tileSize); } else if (bbox) { // get center coordinates in px from [w,s,e,n] bbox - center = abaculus.coordsFromBbox(z, s, bbox, limit, tileSize); + center = abaculus.coordsFromBbox(z, s, bbox, tileSize); } else { return callback(new Error('No coordinates provided.')); } + + if (center.w <= 0 || center.h <= 0) return callback(new Error('Incorrect coordinates')); + if (center.w >= limit || center.h >= limit) return callback(new Error('Desired image is too large.')); + // generate list of tile coordinates center var coords = abaculus.tileList(z, s, center, tileSize); @@ -34,7 +38,7 @@ function abaculus(arg, callback) { abaculus.stitchTiles(coords, format, quality, getTile, callback); } -abaculus.coordsFromBbox = function(z, s, bbox, limit, tileSize) { +abaculus.coordsFromBbox = function(z, s, bbox, tileSize) { var sm = new SphericalMercator({ size: (tileSize || 256) * s }); var topRight = sm.px([bbox[2], bbox[3]], z), bottomLeft = sm.px([bbox[0], bbox[1]], z); @@ -42,19 +46,16 @@ abaculus.coordsFromBbox = function(z, s, bbox, limit, tileSize) { center.w = topRight[0] - bottomLeft[0]; center.h = bottomLeft[1] - topRight[1]; - if (center.w <= 0 || center.h <= 0) throw new Error('Incorrect coordinates'); - var origin = [topRight[0] - center.w / 2, topRight[1] + center.h / 2]; center.x = origin[0]; center.y = origin[1]; center.w = Math.round(center.w); center.h = Math.round(center.h); - if (center.w >= limit || center.h >= limit) throw new Error('Desired image is too large.'); return center; }; -abaculus.coordsFromCenter = function(z, s, center, limit, tileSize) { +abaculus.coordsFromCenter = function(z, s, center, tileSize) { var sm = new SphericalMercator({ size: (tileSize || 256) * s }); var origin = sm.px([center.x, center.y], z); center.x = origin[0]; @@ -62,7 +63,6 @@ abaculus.coordsFromCenter = function(z, s, center, limit, tileSize) { center.w = Math.round(center.w * s); center.h = Math.round(center.h * s); - if (center.w >= limit || center.h >= limit) throw new Error('Desired image is too large.'); return center; }; diff --git a/test/test.js b/test/test.js index ce9e4e0..e745e39 100644 --- a/test/test.js +++ b/test/test.js @@ -26,23 +26,23 @@ describe('Get center from bbox', function() { it('should fail if (x1, y1) and (x2,y2) are equal', function(done) { var bbox = [0, 0, 0, 0]; - assert.throws( function() { - printer.coordsFromBbox(zoom, scale, bbox, limit, tileSize); - }, /Incorrect coordinates/); + var center = printer.coordsFromBbox(zoom, scale, bbox, tileSize); + assert(center.w <= 0 || center.h <= 0, 'Incorrect coordinates') + done(); }); it('should fail if the image is too large', function(done) { var bbox = [-60, -60, 60, 60]; - assert.throws( function() { - printer.coordsFromBbox(7, 2, bbox, limit, tileSize); - }, /Desired image is too large./); + var center = printer.coordsFromBbox(7, 2, bbox, tileSize); + assert(center.w >= limit || center.h >= limit, 'Desired image is too large') + done(); }); it('should return the correct coordinates', function(done) { var bbox = [-60, -60, 60, 60]; - var center = printer.coordsFromBbox(zoom, scale, bbox, limit, tileSize); + var center = printer.coordsFromBbox(zoom, scale, bbox, tileSize); assert.deepEqual(center.w, 10922); assert.deepEqual(center.h, 13736); assert.deepEqual(center.x, 16384); @@ -59,9 +59,10 @@ describe('get coordinates from center', function() { w: 4752, h: 4752 }; - assert.throws( function() { - printer.coordsFromCenter(zoom, scale, center, limit, tileSize); - }, /Desired image is too large./); + + var center = printer.coordsFromCenter(zoom, scale, center, tileSize); + assert(center.w >= limit || center.h >= limit, 'Desired image is too large') + done(); }); it('should return correct origin coords', function(done) { @@ -71,7 +72,7 @@ describe('get coordinates from center', function() { w: 800, h: 800 }; - var center2 = printer.coordsFromCenter(zoom, scale, center, limit, tileSize); + var center2 = printer.coordsFromCenter(zoom, scale, center, tileSize); assert.equal(center2.x, 16384); assert.equal(center2.y, 14525); done(); From 4b300f6e8391d3ac2a75fe5df7db9afdbf888484 Mon Sep 17 00:00:00 2001 From: jingsam Date: Wed, 20 Jul 2016 17:48:48 +0800 Subject: [PATCH 10/10] typo --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 34fdf1b..7f5a39f 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ function abaculus(arg, callback) { limit = arg.limit || 19008, tileSize = arg.tileSize || 256; - if (!getTile) return callback(new Error('Invalid function for getting tiles')); + if (!getTile) return callback(new Error('Invalid function for getting tiles.')); if (center) { // get center coordinates in px from lng,lat @@ -28,7 +28,7 @@ function abaculus(arg, callback) { return callback(new Error('No coordinates provided.')); } - if (center.w <= 0 || center.h <= 0) return callback(new Error('Incorrect coordinates')); + if (center.w <= 0 || center.h <= 0) return callback(new Error('Incorrect coordinates.')); if (center.w >= limit || center.h >= limit) return callback(new Error('Desired image is too large.')); // generate list of tile coordinates center