From 51f9571f838e08f18e3e890fe9a8bc84bd3d6163 Mon Sep 17 00:00:00 2001 From: semone Date: Sun, 4 Oct 2015 21:05:16 +0200 Subject: [PATCH] added new zoom method as inverse of existing scale method --- src/proj4leaflet.js | 27 +++++++++++++++++++++++++++ test/specs.js | 18 +++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/proj4leaflet.js b/src/proj4leaflet.js index e19baeb..7172906 100644 --- a/src/proj4leaflet.js +++ b/src/proj4leaflet.js @@ -124,6 +124,33 @@ return baseScale + scaleDiff * zDiff; } }, + + zoom: function(scale) { + // Find closest number in this._scales, down + var downScale = this._closestElement(this._scales, scale), + downZoom = this._scales.indexOf(downScale), + nextZoom, + scaleDiff; + // Check if scale is downScale => return array index + if (scale === downScale) { + return downZoom; + } + // Interpolate + nextZoom = downZoom + 1; + scaleDiff = this._scales[nextZoom] - downScale; + return (scale - downScale) / scaleDiff + downZoom; + }, + + /* Get the closest lowest element in an array */ + _closestElement: function(array, element) { + var low; + for (var i = array.length; i--;) { + if (array[i] <= element && (low === undefined || low < array[i])) { + low = array[i]; + } + } + return low; + } }); L.Proj.GeoJSON = L.GeoJSON.extend({ diff --git a/test/specs.js b/test/specs.js index f154699..1bd6cab 100644 --- a/test/specs.js +++ b/test/specs.js @@ -27,7 +27,7 @@ describe('L.Proj.CRS', function() { '+towgs84=414.1,41.3,603.1,-0.855,2.141,-7.023,0 +no_defs'); var pp = crs.project(new L.LatLng(55.723337, 14.194313)); - expect(pp.x).toBeCloseTo(1398776, 0) + expect(pp.x).toBeCloseTo(1398776, 0); expect(pp.y).toBeCloseTo(6178304, 0); }); @@ -135,4 +135,20 @@ describe('L.Proj.CRS', function() { worldSize *= 2; } }); + it('convert zoom to scale and viceversa and return the same values', function () { + var crs = new L.Proj.CRS('EPSG:3006', + '+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs', + { + resolutions: [ + 8192, 4096, 2048, 1024, 512, 256, 128, + 64, 32, 16, 8, 4, 2, 1, 0.5 + ] + }); + + expect(crs.zoom(crs.scale(8.9578457485))).toBe(8.9578457485); + expect(crs.zoom(crs.scale(8))).toBe(8); + expect(crs.zoom(crs.scale(1/8191))).toBeCloseTo(1/8191, 6); + expect(crs.zoom(crs.scale(0.5))).toBe(0.5); + expect(crs.zoom(crs.scale(0.51))).toBe(0.51); + }); });