Mapbox Vector Tile Protobuf Serializer

3.1.3 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to use `vt-pbf` to serialize a GeoJSON object (processed by `geojson-vt`) into a binary Mapbox Vector Tile Protobuf (.pbf) file, including recent `null` value handling.

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);

view raw JSON →