[PYTHON] Two Ways to Decode Binary Vector Tiles to GeoJSON

Abstract The entity of the binary vector tile is a group of PBF files installed on the server for each position of the XYZ tile. PBF files are a standard called Protocol Buffers (I don't know the details), and they are binary data serialized from GeoJSON. In other words, since the original is GeoJSON, it can be restored (decoded). This time, I would like to try decoding to GeoJSON using two methods.

tippecanoe-decode Speaking of tippecanoe made by mapbox, it is a famous library used for generating binary vector tiles, but it also has a decoding function from binary vector tiles to GeoJSON.

Install

brew install tippecanoe

Usage

tippecanoe-decode <target_pbf> <zoomlevel> <x> <y>

This is the basic form, and it is possible to extract only a specific source-layer.

Concrete example


tippecanoe-decode sample.pbf 13 7121 3260 --layer=road

Extra edition

If the vector tile is prepared in a directory, it can be decoded just by specifying the directory (in this case, it seems safer to specify minzoom and maxzoom). However, the output is a bit tricky to use, so for reference (probably faster than specifying the PBF files individually).

tippecanoe-decode bvtiles_dir -z 13 -Z 13 --layer=building

Node.js(vector-tile-js) This is also a mapbox npm package.

Install

npm install @mapbox/vector-tile
npm install pbf
npm install fs

Usage Here is the sample code.

sample.js


var Pbf = require('pbf')
var VectorTile = require('@mapbox/vector-tile')
var fs = require('fs')

let pbfdata = fs.readFileSync(PBFFILE_PATH)
let pbf = new Pbf(pbfdata)
const layer = new VectorTile.VectorTile(pbf).layers[SOURCE_LAYER_NAME];
if (layer) {
    for (let i = 0; i < layer.length; i++) {
        const feature = layer.feature(i).toGeoJSON(X, Y, ZOOM_LEVEL);
        console.log(feature) //GeoJSON format Feature
    }
}

References

Recommended Posts

Two Ways to Decode Binary Vector Tiles to GeoJSON
Two Ways to Make Ansible Portable
API implementation to toggle values in two ways (go)