From 49775520c483a2ba6959d505afa297f8aa1341c9 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 5 Jan 2018 17:02:58 -0500 Subject: [PATCH] Add hack solution for #1 --- index.mjs | 23 +++++++++++++++++++++-- test.js | 16 +++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/index.mjs b/index.mjs index 18bef42..a2ebe3f 100644 --- a/index.mjs +++ b/index.mjs @@ -1,6 +1,25 @@ import { lineString, point, featureCollection } from '@turf/helpers' import * as Pbf from 'pbf' +// Hack solution +// Parsing/encoding issue with SharedStreets PBF file +// https://github.com/sharedstreets/sharedstreets-js-pbf/issues/1 +Pbf.prototype.readFields = function (readField, result, end) { + end = end || this.length + + while (this.pos < end) { + var val = this.readVarint() + var tag = val >> 3 + // var startPos = this.pos + + this.type = val & 0x7 + readField(tag, result, this) + + // if (this.pos === startPos) this.skip(val) + } + return result +} + /** * Geometry Pbf * @@ -11,7 +30,7 @@ import * as Pbf from 'pbf' * @example * var buffer = fs.readFileSync('z-x-y.geometry.pbf') * - * var collection = sharedstreetsPbf.geometry(buffer); + * var collection = sharedstreetsPbf.geometry(buffer) * collection.features[0].id // => 'NxPFkg4CrzHeFhwV7Uiq7K' */ export function geometry (buffer) { @@ -89,7 +108,7 @@ export function geometry (buffer) { * @example * var buffer = fs.readFileSync('z-x-y.intersection.pbf') * - * var collection = sharedstreetsPbf.intersection(buffer); + * var collection = sharedstreetsPbf.intersection(buffer) * collection.features[0].id // => 'NxPFkg4CrzHeFhwV7Uiq7K' */ export function intersection (buffer) { diff --git a/test.js b/test.js index c3f9c12..3097b7e 100644 --- a/test.js +++ b/test.js @@ -3,9 +3,23 @@ const path = require('path') const glob = require('glob') const write = require('write-json-file') const load = require('load-json-file') -const { test } = require('tap') +const Pbf = require('pbf') +const test = require('tape') const sharedstreetsPbf = require('./index') +test('read Mapbox Vector Tile', t => { + const buffer = fs.readFileSync(path.join(__dirname, 'test', 'in', '12-1143-1497.vector.pbf')) + new Pbf(buffer).readFields((tag, data, pbf) => {}) + t.end() +}) + +test('parsing issue using Mapbox pbf #1', t => { + const buffer = fs.readFileSync(path.join(__dirname, 'test', 'in', '11-602-769.geometry.pbf')) + new Pbf(buffer).readFields((tag, data, pbf) => {}) + // Error => Unimplemented type: 4 + t.end() +}) + test('sharedstreets-pbf -- geometry', t => { glob.sync(path.join(__dirname, 'test', 'in', '*.geometry.pbf')).forEach(filepath => { const {name, base} = path.parse(filepath)