From 76868ad3f4099dd5eb34e4ba22778b6b653c2d3d Mon Sep 17 00:00:00 2001 From: javimolla Date: Mon, 14 Sep 2015 16:13:46 +0200 Subject: [PATCH] Extract logic from getScaleZoom to CRS Added tests for custom crs with zooms not power of two related --- spec/index.html | 3 +++ spec/suites/geo/CRSSpec.js | 40 ++++++++++++++++++++++++++++++++++++++ spec/suites/map/MapSpec.js | 10 +++++++++- src/geo/crs/CRS.js | 4 ++++ src/map/Map.js | 4 ++-- 5 files changed, 58 insertions(+), 3 deletions(-) diff --git a/spec/index.html b/spec/index.html index 5c94956e..7e2387f6 100644 --- a/spec/index.html +++ b/spec/index.html @@ -84,6 +84,9 @@ + + + diff --git a/spec/suites/geo/CRSSpec.js b/spec/suites/geo/CRSSpec.js index a75efb92..32b18289 100644 --- a/spec/suites/geo/CRSSpec.js +++ b/spec/suites/geo/CRSSpec.js @@ -157,3 +157,43 @@ describe("CRS.Simple", function () { }); }); }); + +describe("CRS", function () { + var crs = L.CRS; + + describe("#zoom && #scale", function () { + it("convert zoom to scale and viceversa and return the same values", function () { + var zoom = 2.5; + var scale = crs.scale(zoom); + expect(crs.zoom(scale)).to.eql(zoom); + }); + }); +}); + +describe("CRS.ZoomNotPowerOfTwo", function () { + var crs = L.extend({}, L.CRS, { + scale: function (zoom) { + return 256 * Math.pow(1.5, zoom); + }, + zoom: function (scale) { + return Math.log(scale / 256) / Math.log(1.5); + } + }); + + describe("#scale", function () { + it("of zoom levels are related by a power of 1.5", function () { + var zoom = 5; + var scale = crs.scale(zoom); + expect(crs.scale(zoom + 1)).to.eql(1.5 * scale); + expect(crs.zoom(1.5 * scale)).to.eql(zoom + 1); + }); + }); + + describe("#zoom && #scale", function () { + it("convert zoom to scale and viceversa and return the same values", function () { + var zoom = 2; + var scale = crs.scale(zoom); + expect(crs.zoom(scale)).to.eql(zoom); + }); + }); +}); \ No newline at end of file diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index 43f692c7..d0302ce8 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -746,5 +746,13 @@ describe("Map", function () { }); }); - + + describe('#getScaleZoom && #getZoomScale', function () { + it("convert zoom to scale and viceversa and return the same values", function () { + var toZoom = 6.25; + var fromZoom = 8.5; + var scale = map.getScaleZoom(toZoom, fromZoom); + expect(Math.round(map.getZoomScale(scale, fromZoom) * 100) / 100).to.eql(toZoom); + }); + }); }); diff --git a/src/geo/crs/CRS.js b/src/geo/crs/CRS.js index e7288df4..4a00f049 100644 --- a/src/geo/crs/CRS.js +++ b/src/geo/crs/CRS.js @@ -33,6 +33,10 @@ L.CRS = { scale: function (zoom) { return 256 * Math.pow(2, zoom); }, + + zoom: function (scale) { + return Math.log(scale / 256) / Math.LN2; + }, // returns the bounds of the world in projected coords if applicable getProjectedBounds: function (zoom) { diff --git a/src/map/Map.js b/src/map/Map.js index 3632613c..ade0ced5 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -400,11 +400,11 @@ L.Map = L.Evented.extend({ }, getScaleZoom: function (scale, fromZoom) { + var crs = this.options.crs; fromZoom = fromZoom === undefined ? this._zoom : fromZoom; - return fromZoom + (Math.log(scale) / Math.LN2); + return crs.zoom(scale * crs.scale(fromZoom)); }, - // conversion methods project: function (latlng, zoom) { // (LatLng[, Number]) -> Point