EncodedPolyline: Added encode() method

The write() method will follow in the next commit.
This commit is contained in:
Tobias Bieniek
2012-12-28 01:31:24 +01:00
parent 8651e05e75
commit 510d42b12f
2 changed files with 83 additions and 0 deletions

View File

@@ -130,5 +130,80 @@ OpenLayers.Format.EncodedPolyline = OpenLayers.Class(OpenLayers.Format, {
return points;
},
/**
* Method: encode
* Serialize an array of n-dimensional points and return an encoded string
*
* Parameters:
* points - {Array(Array(int))} An array containing n-dimensional
* arrays of coordinates
* dims - {int} The dimension of the points that should be read
*
* Returns:
* {String} An encoded string
*/
encode: function (points, dims) {
var encoded_points = "";
var lastPoint = new Array(dims);
for (var i = 0; i < lastPoint.length; ++i)
lastPoint[i] = 0;
for (var i = 0; i < points.length; i++) {
var point = points[i];
for (var dim = 0; dim < lastPoint.length; ++dim) {
var delta = point[dim] - lastPoint[dim];
encoded_points += this.encodeSignedNumber(delta);
}
lastPoint = point;
}
return encoded_points;
},
/**
* Method: encodeSignedNumber
* Encode one single signed integer and return an encoded string
*
* Parameters:
* num - {int} A signed integer that should be encoded
*
* Returns:
* {String} An encoded string
*/
encodeSignedNumber: function (num) {
var sgn_num = num << 1;
if (num < 0)
sgn_num = ~(sgn_num);
return this.encodeNumber(sgn_num);
},
/**
* Method: encodeNumber
* Encode one single unsigned integer and return an encoded string
*
* encodeSignedNumber should be used instead of using this method directly!
*
* Parameters:
* num - {int} An unsigned integer that should be encoded
*
* Returns:
* {String} An encoded string
*/
encodeNumber: function (num) {
var encodeString = "";
var value;
while (num >= 0x20) {
value = (0x20 | (num & 0x1f)) + 63;
encodeString += (String.fromCharCode(value));
num >>= 5;
}
value = num + 63;
encodeString += (String.fromCharCode(value));
return encodeString;
},
CLASS_NAME: "OpenLayers.Format.EncodedPolyline"
});