EncodedPolyline: Added write() method

This commit is contained in:
Tobias Bieniek
2012-12-28 20:24:57 +01:00
parent 06409da72e
commit 06bd07ff8d
2 changed files with 72 additions and 0 deletions

View File

@@ -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 - {<OpenLayers.Feature.Vector>|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

View File

@@ -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);