Projection fixes, specs

This commit is contained in:
mourner
2010-09-07 11:59:58 +03:00
parent f734679158
commit 16097461f5
2 changed files with 44 additions and 10 deletions

View File

@ -1,5 +1,42 @@
describe("Projection.Mercator", function() {
it("should have tests", function() {
expect().toBe();
var p = L.Projection.Mercator;
beforeEach(function() {
function almostEqual(a, b, p) {
return Math.abs(a - b) <= (p || 1.0E-12);
};
this.addMatchers({
toAlmostEqual: function(expected, margin) {
var p1 = this.actual,
p2 = expected;
return almostEqual(p1.x, p2.x, margin) && almostEqual(p1.y, p2.y, margin);
}
});
});
describe("#project", function() {
it("should do projection properly", function() {
//edge cases
expect(p.project(new L.LatLng(0, 0))).toAlmostEqual(new L.Point(0, 0));
expect(p.project(new L.LatLng(90, 180))).toAlmostEqual(new L.Point(-Math.PI, Math.PI));
expect(p.project(new L.LatLng(-90, -180))).toAlmostEqual(new L.Point(-Math.PI, -Math.PI));
expect(p.project(new L.LatLng(50, 30))).toAlmostEqual(new L.Point(0.523598775598, 1.010683188683));
});
});
describe("#unproject", function() {
it("should do unprojection properly", function() {
function pr(point) {
return p.project(p.unproject(point));
}
expect(pr(new L.Point(0, 0))).toAlmostEqual(new L.Point(0, 0));
expect(pr(new L.Point(-Math.PI, Math.PI))).toAlmostEqual(new L.Point(-Math.PI, Math.PI));
expect(pr(new L.Point(-Math.PI, -Math.PI))).toAlmostEqual(new L.Point(-Math.PI, -Math.PI));
expect(pr(new L.Point(0.523598775598, 1.010683188683))).toAlmostEqual(new L.Point(0.523598775598, 1.010683188683));
});
});
});

View File

@ -6,12 +6,10 @@ L.Projection = {};
// Mercator Projection - see http://en.wikipedia.org/wiki/Mercator_projection
L.Projection.Mercator = {
statics: {
MAX_LATITUDE: (function() {
var a = Math.exp(2 * Math.PI);
return Math.asin((a - 1)/(a + 1)) * L.LatLng.RAD_TO_DEG;
})()
},
MAX_LATITUDE: (function() {
var a = Math.exp(2 * Math.PI);
return Math.asin((a - 1)/(a + 1)) * L.LatLng.RAD_TO_DEG;
})(),
project: function(/*LatLng*/ latlng) /*-> Point*/ {
var d = L.LatLng.DEG_TO_RAD,
@ -19,10 +17,9 @@ L.Projection.Mercator = {
lat = Math.max(Math.min(max, latlng.lat), -max),
x = latlng.lng * d,
y = lat * d;
y = Math.log(Math.tan(Math.PI/4 + y/2));
return new CM.Point(x, y);
return new L.Point(x, y);
},
unproject: function(/*Point*/ point) /*-> LatLng*/ {