Added basic ol.parser.polyline module
This module will contain the low-level functions for Google's "Encoded Polyline" algorithm.
This commit is contained in:
35
src/ol/parser/polyline.js
Normal file
35
src/ol/parser/polyline.js
Normal file
@@ -0,0 +1,35 @@
|
||||
goog.provide('ol.parser.polyline');
|
||||
|
||||
|
||||
/**
|
||||
* Encode one single signed integer and return an encoded string
|
||||
*
|
||||
* @param {number} num Signed integer that should be encoded.
|
||||
* @return {string} The encoded string.
|
||||
*/
|
||||
ol.parser.polyline.encodeSignedInteger = function(num) {
|
||||
var sgn_num = num << 1;
|
||||
if (num < 0)
|
||||
sgn_num = ~(sgn_num);
|
||||
|
||||
return ol.parser.polyline.encodeUnsignedInteger(sgn_num);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Encode one single unsigned integer and return an encoded string
|
||||
*
|
||||
* @param {number} num Unsigned integer that should be encoded.
|
||||
* @return {string} The encoded string.
|
||||
*/
|
||||
ol.parser.polyline.encodeUnsignedInteger = function(num) {
|
||||
var value, encodeString = '';
|
||||
while (num >= 0x20) {
|
||||
value = (0x20 | (num & 0x1f)) + 63;
|
||||
encodeString += (String.fromCharCode(value));
|
||||
num >>= 5;
|
||||
}
|
||||
value = num + 63;
|
||||
encodeString += (String.fromCharCode(value));
|
||||
return encodeString;
|
||||
};
|
||||
41
test/spec/ol/parser/polyline.test.js
Normal file
41
test/spec/ol/parser/polyline.test.js
Normal file
@@ -0,0 +1,41 @@
|
||||
goog.provide('ol.test.parser.polyline');
|
||||
|
||||
describe('ol.parser.polyline', function() {
|
||||
|
||||
describe('encodeSignedInteger', function() {
|
||||
it('returns expected value', function() {
|
||||
var encodeSignedInteger = ol.parser.polyline.encodeSignedInteger;
|
||||
|
||||
expect(encodeSignedInteger(0)).toEqual('?');
|
||||
expect(encodeSignedInteger(-1)).toEqual('@');
|
||||
expect(encodeSignedInteger(1)).toEqual('A');
|
||||
expect(encodeSignedInteger(-2)).toEqual('B');
|
||||
expect(encodeSignedInteger(2)).toEqual('C');
|
||||
expect(encodeSignedInteger(15)).toEqual(']');
|
||||
expect(encodeSignedInteger(-16)).toEqual('^');
|
||||
|
||||
expect(encodeSignedInteger(16)).toEqual('_@');
|
||||
expect(encodeSignedInteger(16 * 32)).toEqual('__@');
|
||||
expect(encodeSignedInteger(16 * 32 * 32)).toEqual('___@');
|
||||
});
|
||||
});
|
||||
|
||||
describe('encodeUnsignedInteger', function() {
|
||||
it('returns expected value', function() {
|
||||
var encodeUnsignedInteger = ol.parser.polyline.encodeUnsignedInteger;
|
||||
|
||||
expect(encodeUnsignedInteger(0)).toEqual('?');
|
||||
expect(encodeUnsignedInteger(1)).toEqual('@');
|
||||
expect(encodeUnsignedInteger(2)).toEqual('A');
|
||||
expect(encodeUnsignedInteger(30)).toEqual(']');
|
||||
expect(encodeUnsignedInteger(31)).toEqual('^');
|
||||
expect(encodeUnsignedInteger(32)).toEqual('_@');
|
||||
|
||||
expect(encodeUnsignedInteger(32 * 32)).toEqual('__@');
|
||||
expect(encodeUnsignedInteger(5 * 32 * 32)).toEqual('__D');
|
||||
expect(encodeUnsignedInteger(32 * 32 * 32)).toEqual('___@');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
goog.require('ol.parser.polyline');
|
||||
Reference in New Issue
Block a user