What is @mapbox/vector-tile?
@mapbox/vector-tile is a library for working with vector tiles, which are a way to deliver geographic data in small chunks. This package allows you to parse and work with vector tiles, which are commonly used in mapping applications to render map data efficiently.
What are @mapbox/vector-tile's main functionalities?
Parsing Vector Tiles
This code demonstrates how to parse a vector tile file and convert its features to GeoJSON format. It reads a vector tile from the file system, parses it using the VectorTile class, and iterates over its layers and features.
const fs = require('fs');
const { VectorTile } = require('@mapbox/vector-tile');
const Protobuf = require('pbf');
const data = fs.readFileSync('path/to/vector-tile.mvt');
const tile = new VectorTile(new Protobuf(data));
for (const layerName in tile.layers) {
const layer = tile.layers[layerName];
console.log(`Layer: ${layerName}`);
for (let i = 0; i < layer.length; i++) {
const feature = layer.feature(i);
console.log(feature.toGeoJSON(0, 0, 0));
}
}
Accessing Feature Properties
This code shows how to access the properties of a specific feature within a vector tile. It reads the vector tile, parses it, and then retrieves the properties of the first feature in a specified layer.
const fs = require('fs');
const { VectorTile } = require('@mapbox/vector-tile');
const Protobuf = require('pbf');
const data = fs.readFileSync('path/to/vector-tile.mvt');
const tile = new VectorTile(new Protobuf(data));
const layer = tile.layers['layer-name'];
const feature = layer.feature(0);
console.log(feature.properties);
Converting Features to GeoJSON
This code demonstrates how to convert a feature from a vector tile to GeoJSON format. It reads and parses the vector tile, retrieves a feature from a specified layer, and converts it to GeoJSON.
const fs = require('fs');
const { VectorTile } = require('@mapbox/vector-tile');
const Protobuf = require('pbf');
const data = fs.readFileSync('path/to/vector-tile.mvt');
const tile = new VectorTile(new Protobuf(data));
const layer = tile.layers['layer-name'];
const feature = layer.feature(0);
const geojson = feature.toGeoJSON(0, 0, 0);
console.log(JSON.stringify(geojson, null, 2));
Other packages similar to @mapbox/vector-tile
geojson-vt
geojson-vt is a library for slicing GeoJSON data into vector tiles on the fly in the browser or on the server. While it focuses more on creating vector tiles from GeoJSON data rather than parsing existing vector tiles, it can be used in conjunction with @mapbox/vector-tile for a complete solution for working with vector tiles.
tilelive
tilelive is a library for working with tiles, including vector tiles, in Node.js. It provides a range of tools for reading, writing, and transforming tiles. While it offers broader functionality than @mapbox/vector-tile, it can be more complex to use for simple vector tile parsing tasks.
vector-tile
This library reads Mapbox Vector Tiles and allows access to the layers and features.
Example
import {VectorTile} from '@mapbox/vector-tile';
import Protobuf from 'pbf';
const tile = new VectorTile(new Protobuf(data));
tile.layers;
const landuse = tile.layers.landuse;
landuse.length;
landuse.feature(0);
Vector tiles contained in serialtiles-spec
are gzip-encoded, so a complete example of parsing them with the native
zlib module would be:
import {VectorTile} from '@mapbox/vector-tile';
import Protobuf from 'pbf';
import {gunzipSync} from 'zlib';
const buffer = gunzipSync(data);
const tile = new VectorTile(new Protobuf(buffer));
Install
To install:
npm install @mapbox/vector-tile
API Reference
VectorTile
An object that parses vector tile data and makes it readable.
Constructor
- new VectorTile(protobuf[, end]) —
parses the vector tile data contained in the given Protobuf object,
saving resulting layers in the created object as a
layers
property. Optionally accepts end index.
Properties
- layers (Object) — an object containing parsed layers in the form of
{<name>: <layer>, ...}
,
where each layer is a VectorTileLayer
object.
VectorTileLayer
An object that contains the data for a single vector tile layer.
Properties
- version (
Number
, default: 1
) - name (
String
) — layer name - extent (
Number
, default: 4096
) — tile extent size - length (
Number
) — number of features in the layer
Methods
- feature(i) — get a feature (
VectorTileFeature
) by the given index from the layer.
VectorTileFeature
An object that contains the data for a single feature.
Properties
- type (
Number
) — type of the feature (also see VectorTileFeature.types
) - extent (
Number
) — feature extent size - id (
Number
) — feature identifier, if present - properties (
Object
) — object literal with feature properties
Methods
- loadGeometry() — parses feature geometry and returns an array of
Point arrays (with each point having
x
and y
properties) - bbox() — calculates and returns the bounding box of the feature in the form
[x1, y1, x2, y2]
- toGeoJSON(x, y, z) — returns a GeoJSON representation of the feature. (
x
, y
, and z
refer to the containing tile's index.)