diff --git a/src/protobuf.js b/src/protobuf.js index d4e1ab8..d9bd8ba 100644 --- a/src/protobuf.js +++ b/src/protobuf.js @@ -6,19 +6,18 @@ export function readVarInt64 (ta_struct) { let cursor = ta_struct.cursor let nVal = 0 - let nShift = 0 + let multiplier = 1 let nByte while (true) { nByte = ta_struct.buffer[cursor] + cursor++ + nVal += (nByte & 0x7f) * multiplier if ((nByte & 0x80) === 0) { - cursor++ ta_struct.cursor = cursor - return nVal | (nByte << nShift) + return nVal } - nVal = nVal | (nByte & 0x7f) << nShift - cursor++ - nShift += 7 + multiplier *= 128 } } @@ -28,7 +27,7 @@ export function readVarSInt64 (ta_struct) { } export function unzigzag (nVal) { - if ((nVal & 1) === 0) - return nVal >> 1 - return -(nVal >> 1) - 1 -} \ No newline at end of file + if ((nVal % 2) === 0) + return nVal / 2 + return -((nVal + 1) / 2) +} diff --git a/test/twkb.spec.js b/test/twkb.spec.js index b8b881c..c619b18 100644 --- a/test/twkb.spec.js +++ b/test/twkb.spec.js @@ -23,6 +23,23 @@ describe('twkb', function () { }) }) + it('should decode precision=7 point in east Asia longitude range', function () { + // POINT(141.961144 35.681234) with precision 7 + var g = twkb.toGeoJSON(Buffer.from('e100e0b4ecc90ae898a4d402', 'hex')) + assert.deepEqual(g, { + type: 'FeatureCollection', + features: [ + { + type: 'Feature', + geometry: { + type: 'Point', + coordinates: [141.961144, 35.681234] + } + } + ] + }) + }) + it('should decode linestring to geojson', function () { var g = twkb.toGeoJSON(Buffer.from('02000202020808', 'hex')) assert.deepEqual(g, {