From 4d07cefec81020530d8da79fbfe6fdc052423d4f Mon Sep 17 00:00:00 2001 From: Jonathan Beezley Date: Thu, 14 Apr 2016 09:05:57 -0400 Subject: [PATCH] Cache proj4 transforms for transformCoordinates --- src/transform.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/transform.js b/src/transform.js index 79f7f0f5aa..90f7c6761d 100644 --- a/src/transform.js +++ b/src/transform.js @@ -157,6 +157,13 @@ var transform = function (options) { return this; }; +/** + * This object stores a cache of transform classes so they don't have + * to be recreated on every call to transformCoordinates. + * @private + */ +var cache = {}; + /** * Transform an array of coordinates from one projection into another. The * transformation may occur in place (modifying the input coordinate array), @@ -182,7 +189,14 @@ transform.transformCoordinates = function ( } var i, count, offset, xAcc, yAcc, zAcc, writer, output, projPoint, - trans = transform({source: srcPrj, target: tgtPrj}); + trans; + + if (!cache.hasOwnProperty(srcPrj) || !cache[srcPrj].hasOwnProperty(tgtPrj)) { + cache[srcPrj] = cache[srcPrj] || {}; + cache[srcPrj][tgtPrj] = transform({source: srcPrj, target: tgtPrj}); + } + + trans = cache[srcPrj][tgtPrj]; /// Default Z accessor zAcc = function () {