From 06bd07ff8de423b1d6f33ca67038ce3b4fc61b3a Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 28 Dec 2012 20:24:57 +0100 Subject: [PATCH] EncodedPolyline: Added write() method --- lib/OpenLayers/Format/EncodedPolyline.js | 45 ++++++++++++++++++++++++ tests/Format/EncodedPolyline.html | 27 ++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/lib/OpenLayers/Format/EncodedPolyline.js b/lib/OpenLayers/Format/EncodedPolyline.js index 53050f294e..1255eb1c91 100644 --- a/lib/OpenLayers/Format/EncodedPolyline.js +++ b/lib/OpenLayers/Format/EncodedPolyline.js @@ -130,6 +130,51 @@ OpenLayers.Format.EncodedPolyline = OpenLayers.Class(OpenLayers.Format, { return points; }, + /** + * APIMethod: write + * Serialize a feature or array of features into a WKT string. + * + * Parameters: + * features - {|Array} A feature or array of + * features + * + * Returns: + * {String} The WKT string representation of the input geometries + */ + write: function(features) { + var feature; + if (features.constructor == Array) + feature = features[0]; + else + feature = features; + + var geometry = feature.geometry; + var type = geometry.CLASS_NAME.split('.')[2].toLowerCase(); + + var pointGeometries; + if (type == "point") + pointGeometries = new Array(geometry); + else if (type == "linestring" || + type == "linearring" || + type == "multipoint") + pointGeometries = geometry.components; + else if (type == "polygon") + pointGeometries = geometry.components[0].components; + else + return null; + + var points = new Array(); + for (var i in pointGeometries) { + var pointGeometry = pointGeometries[i]; + var point = [Math.round(pointGeometry.y * 1e5), + Math.round(pointGeometry.x * 1e5)]; + points.push(point); + } + + var result = this.encode(points, 2); + return result; + }, + /** * APIMethod: encode * Serialize an array of n-dimensional points and return an encoded string diff --git a/tests/Format/EncodedPolyline.html b/tests/Format/EncodedPolyline.html index 785b56df41..1a93a41afa 100644 --- a/tests/Format/EncodedPolyline.html +++ b/tests/Format/EncodedPolyline.html @@ -18,6 +18,8 @@ basePoints[2][0] * 1e-5) ]; + var singlePoint = new OpenLayers.Feature.Vector(points[0]); + var linestring = new OpenLayers.Feature.Vector( new OpenLayers.Geometry.LineString(points) ); @@ -91,6 +93,31 @@ } } + function test_Format_EncodedPolyline_write(t) { + t.plan(5); + + var format = new OpenLayers.Format.EncodedPolyline(); + + t.eq(format.write(linestring), encoded, + "format correctly writes encoded polyline"); + + t.eq(format.write(multipoint), encoded, + "format correctly writes encoded multipoint"); + + // Different output than encoded, + // because polygon closing point is included + t.eq(format.write(linearring), + "_p~iF~ps|U_ulLnnqC_mqNvxq`@~b_\\ghde@", + "format correctly writes encoded linearring"); + + t.eq(format.write(polygon), + "_p~iF~ps|U_ulLnnqC_mqNvxq`@~b_\\ghde@", + "format correctly writes encoded polygon"); + + t.eq(format.write(singlePoint), "_p~iF~ps|U", + "format correctly writes encoded point"); + } + function test_Format_EncodedPolyline_encode(t) { t.plan(1);