@mapwhit/vt-pbf
v2.0.0
Published
Serialize mapbox vector tiles to binary protobufs in javascript.
Readme
@maphit/vt-pbf
Fork of vt-pbf
Serialize Mapbox vector tiles to binary protobufs in javascript.
Usage
As far as I know, the two places you might get a JS representation of a vector tile are geojson-vt and vector-tile-js. These both use slightly different internal representations, so serializing each looks slightly different:
From vector-tile-js
import vtpbf from 'vt-pbf';
import { VectorTile } from '@mapwhit/vector-tile';
import Protobuf from 'pbf';
const data = fs.readFileSync(import.meta.dirname + '/fixtures/rectangle-1.0.0.pbf')
const tile = new VectorTile(new Protobuf(data))
const orig = tile.layers['geojsonLayer'].feature(0).toGeoJSON(0, 0, 1)
const buff = vtpbf(tile)
fs.writeFileSync('my-tile.pbf', buff)From geojson-vt
import * as vtpbf from 'vt-pbf';
import geojsonVt from 'geojson-vt';
const orig = JSON.parse(fs.readFileSync(import.meta.dirname + '/fixtures/rectangle.geojson'))
const tileindex = geojsonVt(orig)
const tile = tileindex.getTile(1, 0, 0)
// pass in an object mapping layername -> tile object
const buff = vtpbf.fromGeojsonVt({ 'geojsonLayer': tile })
fs.writeFileSync('my-tile.pbf', buff)vtpbf.fromGeojsonVt takes two arguments:
layerMapis an object where keys are layer names and values are a geojson-vt tile,optionsis an object (optional argument). There are 2 supported keys:versionto define the version of the mvt spec used andextentto define the extent of the tile.versiondefaults to 1 andextentto 4096.
