Skip to content

Commit

Permalink
bugfix for accumulating rounding errors encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
MaRaSu committed Nov 7, 2019
1 parent 68e7819 commit b23d198
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Polyline.encoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

for (var i = 0, len = numbers.length; i < len;) {
for (var d = 0; d < options.dimension; ++d, ++i) {
var num = numbers[i];
var num = numbers[i].toFixed(options.precision);
var delta = num - (lastNumbers[d] || 0);
lastNumbers[d] = num;

Expand Down
17 changes: 16 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require('chai-leaflet')
chai.should();

describe('PolyUtil', function () {
var latlngs, encoded, encoded6, encoded5, delta;
var latlngs, latlngs6, encoded, encoded6, encoded5, delta;

var floats, smallFloats, encodedFloats;
var signedIntegers, encodedSignedIntegers;
Expand All @@ -23,6 +23,16 @@ describe('PolyUtil', function () {
[43.252, -126.453]
];

latlngs6 = [
[0.000000, 0.000000],
[0.000006, 0.000006],
[0.000012, 0.000012],
[0.000018, 0.000018],
[0.000024, 0.000024],
[0.000030, 0.000030],
[0.000000, 0.000000]
];

encoded = '_p~iF~cn~U_ulLn{vA_mqNvxq`@';
encoded5 = 'ehukEveaqO|D~Laf@YiZw[oOgQqGeIqE_GeXu_@aOiKkCkMbGrDxIzF`Ct@~@L`FYhAMrNoFfEoArIwBvGwBvB}@jHcBnFcBz@g@jMsDzJ_DfEoAf@SRjCrDbQbBg@vB{@jCoArDg@bBfE|CvKtIbWrDrNvBnAvGfEvBjGqNzNcMtDgN`I_LpM';
encoded6 = '_izlhA~pvydF_{geC~{mZ_kwzCn`{nI';
Expand Down Expand Up @@ -113,6 +123,11 @@ describe('PolyUtil', function () {
polyUtil.decode(encoded).should.eql(latlngs);
});

it('with precision 6 latlngs to catch accumulating rounding errors', function () {
var encoded = polyUtil.encode(latlngs6);
polyUtil.decode(encoded).pop().should.eql(latlngs6.pop());
});

it('latlngs height', function () {
var xyz = [
[38.5, -120.5, 3],
Expand Down

0 comments on commit b23d198

Please sign in to comment.