Mapbox Vector Tile Protobuf Serializer
vt-pbf is a JavaScript library for serializing Mapbox vector tiles into binary Protobuf format, adhering to the Mapbox Vector Tile Specification. It provides methods to convert both `vector-tile-js` objects and `geojson-vt` tile structures into a Protobuf buffer. The current stable version is 3.1.3. Releases appear to be driven by bug fixes and performance improvements, with a focus on specification compliance and encoding correctness for various GeoJSON types and property values, as seen with recent fixes for `null` value encoding. Its primary differentiator is its direct integration with Mapbox's ecosystem libraries like `geojson-vt` and `@mapbox/vector-tile` for efficient tile generation and manipulation, rather than being a generic Protobuf library.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'features')
cause Attempting to pass a raw GeoJSON object directly to `vtpbf` or `vtpbf.fromGeojsonVt` without first processing it through `geojson-vt` or `vector-tile-js`.fixUse `geojsonVt(yourGeoJson).getTile(z, x, y)` to obtain a tile object, then pass that to `vtpbf.fromGeojsonVt`. -
Error: Unknown geometry type: X
cause The input tile object contains a GeoJSON geometry type that is not supported by the Mapbox Vector Tile specification or `vt-pbf`'s encoder.fixEnsure your GeoJSON data only contains Point, LineString, and Polygon geometry types. Multi-part geometries are generally supported but complex or non-standard types will fail.
Warnings
- breaking The encoding of `null` property values was fixed in v3.1.2 and further refined in v3.1.3 to strictly conform to the Mapbox Vector Tile specification. Older versions might have encoded `null` values incorrectly, leading to non-compliant or incompatible PBFs.
- gotcha The library expects specific input formats depending on the source: either a `VectorTile` instance from `@mapbox/vector-tile` or a tile object generated by `geojson-vt`. Directly passing raw GeoJSON or other arbitrary structures will result in errors.
- gotcha When using `vtpbf.fromGeojsonVt`, the `options` argument allows specifying `version` and `extent`. Failing to set these correctly might lead to PBFs that do not match the intended Mapbox Vector Tile specification version or coordinate system extent, causing rendering issues in clients.
Install
-
npm install vt-pbf -
yarn add vt-pbf -
pnpm add vt-pbf
Imports
- vtpbf
const vtpbf = require('vt-pbf');import vtpbf from 'vt-pbf';
- fromGeojsonVt
import { fromGeojsonVt } from 'vt-pbf';import vtpbf from 'vt-pbf'; const buff = vtpbf.fromGeojsonVt({ 'layer': tile }); - vtpbf as vtSerializer
import vtSerializer from 'vt-pbf';
Quickstart
import vtpbf from 'vt-pbf';
import geojsonVt from 'geojson-vt';
import fs from 'node:fs';
const sampleGeoJSON = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": { "name": "Example Polygon", "value": null },
"geometry": {
"type": "Polygon",
"coordinates": [[[-10, -10], [10, -10], [10, 10], [-10, 10], [-10, -10]]]
}
}
]
};
async function serializeGeoJsonToPbf() {
// Generate a tile index from GeoJSON
const tileindex = geojsonVt(sampleGeoJSON, { maxZoom: 14 });
// Get a specific tile (e.g., zoom 0, x 0, y 0)
const tile = tileindex.getTile(0, 0, 0);
if (tile) {
// Serialize the geojson-vt tile object into a PBF buffer
const buffer = vtpbf.fromGeojsonVt({ 'myLayer': tile }, { version: 2, extent: 4096 });
// Write the buffer to a file
const filePath = './my-serialized-tile.pbf';
fs.writeFileSync(filePath, buffer);
console.log(`Successfully wrote PBF tile to ${filePath}. File size: ${buffer.byteLength} bytes`);
} else {
console.log('No tile found for the specified coordinates.');
}
}
serializeGeoJsonToPbf().catch(console.error);