This library reads Mapbox Vector Tiles and allows access to the layers and features.
import {VectorTile} from '@mapbox/vector-tile';
import Protobuf from 'pbf';
const tile = new VectorTile(new Protobuf(data));
// Contains a map of all layers
tile.layers;
const landuse = tile.layers.landuse;
// Amount of features in this layer
landuse.length;
// Returns the first feature
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));
To install:
npm install @mapbox/vector-tile
An object that parses vector tile data and makes it readable.
-
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.
-
layers (Object) — an object containing parsed layers in the form of
{<name>: <layer>, ...}
, where each layer is aVectorTileLayer
object.
An object that contains the data for a single vector tile layer.
-
version (
Number
, default:1
) -
name (
String
) — layer name -
extent (
Number
, default:4096
) — tile extent size -
length (
Number
) — number of features in the layer
-
feature(i) — get a feature (
VectorTileFeature
) by the given index from the layer.
An object that contains the data for a single feature.
-
type (
Number
) — type of the feature (also seeVectorTileFeature.types
) -
extent (
Number
) — feature extent size -
id (
Number
) — feature identifier, if present -
properties (
Object
) — object literal with feature properties
-
loadGeometry() — parses feature geometry and returns an array of
Point arrays (with each point having
x
andy
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
, andz
refer to the containing tile's index.)