Improve readability and efficiency

This commit is contained in:
Andreas Hocevar
2020-06-26 10:14:22 +02:00
parent bef4d8a494
commit 39f4627b8c

View File

@@ -644,32 +644,29 @@ export function createSafeCoordinateTransform(
inverse inverse
) { ) {
return function (coord) { return function (coord) {
const worldsAway = getWorldsAway(coord, sourceProj); let x, y, worldsAway;
const sourceExtent = sourceProj.getExtent(); if (sourceProj.canWrapX()) {
const destExtent = destProj.getExtent(); worldsAway = getWorldsAway(coord, sourceProj);
let clampBottom, clampTop; const sourceExtent = sourceProj.getExtent();
if (destExtent) { if (worldsAway && sourceExtent) {
const clampBottomLeft = inverse(destExtent.slice(0, 2)); x = coord[0] - worldsAway * getWidth(sourceExtent);
const clampTopRight = inverse(destExtent.slice(2, 4)); }
clampBottom = isNaN(clampBottomLeft[1])
? sourceExtent
? sourceExtent[1]
: undefined
: clampBottomLeft[1];
clampTop = isNaN(clampTopRight[1])
? sourceExtent
? sourceExtent[3]
: undefined
: clampTopRight[1];
} }
const transformed = sourceExtent const destExtent = destProj.getExtent();
? forward([ if (destExtent) {
coord[0] - worldsAway * getWidth(sourceExtent), const clampMin = inverse(destExtent.slice(0, 2));
clampTop ? clamp(coord[1], clampBottom, clampTop) : coord[1], const clampMax = inverse(destExtent.slice(2, 4));
]) if (!isNaN(clampMin[1]) && !isNaN(clampMax[1])) {
: forward(coord); y = clamp(coord[1], clampMin[1], clampMax[1]);
}
}
const transformed = forward(
x === undefined && y === undefined
? coord
: [x === undefined ? coord[0] : x, y === undefined ? coord[1] : y]
);
if (worldsAway && destProj.canWrapX()) { if (worldsAway && destProj.canWrapX()) {
transformed[0] += worldsAway * getWidth(destProj.getExtent()); transformed[0] += worldsAway * getWidth(destExtent);
} }
return transformed; return transformed;
}; };