Skip to content

Commit

Permalink
Add hack solution for #1
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Jan 5, 2018
1 parent b578b88 commit 4977552
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
23 changes: 21 additions & 2 deletions 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
*
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 15 additions & 1 deletion test.js
Expand Up @@ -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)
Expand Down

0 comments on commit 4977552

Please sign in to comment.