Skip to content

Commit

Permalink
Merge pull request #979 from OpenGeoscience/transform-axis-faster
Browse files Browse the repository at this point in the history
Speed up coordinate transforms that only switch y-axis.
  • Loading branch information
manthey authored Feb 28, 2019
2 parents 315b653 + fd7fda0 commit 7e1e96a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 17 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 7e1e96a

Please sign in to comment.