EncodedPolyline: Added encode() method
The write() method will follow in the next commit.
This commit is contained in:
@@ -130,5 +130,80 @@ OpenLayers.Format.EncodedPolyline = OpenLayers.Class(OpenLayers.Format, {
|
|||||||
return points;
|
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"
|
CLASS_NAME: "OpenLayers.Format.EncodedPolyline"
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -91,6 +91,14 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_Format_EncodedPolyline_encode(t) {
|
||||||
|
t.plan(1);
|
||||||
|
|
||||||
|
var format = new OpenLayers.Format.EncodedPolyline();
|
||||||
|
|
||||||
|
t.eq(format.encode(basePoints, 2), encoded);
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user