Format: Added EncodedPolyline class

This class is able to convert an encoded polyline string into a LineString embedded in a Vector Feature.

See https://developers.google.com/maps/documentation/utilities/polylinealgorithm for more information.
This commit is contained in:
Tobias Bieniek
2012-12-26 18:25:04 +01:00
parent 4b2e80fa36
commit 63a9567858
4 changed files with 128 additions and 0 deletions

View File

@@ -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",

View File

@@ -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 <OpenLayers.Format.EncodedPolyline> constructor.
*
* Inherits from:
* - <OpenLayers.Format>
*/
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:
* {<OpenLayers.Format.EncodedPolyline>} 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:
* {<OpenLayers.Feature.Vector>} 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"
});

View File

@@ -0,0 +1,41 @@
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
var linestring = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString([
new OpenLayers.Geometry.Point(-120.2, 38.5),
new OpenLayers.Geometry.Point(-120.95, 40.7),
new OpenLayers.Geometry.Point(-126.45300000000002, 43.252)
])
);
var encoded = "_p~iF~ps|U_ulLnnqC_mqNvxq`@";
function test_Format_EncodedPolyline_constructor(t) {
t.plan(4);
var options = {'foo': 'bar'};
var format = new OpenLayers.Format.EncodedPolyline(options);
t.ok(format instanceof OpenLayers.Format.EncodedPolyline,
"new OpenLayers.Format.EncodedPolyline returns object" );
t.eq(format.foo, "bar", "constructor sets options correctly");
t.eq(typeof format.read, "function", "format has a read function");
t.eq(typeof format.write, "function", "format has a write function");
}
function test_Format_EncodedPolyline_read(t) {
t.plan(1);
var format = new OpenLayers.Format.EncodedPolyline();
t.ok(linestring.geometry.equals(format.read(encoded).geometry),
"format correctly reads encoded polyline");
}
</script>
</head>
<body>
</body>
</html>

View File

@@ -66,6 +66,7 @@
<li>Format/XML/VersionedOGC.html</li>
<li>Format/ArcXML/Features.html</li>
<li>Format/CQL.html</li>
<li>Format/EncodedPolyline.html</li>
<li>Format/GeoJSON.html</li>
<li>Format/GeoRSS.html</li>
<li>Format/GML.html</li>