From 42da3f18dd43f2302de82ad8341f0bccfc225dc7 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sun, 5 Nov 2017 13:44:51 -0700 Subject: [PATCH] Keep longitude between -180 and 180 --- src/ol/proj.js | 8 +++++- .../ol/{proj/index.test.js => proj.test.js} | 27 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) rename test/spec/ol/{proj/index.test.js => proj.test.js} (96%) diff --git a/src/ol/proj.js b/src/ol/proj.js index fe2c0c9765..bf5b0bb6ac 100644 --- a/src/ol/proj.js +++ b/src/ol/proj.js @@ -3,6 +3,7 @@ goog.provide('ol.proj'); goog.require('ol'); goog.require('ol.Sphere'); goog.require('ol.extent'); +goog.require('ol.math'); goog.require('ol.proj.EPSG3857'); goog.require('ol.proj.EPSG4326'); goog.require('ol.proj.Projection'); @@ -282,8 +283,13 @@ ol.proj.fromLonLat = function(coordinate, opt_projection) { * @api */ ol.proj.toLonLat = function(coordinate, opt_projection) { - return ol.proj.transform(coordinate, + var lonLat = ol.proj.transform(coordinate, opt_projection !== undefined ? opt_projection : 'EPSG:3857', 'EPSG:4326'); + var lon = lonLat[0]; + if (lon < -180 || lon > 180) { + lonLat[0] = ol.math.modulo(lon + 180, 360) - 180; + } + return lonLat; }; diff --git a/test/spec/ol/proj/index.test.js b/test/spec/ol/proj.test.js similarity index 96% rename from test/spec/ol/proj/index.test.js rename to test/spec/ol/proj.test.js index 211d19149e..5e68cbb34f 100644 --- a/test/spec/ol/proj/index.test.js +++ b/test/spec/ol/proj.test.js @@ -1,6 +1,5 @@ - - goog.require('ol.proj'); +goog.require('ol.proj.EPSG3857'); goog.require('ol.proj.EPSG4326'); goog.require('ol.proj.Projection'); @@ -12,6 +11,30 @@ describe('ol.proj', function() { ol.proj.addCommon(); }); + describe('toLonLat()', function() { + var cases = [{ + from: [0, 0], + to: [0, 0] + }, { + from: [-12356463.478053365, 5700582.732404122], + to: [-111, 45.5] + }, { + from: [2 * ol.proj.EPSG3857.HALF_SIZE - 12356463.478053365, 5700582.732404122], + to: [-111, 45.5] + }, { + from: [-4 * ol.proj.EPSG3857.HALF_SIZE - 12356463.478053365, 5700582.732404122], + to: [-111, 45.5] + }]; + + cases.forEach(function(c) { + it('works for ' + c.from.join(', '), function() { + var lonLat = ol.proj.toLonLat(c.from); + expect(lonLat[0]).to.roughlyEqual(c.to[0], 1e-9); + expect(lonLat[1]).to.roughlyEqual(c.to[1], 1e-9); + }); + }); + }); + describe('projection equivalence', function() { function _testAllEquivalent(codes) {