diff --git a/spec/suites/geo/ProjectionSpec.js b/spec/suites/geo/ProjectionSpec.js index 41c469d7..f3ca8a5c 100644 --- a/spec/suites/geo/ProjectionSpec.js +++ b/spec/suites/geo/ProjectionSpec.js @@ -46,3 +46,51 @@ describe("Projection.Mercator", function () { }); }); }); +describe("Projection.SphericalMercator", function() { + var p = L.Projection.SphericalMercator; + + describe("#project", function() { + it("projects a center point", function() { + //edge cases + expect(p.project(new L.LatLng(0, 0))).near(new L.Point(0, 0)); + }); + + it("projects the northeast corner of the world", function() { + expect(p.project(new L.LatLng(90, 180))).near(new L.Point(20037508, 20037508)); + }); + + it("projects the southwest corner of the world", function() { + expect(p.project(new L.LatLng(-90, -180))).near(new L.Point(-20037508, -20037508)); + }); + + it("projects other points", function() { + expect(p.project(new L.LatLng(50, 30))).near(new L.Point(3339584, 6446275)); + + // from https://github.com/Leaflet/Leaflet/issues/1578 + expect(p.project(new L.LatLng(51.9371170300465, 80.11230468750001))) + .near(new L.Point(8918060.96409, 6788763.38325)); + }); + }); + + describe("#unproject", function() { + function pr(point) { + return p.project(p.unproject(point)); + } + + it("unprojects a center point", function() { + expect(pr(new L.Point(0, 0))).near(new L.Point(0, 0)); + }); + + it("unprojects pi points", function() { + expect(pr(new L.Point(-Math.PI, Math.PI))).near(new L.Point(-Math.PI, Math.PI)); + expect(pr(new L.Point(-Math.PI, -Math.PI))).near(new L.Point(-Math.PI, -Math.PI)); + + expect(pr(new L.Point(0.523598775598, 1.010683188683))).near(new L.Point(0.523598775598, 1.010683188683)); + }); + + it('unprojects other points', function () { + // from https://github.com/Leaflet/Leaflet/issues/1578 + expect(pr(new L.Point(8918060.964088084, 6755099.410887127))); + }); + }); +}); \ No newline at end of file diff --git a/src/geo/projection/Projection.SphericalMercator.js b/src/geo/projection/Projection.SphericalMercator.js index 0cf969f8..a3a6e03b 100644 --- a/src/geo/projection/Projection.SphericalMercator.js +++ b/src/geo/projection/Projection.SphericalMercator.js @@ -4,23 +4,23 @@ L.Projection.SphericalMercator = { MAX_LATITUDE: 85.0511287798, - + R_MAJOR: 6378137, project: function (latlng) { // (LatLng) -> Point var d = L.LatLng.DEG_TO_RAD, max = this.MAX_LATITUDE, lat = Math.max(Math.min(max, latlng.lat), -max), - x = latlng.lng * d, + x = latlng.lng * d * this.R_MAJOR, y = lat * d; y = Math.log(Math.tan((Math.PI / 4) + (y / 2))); - + y *= this.R_MAJOR; return new L.Point(x, y); }, unproject: function (point) { // (Point, Boolean) -> LatLng var d = L.LatLng.RAD_TO_DEG, - lng = point.x * d, - lat = (2 * Math.atan(Math.exp(point.y)) - (Math.PI / 2)) * d; + lng = point.x * d / this.R_MAJOR, + lat = (2 * Math.atan(Math.exp(point.y / this.R_MAJOR)) - (Math.PI / 2)) * d; return new L.LatLng(lat, lng); }