diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 4df00ddf62..b6d5b397ff 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -306,6 +306,7 @@ "OpenLayers/Format/GML/v2.js", "OpenLayers/Format/GML/v3.js", "OpenLayers/Format/Atom.js", + "OpenLayers/Format/EncodedPolyline.js", "OpenLayers/Format/KML.js", "OpenLayers/Format/GeoRSS.js", "OpenLayers/Format/WFS.js", diff --git a/lib/OpenLayers/Format/EncodedPolyline.js b/lib/OpenLayers/Format/EncodedPolyline.js new file mode 100644 index 0000000000..237782b7f7 --- /dev/null +++ b/lib/OpenLayers/Format/EncodedPolyline.js @@ -0,0 +1,85 @@ +/* Copyright (c) 2006-2012 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Format.js + * @requires OpenLayers/Feature/Vector.js + */ + +/** + * Class: OpenLayers.Format.EncodedPolyline + * Class for reading and writing encoded polylines. Create a new instance + * with the constructor. + * + * Inherits from: + * - + */ +OpenLayers.Format.EncodedPolyline = OpenLayers.Class(OpenLayers.Format, { + + /** + * Constructor: OpenLayers.Format.EncodedPolyline + * Create a new parser for encoded polylines + * + * Parameters: + * options - {Object} An optional object whose properties will be set on + * this instance + * + * Returns: + * {} A new encoded polylines parser. + */ + initialize: function(options) { + OpenLayers.Format.prototype.initialize.apply(this, [options]); + }, + + /** + * Method: read + * Deserialize an encoded polyline string and return a vector feature. + * + * Parameters: + * encoded - {String} An encoded polyline string + * + * Returns: + * {} A vector feature with a linestring. + */ + read: function(encoded) { + var points = new Array(); + + var lat = 0; + var lon = 0; + + for (var i = 0; i < encoded.length;) { + var b; + var result = 0; + var shift = 0; + + do { + b = encoded.charCodeAt(i++) - 63; + result |= (b & 0x1f) << shift; + shift += 5; + } while (b >= 0x20); + + lat += ((result & 1) ? ~(result >> 1) : (result >> 1)); + + result = 0; + shift = 0; + + do { + b = encoded.charCodeAt(i++) - 63; + result |= (b & 0x1f) << shift; + shift += 5; + } while (b >= 0x20); + + lon += ((result & 1) ? ~(result >> 1) : (result >> 1)); + + points.push(new OpenLayers.Geometry.Point(lon * 1e-5, lat * 1e-5)); + } + + return new OpenLayers.Feature.Vector( + new OpenLayers.Geometry.LineString(points) + ); + }, + + CLASS_NAME: "OpenLayers.Format.EncodedPolyline" +}); diff --git a/tests/Format/EncodedPolyline.html b/tests/Format/EncodedPolyline.html new file mode 100644 index 0000000000..29e0a65709 --- /dev/null +++ b/tests/Format/EncodedPolyline.html @@ -0,0 +1,41 @@ + + + + + + + + diff --git a/tests/list-tests.html b/tests/list-tests.html index 8c37464b84..dfcaa2f4a0 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -66,6 +66,7 @@
  • Format/XML/VersionedOGC.html
  • Format/ArcXML/Features.html
  • Format/CQL.html
  • +
  • Format/EncodedPolyline.html
  • Format/GeoJSON.html
  • Format/GeoRSS.html
  • Format/GML.html