From 9804dc8fa3265538a9f937cc19e5089ebc4b3295 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Thu, 5 Nov 2020 21:51:05 +0100 Subject: [PATCH 1/2] Clamp to lowest/highest possible value instead of validity extent --- src/ol/proj/epsg3857.js | 18 ++++++++++++------ test/spec/ol/proj/epsg3857.test.js | 12 ++++++++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/ol/proj/epsg3857.js b/src/ol/proj/epsg3857.js index 31552638ab..7dd27aa262 100644 --- a/src/ol/proj/epsg3857.js +++ b/src/ol/proj/epsg3857.js @@ -31,6 +31,13 @@ export const EXTENT = [-HALF_SIZE, -HALF_SIZE, HALF_SIZE, HALF_SIZE]; */ export const WORLD_EXTENT = [-180, -85, 180, 85]; +/** + * Maximum safe value in y direction + * @const + * @type {number} + */ +export const MAX_SAFE_Y = RADIUS * Math.log(Math.tan((Math.PI * 180) / 360)); + /** * @classdesc * Projection object for web/spherical Mercator (EPSG:3857). @@ -87,14 +94,13 @@ export function fromEPSG4326(input, opt_output, opt_dimension) { output = new Array(length); } } - const halfSize = HALF_SIZE; for (let i = 0; i < length; i += dimension) { - output[i] = (halfSize * input[i]) / 180; + output[i] = (HALF_SIZE * input[i]) / 180; let y = RADIUS * Math.log(Math.tan((Math.PI * (+input[i + 1] + 90)) / 360)); - if (y > halfSize) { - y = halfSize; - } else if (y < -halfSize) { - y = -halfSize; + if (y > MAX_SAFE_Y) { + y = MAX_SAFE_Y; + } else if (y < -MAX_SAFE_Y) { + y = -MAX_SAFE_Y; } output[i + 1] = y; } diff --git a/test/spec/ol/proj/epsg3857.test.js b/test/spec/ol/proj/epsg3857.test.js index 036a8a1b52..eb7f1643e6 100644 --- a/test/spec/ol/proj/epsg3857.test.js +++ b/test/spec/ol/proj/epsg3857.test.js @@ -1,4 +1,8 @@ -import {HALF_SIZE, fromEPSG4326} from '../../../../src/ol/proj/epsg3857.js'; +import { + HALF_SIZE, + MAX_SAFE_Y, + fromEPSG4326, +} from '../../../../src/ol/proj/epsg3857.js'; import { addCommon, clearAllProjections, @@ -24,11 +28,11 @@ describe('ol/proj/epsg3857', function () { }, { g: [-180, -90], - m: [-HALF_SIZE, -HALF_SIZE], + m: [-HALF_SIZE, -MAX_SAFE_Y], }, { g: [180, 90], - m: [HALF_SIZE, HALF_SIZE], + m: [HALF_SIZE, MAX_SAFE_Y], }, { g: [-111.0429, 45.677], @@ -47,7 +51,7 @@ describe('ol/proj/epsg3857', function () { it('does not produce unexpected results for string coordinates', function () { const transformed = fromEPSG4326(['180', '90']); expect(transformed[0]).to.roughlyEqual(HALF_SIZE, 1e-5); - expect(transformed[1]).to.roughlyEqual(HALF_SIZE, 1e-5); + expect(transformed[1]).to.roughlyEqual(MAX_SAFE_Y, 1e-5); }); }); From 2526dc738c024ec313fd33dd7a352ad1a6de6566 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Thu, 5 Nov 2020 23:11:56 +0100 Subject: [PATCH 2/2] More straithgforward calculation of MAX_SAFE_Y Co-authored-by: MoonE --- src/ol/proj/epsg3857.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ol/proj/epsg3857.js b/src/ol/proj/epsg3857.js index 7dd27aa262..1a8ad31072 100644 --- a/src/ol/proj/epsg3857.js +++ b/src/ol/proj/epsg3857.js @@ -36,7 +36,7 @@ export const WORLD_EXTENT = [-180, -85, 180, 85]; * @const * @type {number} */ -export const MAX_SAFE_Y = RADIUS * Math.log(Math.tan((Math.PI * 180) / 360)); +export const MAX_SAFE_Y = RADIUS * Math.log(Math.tan(Math.PI / 2)); /** * @classdesc