From fd7fda000f2c03c43c48cb3ed19d54915907625c Mon Sep 17 00:00:00 2001 From: David Manthey Date: Wed, 20 Feb 2019 14:26:42 -0500 Subject: [PATCH] Speed up coordinate transforms that only switch y-axis. This is a common transform for pixel-space displays. --- CHANGELOG.md | 1 + src/transform.js | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 775f2969d4..38da5ce6b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Improvements - Make fewer function calls when computing polygon strokes (#980) +- Speed up coordinate transforms that only switch y-axis (#979) ## Version 0.19.1 diff --git a/src/transform.js b/src/transform.js index 45fc32635a..aba5b23811 100644 --- a/src/transform.js +++ b/src/transform.js @@ -14,6 +14,10 @@ var transformCache = {}; * simple behavior. */ var maxTransformCacheSize = 10; +/* A RegExp to detect if two transforms only different by the middle axis's + * direction. */ +var axisPattern = new RegExp('^(.* |)\\+axis=e(n|s)u(| .*)$'); + /** * This purpose of this class is to provide a generic interface for computing * coordinate transformationss. The interface is taken from the proj4js, @@ -458,16 +462,27 @@ transform.transformCoordinatesArray = function (trans, coordinates, numberOfComp * coordinates are an array of [x0, y0, z0, x1, y1, z1, ...]. * * @param {string} srcPrj The source projection. - * @param {string} tgtPrj The destination projection. + * @param {string} tgtPrj The destination projection. This must not be the + * same as the source projection. * @param {number[]} coordinates A flat array of values. * @returns {number[]} The transformed coordinates. */ transform.transformCoordinatesFlatArray3 = function (srcPrj, tgtPrj, coordinates) { 'use strict'; + var i, + smatch = srcPrj.match(axisPattern), + tmatch = tgtPrj.match(axisPattern); + // if the two projections only differ in the middle axis + if (smatch && tmatch && smatch[1] === tmatch[1] && smatch[3] === tmatch[3]) { + for (i = coordinates.length - 3 + 1; i >= 0; i -= 3) { + coordinates[i] *= -1; + } + return coordinates; + } var src = proj4.Proj(srcPrj), tgt = proj4.Proj(tgtPrj), - i, projPoint, initPoint = {}; + projPoint, initPoint = {}; for (i = coordinates.length - 3; i >= 0; i -= 3) { initPoint.x = +coordinates[i]; initPoint.y = +coordinates[i + 1];