From fef349120bc762076fbf5e99483d10cbbb46cffa Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Fri, 26 Jun 2020 17:26:41 +0200 Subject: [PATCH] Avoid extra extent width calculation --- src/ol/coordinate.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/ol/coordinate.js b/src/ol/coordinate.js index 29048f173a..85b31f325c 100644 --- a/src/ol/coordinate.js +++ b/src/ol/coordinate.js @@ -406,26 +406,35 @@ export function toStringXY(coordinate, opt_fractionDigits) { * exclusive. * * @param {Coordinate} coordinate Coordinate. - * @param {import("./proj/Projection.js").default} projection Projection + * @param {import("./proj/Projection.js").default} projection Projection. * @return {Coordinate} The coordinate within the real world extent. */ export function wrapX(coordinate, projection) { - const worldsAway = getWorldsAway(coordinate, projection); + const worldWidth = getWidth(projection.getExtent()); + const worldsAway = getWorldsAway(coordinate, projection, worldWidth); if (worldsAway) { - coordinate[0] -= worldsAway * getWidth(projection.getExtent()); + coordinate[0] -= worldsAway * worldWidth; } return coordinate; } - -export function getWorldsAway(coordinate, projection) { +/** + * @param {Coordinate} coordinate Coordinate. + * @param {import("./proj/Projection.js").default} projection Projection. + * @param {number=} opt_sourceExtentWidth Width of the source extent. + * @return {number} Offset in world widths. + */ +export function getWorldsAway(coordinate, projection, opt_sourceExtentWidth) { const projectionExtent = projection.getExtent(); let worldsAway = 0; if ( projection.canWrapX() && (coordinate[0] < projectionExtent[0] || coordinate[0] > projectionExtent[2]) ) { - const worldWidth = getWidth(projectionExtent); - worldsAway = Math.floor((coordinate[0] - projectionExtent[0]) / worldWidth); + const sourceExtentWidth = + opt_sourceExtentWidth || getWidth(projectionExtent); + worldsAway = Math.floor( + (coordinate[0] - projectionExtent[0]) / sourceExtentWidth + ); } return worldsAway; }