From c118d9884da0f52279adcddf347378615865d822 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Thu, 27 Dec 2012 23:07:31 +0100 Subject: [PATCH] EncodedPolyline: Extracted universal decode(encoded, dims) method --- lib/OpenLayers/Format/EncodedPolyline.js | 57 ++++++++++++++++++------ tests/Format/EncodedPolyline.html | 30 +++++++++++-- 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/lib/OpenLayers/Format/EncodedPolyline.js b/lib/OpenLayers/Format/EncodedPolyline.js index a71b0a449b..5c7b68c221 100644 --- a/lib/OpenLayers/Format/EncodedPolyline.js +++ b/lib/OpenLayers/Format/EncodedPolyline.js @@ -61,11 +61,50 @@ OpenLayers.Format.EncodedPolyline = OpenLayers.Class(OpenLayers.Format, { else if (this.geometryType != "polygon") return null; + var points = this.decode(encoded, 2); + var pointGeometries = new Array(); + for (i in points) { + var point = points[i]; + pointGeometries.push( + new OpenLayers.Geometry.Point(point[1] * 1e-5, point[0] * 1e-5) + ); + } + + if (this.geometryType == "polygon") + return new OpenLayers.Feature.Vector( + new OpenLayers.Geometry.Polygon([ + new OpenLayers.Geometry.LinearRing(pointGeometries) + ]) + ); + + return new OpenLayers.Feature.Vector( + new geomType(pointGeometries) + ); + }, + + /** + * Method: decode + * Deserialize an encoded string and return an array of n-dimensional + * points. + * + * Parameters: + * encoded - {String} An encoded string + * dims - {int} The dimension of the points that are returned + * + * Returns: + * {Array(Array(int))} An array containing n-dimensional arrays of + * coordinates. + */ + decode: function(encoded, dims) { var points = new Array(); - var point = new Array(0, 0); + var point = new Array(dims); + + // Reset the point array + for (var i = 0; i < point.length; ++i) + point[i] = 0; for (var i = 0; i < encoded.length;) { - for (var dim = 0; dim < 2; ++dim) { + for (var dim = 0; dim < dims; ++dim) { var result = 0; var shift = 0; @@ -79,20 +118,10 @@ OpenLayers.Format.EncodedPolyline = OpenLayers.Class(OpenLayers.Format, { point[dim] += ((result & 1) ? ~(result >> 1) : (result >> 1)); } - points.push(new OpenLayers.Geometry.Point(point[1] * 1e-5, - point[0] * 1e-5)); + points.push(point.slice(0)); } - if (this.geometryType == "polygon") - return new OpenLayers.Feature.Vector( - new OpenLayers.Geometry.Polygon([ - new OpenLayers.Geometry.LinearRing(points) - ]) - ); - - return new OpenLayers.Feature.Vector( - new geomType(points) - ); + return points; }, CLASS_NAME: "OpenLayers.Format.EncodedPolyline" diff --git a/tests/Format/EncodedPolyline.html b/tests/Format/EncodedPolyline.html index 10f2487347..9ad7dbd726 100644 --- a/tests/Format/EncodedPolyline.html +++ b/tests/Format/EncodedPolyline.html @@ -3,10 +3,19 @@