polyline: Added encode/decodeFloats() functions
This commit is contained in:
@@ -72,6 +72,49 @@ ol.parser.polyline.decodeFlatCoordinates = function(encoded, opt_dimension) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode a list of floating point numbers and return an encoded string
|
||||||
|
*
|
||||||
|
* Attention: This function will modify the passed array!
|
||||||
|
*
|
||||||
|
* @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.
|
||||||
|
* @return {string} The encoded string.
|
||||||
|
*/
|
||||||
|
ol.parser.polyline.encodeFloats = function(numbers, opt_factor) {
|
||||||
|
var factor = opt_factor || 1e5;
|
||||||
|
|
||||||
|
var numbersLength = numbers.length;
|
||||||
|
for (var i = 0; i < numbersLength; ++i) {
|
||||||
|
numbers[i] = Math.round(numbers[i] * factor);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ol.parser.polyline.encodeSignedIntegers(numbers);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode a list of floating point numbers from an encoded string
|
||||||
|
*
|
||||||
|
* @param {string} encoded An encoded string.
|
||||||
|
* @param {number=} opt_factor The factor by which the result will be divided.
|
||||||
|
* @return {Array.<number>} A list of floating point numbers.
|
||||||
|
*/
|
||||||
|
ol.parser.polyline.decodeFloats = function(encoded, opt_factor) {
|
||||||
|
var factor = opt_factor || 1e5;
|
||||||
|
|
||||||
|
var numbers = ol.parser.polyline.decodeSignedIntegers(encoded);
|
||||||
|
|
||||||
|
var numbersLength = numbers.length;
|
||||||
|
for (var i = 0; i < numbersLength; ++i) {
|
||||||
|
numbers[i] /= factor;
|
||||||
|
}
|
||||||
|
|
||||||
|
return numbers;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode a list of signed integers and return an encoded string
|
* Encode a list of signed integers and return an encoded string
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -27,6 +27,31 @@ describe('ol.parser.polyline', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
var floats = [0.00, 0.15, -0.01, -0.16, 0.16, 0.01];
|
||||||
|
var smallFloats = [0.00000, 0.00015, -0.00001, -0.00016, 0.00016, 0.00001];
|
||||||
|
var encodedFloats = '?]@^_@A';
|
||||||
|
|
||||||
|
describe('encodeFloats', function() {
|
||||||
|
it('returns expected value', function() {
|
||||||
|
var encodeFloats = ol.parser.polyline.encodeFloats;
|
||||||
|
|
||||||
|
expect(encodeFloats(smallFloats.slice())).toEqual(encodedFloats);
|
||||||
|
expect(encodeFloats(smallFloats.slice(), 1e5)).toEqual(encodedFloats);
|
||||||
|
expect(encodeFloats(floats.slice(), 1e2)).toEqual(encodedFloats);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('decodeFloats', function() {
|
||||||
|
it('returns expected value', function() {
|
||||||
|
var decodeFloats = ol.parser.polyline.decodeFloats;
|
||||||
|
|
||||||
|
expect(decodeFloats(encodedFloats)).toEqual(smallFloats);
|
||||||
|
expect(decodeFloats(encodedFloats, 1e5)).toEqual(smallFloats);
|
||||||
|
expect(decodeFloats(encodedFloats, 1e2)).toEqual(floats);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
var signedIntegers = [0, 15, -1, -16, 16, 1];
|
var signedIntegers = [0, 15, -1, -16, 16, 1];
|
||||||
var encodedSignedIntegers = '?]@^_@A';
|
var encodedSignedIntegers = '?]@^_@A';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user