From 6f0812239808d7f020039413024bb9da558c08c3 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Sun, 10 Mar 2013 20:45:12 +0100 Subject: [PATCH] polyline: Used new *Deltas() functions in *FlatCoordinates() --- src/ol/parser/polyline.js | 50 +++------------------------- test/spec/ol/parser/polyline.test.js | 2 +- 2 files changed, 5 insertions(+), 47 deletions(-) diff --git a/src/ol/parser/polyline.js b/src/ol/parser/polyline.js index 7303d8e871..f07c979894 100644 --- a/src/ol/parser/polyline.js +++ b/src/ol/parser/polyline.js @@ -4,6 +4,8 @@ goog.provide('ol.parser.polyline'); /** * Encode a list of coordinates in a flat array and return an encoded string * + * Attention: This function will modify the passed array! + * * @param {Array.} flatPoints A flat array of coordinates. * @param {number=} opt_dimension The dimension of the coordinates in the array. * @return {string} The encoded string. @@ -11,25 +13,7 @@ goog.provide('ol.parser.polyline'); ol.parser.polyline.encodeFlatCoordinates = function(flatPoints, opt_dimension) { var dimension = opt_dimension || 2; - var i; - - var lastPoint = new Array(dimension); - for (i = 0; i < dimension; ++i) { - lastPoint[i] = 0; - } - - var encoded = '', flatPointsLength = flatPoints.length; - for (i = 0; i < flatPointsLength;) { - for (var d = 0; d < dimension; ++d) { - var part = Math.round(flatPoints[i++] * 1e5); - var delta = part - lastPoint[d]; - lastPoint[d] = part; - - encoded += ol.parser.polyline.encodeSignedInteger(delta); - } - } - - return encoded; + return ol.parser.polyline.encodeDeltas(flatPoints, dimension); }; @@ -43,36 +27,10 @@ ol.parser.polyline.encodeFlatCoordinates = */ ol.parser.polyline.decodeFlatCoordinates = function(encoded, opt_dimension) { var dimension = opt_dimension || 2; - var i; - - var lastPoint = new Array(dimension); - for (i = 0; i < dimension; ++i) { - lastPoint[i] = 0; - } - - var flatPoints = new Array(), encodedLength = encoded.length; - for (i = 0; i < encodedLength;) { - for (var d = 0; d < dimension; ++d) { - var result = 0; - var shift = 0; - - var b; - do { - b = encoded.charCodeAt(i++) - 63; - result |= (b & 0x1f) << shift; - shift += 5; - } while (b >= 0x20); - - lastPoint[d] += (result & 1) ? ~(result >> 1) : (result >> 1); - flatPoints.push(lastPoint[d] / 1e5); - } - } - - return flatPoints; + return ol.parser.polyline.decodeDeltas(encoded, dimension); }; - /** * Encode a list of n-dimensional points and return an encoded string * diff --git a/test/spec/ol/parser/polyline.test.js b/test/spec/ol/parser/polyline.test.js index 270b263366..ae5d0e3de7 100644 --- a/test/spec/ol/parser/polyline.test.js +++ b/test/spec/ol/parser/polyline.test.js @@ -12,7 +12,7 @@ describe('ol.parser.polyline', function() { // from the "Encoded Polyline Algorithm Format" page at Google expect(encodeFlatCoordinates( - flat_points)).toEqual('_p~iF~ps|U_ulLnnqC_mqNvxq`@'); + flat_points.slice())).toEqual('_p~iF~ps|U_ulLnnqC_mqNvxq`@'); }); });