Merge pull request #1777 from fredj/polyline-cleanup

ol.format.Polyline cleanup
This commit is contained in:
Frédéric Junod
2014-02-28 10:40:11 +01:00
2 changed files with 67 additions and 140 deletions

View File

@@ -28,8 +28,7 @@ goog.inherits(ol.format.Polyline, ol.format.Text);
* @param {number=} opt_dimension The dimension of the coordinates in the array.
* @return {string} The encoded string.
*/
ol.format.Polyline.encodeFlatCoordinates =
function(flatPoints, opt_dimension) {
ol.format.Polyline.encodeFlatCoordinates = function(flatPoints, opt_dimension) {
var dimension = goog.isDef(opt_dimension) ? opt_dimension : 2;
return ol.format.Polyline.encodeDeltas(flatPoints, dimension);
};
@@ -40,7 +39,7 @@ ol.format.Polyline.encodeFlatCoordinates =
*
* @param {string} encoded An encoded string.
* @param {number=} opt_dimension The dimension of the coordinates in the
* encoded string.
* encoded string.
* @return {Array.<number>} A flat array of coordinates.
*/
ol.format.Polyline.decodeFlatCoordinates = function(encoded, opt_dimension) {
@@ -57,7 +56,8 @@ ol.format.Polyline.decodeFlatCoordinates = function(encoded, opt_dimension) {
* @param {Array.<number>} numbers A list of n-dimensional points.
* @param {number} dimension The dimension of the points in the list.
* @param {number=} opt_factor The factor by which the numbers will be
* multiplied. The remaining decimal places will get rounded away.
* multiplied. The remaining decimal places will get rounded away.
* Default is `1e5`.
* @return {string} The encoded string.
*/
ol.format.Polyline.encodeDeltas = function(numbers, dimension, opt_factor) {
@@ -90,7 +90,7 @@ ol.format.Polyline.encodeDeltas = function(numbers, dimension, opt_factor) {
* @param {string} encoded An encoded string.
* @param {number} dimension The dimension of the points in the encoded string.
* @param {number=} opt_factor The factor by which the resulting numbers will
* be divided.
* be divided. Default is `1e5`.
* @return {Array.<number>} A list of n-dimensional points.
*/
ol.format.Polyline.decodeDeltas = function(encoded, dimension, opt_factor) {
@@ -125,7 +125,8 @@ ol.format.Polyline.decodeDeltas = function(encoded, dimension, opt_factor) {
*
* @param {Array.<number>} numbers A list of floating point numbers.
* @param {number=} opt_factor The factor by which the numbers will be
* multiplied. The remaining decimal places will get rounded away.
* multiplied. The remaining decimal places will get rounded away.
* Default is `1e5`.
* @return {string} The encoded string.
*/
ol.format.Polyline.encodeFloats = function(numbers, opt_factor) {
@@ -145,6 +146,7 @@ ol.format.Polyline.encodeFloats = function(numbers, opt_factor) {
*
* @param {string} encoded An encoded string.
* @param {number=} opt_factor The factor by which the result will be divided.
* Default is `1e5`.
* @return {Array.<number>} A list of floating point numbers.
*/
ol.format.Polyline.decodeFloats = function(encoded, opt_factor) {
@@ -235,59 +237,6 @@ ol.format.Polyline.decodeUnsignedIntegers = function(encoded) {
};
/**
* Encode one single floating point number and return an encoded string
*
* @param {number} num Floating point number that should be encoded.
* @param {number=} opt_factor The factor by which num will be multiplied.
* The remaining decimal places will get rounded away.
* @return {string} The encoded string.
*/
ol.format.Polyline.encodeFloat = function(num, opt_factor) {
var factor = goog.isDef(opt_factor) ? opt_factor : 1e5;
num = Math.round(num * factor);
return ol.format.Polyline.encodeSignedInteger(num);
};
/**
* Decode one single floating point number from an encoded string
*
* @param {string} encoded An encoded string.
* @param {number=} opt_factor The factor by which the result will be divided.
* @return {number} The decoded floating point number.
*/
ol.format.Polyline.decodeFloat = function(encoded, opt_factor) {
var factor = goog.isDef(opt_factor) ? opt_factor : 1e5;
var result = ol.format.Polyline.decodeSignedInteger(encoded);
return result / factor;
};
/**
* 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.format.Polyline.encodeSignedInteger = function(num) {
var signedNum = (num < 0) ? ~(num << 1) : (num << 1);
return ol.format.Polyline.encodeUnsignedInteger(signedNum);
};
/**
* Decode one single signed integer from an encoded string
*
* @param {string} encoded An encoded string.
* @return {number} The decoded signed integer.
*/
ol.format.Polyline.decodeSignedInteger = function(encoded) {
var result = ol.format.Polyline.decodeUnsignedInteger(encoded);
return ((result & 1) ? ~(result >> 1) : (result >> 1));
};
/**
* Encode one single unsigned integer and return an encoded string
*
@@ -307,28 +256,6 @@ ol.format.Polyline.encodeUnsignedInteger = function(num) {
};
/**
* Decode one single unsigned integer from an encoded string
*
* @param {string} encoded An encoded string.
* @return {number} The decoded unsigned integer.
*/
ol.format.Polyline.decodeUnsignedInteger = function(encoded) {
var result = 0;
var shift = 0;
var i, ii;
for (i = 0, ii = encoded.length; i < ii; ++i) {
var b = encoded.charCodeAt(i) - 63;
result |= (b & 0x1f) << shift;
if (b < 0x20) {
break;
}
shift += 5;
}
return result;
};
/**
* @inheritDoc
*/

View File

@@ -140,47 +140,47 @@ describe('ol.format.Polyline', function() {
describe('encodeFloat', function() {
it('returns expected value', function() {
var encodeFloat = ol.format.Polyline.encodeFloat;
var encodeFloats = ol.format.Polyline.encodeFloats;
expect(encodeFloat(0.00000)).to.eql('?');
expect(encodeFloat(-0.00001)).to.eql('@');
expect(encodeFloat(0.00001)).to.eql('A');
expect(encodeFloat(-0.00002)).to.eql('B');
expect(encodeFloat(0.00002)).to.eql('C');
expect(encodeFloat(0.00015)).to.eql(']');
expect(encodeFloat(-0.00016)).to.eql('^');
expect(encodeFloats([0.00000])).to.eql('?');
expect(encodeFloats([-0.00001])).to.eql('@');
expect(encodeFloats([0.00001])).to.eql('A');
expect(encodeFloats([-0.00002])).to.eql('B');
expect(encodeFloats([0.00002])).to.eql('C');
expect(encodeFloats([0.00015])).to.eql(']');
expect(encodeFloats([-0.00016])).to.eql('^');
expect(encodeFloat(-0.1, 10)).to.eql('@');
expect(encodeFloat(0.1, 10)).to.eql('A');
expect(encodeFloats([-0.1], 10)).to.eql('@');
expect(encodeFloats([0.1], 10)).to.eql('A');
expect(encodeFloat(16 * 32 / 1e5)).to.eql('__@');
expect(encodeFloat(16 * 32 * 32 / 1e5)).to.eql('___@');
expect(encodeFloats([16 * 32 / 1e5])).to.eql('__@');
expect(encodeFloats([16 * 32 * 32 / 1e5])).to.eql('___@');
// from the "Encoded Polyline Algorithm Format" page at Google
expect(encodeFloat(-179.9832104)).to.eql('`~oia@');
expect(encodeFloats([-179.9832104])).to.eql('`~oia@');
});
});
describe('decodeFloat', function() {
it('returns expected value', function() {
var decodeFloat = ol.format.Polyline.decodeFloat;
var decodeFloats = ol.format.Polyline.decodeFloats;
expect(decodeFloat('?')).to.eql(0.00000);
expect(decodeFloat('@')).to.eql(-0.00001);
expect(decodeFloat('A')).to.eql(0.00001);
expect(decodeFloat('B')).to.eql(-0.00002);
expect(decodeFloat('C')).to.eql(0.00002);
expect(decodeFloat(']')).to.eql(0.00015);
expect(decodeFloat('^')).to.eql(-0.00016);
expect(decodeFloats('?')).to.eql([0.00000]);
expect(decodeFloats('@')).to.eql([-0.00001]);
expect(decodeFloats('A')).to.eql([0.00001]);
expect(decodeFloats('B')).to.eql([-0.00002]);
expect(decodeFloats('C')).to.eql([0.00002]);
expect(decodeFloats(']')).to.eql([0.00015]);
expect(decodeFloats('^')).to.eql([-0.00016]);
expect(decodeFloat('@', 10)).to.eql(-0.1);
expect(decodeFloat('A', 10)).to.eql(0.1);
expect(decodeFloats('@', 10)).to.eql([-0.1]);
expect(decodeFloats('A', 10)).to.eql([0.1]);
expect(decodeFloat('__@')).to.eql(16 * 32 / 1e5);
expect(decodeFloat('___@')).to.eql(16 * 32 * 32 / 1e5);
expect(decodeFloats('__@')).to.eql([16 * 32 / 1e5]);
expect(decodeFloats('___@')).to.eql([16 * 32 * 32 / 1e5]);
// from the "Encoded Polyline Algorithm Format" page at Google
expect(decodeFloat('`~oia@')).to.eql(-179.98321);
expect(decodeFloats('`~oia@')).to.eql([-179.98321]);
});
});
@@ -188,37 +188,37 @@ describe('ol.format.Polyline', function() {
describe('encodeSignedInteger', function() {
it('returns expected value', function() {
var encodeSignedInteger = ol.format.Polyline.encodeSignedInteger;
var encodeSignedIntegers = ol.format.Polyline.encodeSignedIntegers;
expect(encodeSignedInteger(0)).to.eql('?');
expect(encodeSignedInteger(-1)).to.eql('@');
expect(encodeSignedInteger(1)).to.eql('A');
expect(encodeSignedInteger(-2)).to.eql('B');
expect(encodeSignedInteger(2)).to.eql('C');
expect(encodeSignedInteger(15)).to.eql(']');
expect(encodeSignedInteger(-16)).to.eql('^');
expect(encodeSignedIntegers([0])).to.eql('?');
expect(encodeSignedIntegers([-1])).to.eql('@');
expect(encodeSignedIntegers([1])).to.eql('A');
expect(encodeSignedIntegers([-2])).to.eql('B');
expect(encodeSignedIntegers([2])).to.eql('C');
expect(encodeSignedIntegers([15])).to.eql(']');
expect(encodeSignedIntegers([-16])).to.eql('^');
expect(encodeSignedInteger(16)).to.eql('_@');
expect(encodeSignedInteger(16 * 32)).to.eql('__@');
expect(encodeSignedInteger(16 * 32 * 32)).to.eql('___@');
expect(encodeSignedIntegers([16])).to.eql('_@');
expect(encodeSignedIntegers([16 * 32])).to.eql('__@');
expect(encodeSignedIntegers([16 * 32 * 32])).to.eql('___@');
});
});
describe('decodeSignedInteger', function() {
it('returns expected value', function() {
var decodeSignedInteger = ol.format.Polyline.decodeSignedInteger;
var decodeSignedIntegers = ol.format.Polyline.decodeSignedIntegers;
expect(decodeSignedInteger('?')).to.eql(0);
expect(decodeSignedInteger('@')).to.eql(-1);
expect(decodeSignedInteger('A')).to.eql(1);
expect(decodeSignedInteger('B')).to.eql(-2);
expect(decodeSignedInteger('C')).to.eql(2);
expect(decodeSignedInteger(']')).to.eql(15);
expect(decodeSignedInteger('^')).to.eql(-16);
expect(decodeSignedIntegers('?')).to.eql([0]);
expect(decodeSignedIntegers('@')).to.eql([-1]);
expect(decodeSignedIntegers('A')).to.eql([1]);
expect(decodeSignedIntegers('B')).to.eql([-2]);
expect(decodeSignedIntegers('C')).to.eql([2]);
expect(decodeSignedIntegers(']')).to.eql([15]);
expect(decodeSignedIntegers('^')).to.eql([-16]);
expect(decodeSignedInteger('_@')).to.eql(16);
expect(decodeSignedInteger('__@')).to.eql(16 * 32);
expect(decodeSignedInteger('___@')).to.eql(16 * 32 * 32);
expect(decodeSignedIntegers('_@')).to.eql([16]);
expect(decodeSignedIntegers('__@')).to.eql([16 * 32]);
expect(decodeSignedIntegers('___@')).to.eql([16 * 32 * 32]);
});
});
@@ -246,21 +246,21 @@ describe('ol.format.Polyline', function() {
describe('decodeUnsignedInteger', function() {
it('returns expected value', function() {
var decodeUnsignedInteger = ol.format.Polyline.decodeUnsignedInteger;
var decodeUnsignedIntegers = ol.format.Polyline.decodeUnsignedIntegers;
expect(decodeUnsignedInteger('?')).to.eql(0);
expect(decodeUnsignedInteger('@')).to.eql(1);
expect(decodeUnsignedInteger('A')).to.eql(2);
expect(decodeUnsignedInteger(']')).to.eql(30);
expect(decodeUnsignedInteger('^')).to.eql(31);
expect(decodeUnsignedInteger('_@')).to.eql(32);
expect(decodeUnsignedIntegers('?')).to.eql([0]);
expect(decodeUnsignedIntegers('@')).to.eql([1]);
expect(decodeUnsignedIntegers('A')).to.eql([2]);
expect(decodeUnsignedIntegers(']')).to.eql([30]);
expect(decodeUnsignedIntegers('^')).to.eql([31]);
expect(decodeUnsignedIntegers('_@')).to.eql([32]);
expect(decodeUnsignedInteger('__@')).to.eql(32 * 32);
expect(decodeUnsignedInteger('__D')).to.eql(5 * 32 * 32);
expect(decodeUnsignedInteger('___@')).to.eql(32 * 32 * 32);
expect(decodeUnsignedIntegers('__@')).to.eql([32 * 32]);
expect(decodeUnsignedIntegers('__D')).to.eql([5 * 32 * 32]);
expect(decodeUnsignedIntegers('___@')).to.eql([32 * 32 * 32]);
// from the "Encoded Polyline Algorithm Format" page at Google
expect(decodeUnsignedInteger('mD')).to.eql(174);
expect(decodeUnsignedIntegers('mD')).to.eql([174]);
});
});