Skip to content

Commit

Permalink
added new zoom method as inverse of existing scale method
Browse files Browse the repository at this point in the history
  • Loading branch information
semone authored and perliedman committed Oct 16, 2015
1 parent a12660e commit 51f9571
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/proj4leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
18 changes: 17 additions & 1 deletion test/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down Expand Up @@ -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);
});
});

0 comments on commit 51f9571

Please sign in to comment.