Keep longitude between -180 and 180

This commit is contained in:
Tim Schaub
2017-11-05 13:44:51 -07:00
parent 4a6317dde3
commit 42da3f18dd
2 changed files with 32 additions and 3 deletions

View File

@@ -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;
};

View File

@@ -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) {