Abstract
バイナリベクタータイルの実体は、XYZタイルの位置ごとにサーバーに設置されたPBFファイル群です。PBFファイルはプロトコルバッファと呼ばれる規格で(その詳細は把握していませんが)、GeoJSONをシリアライズしたバイナリデータです。つまり元はGeoJSONなので復元(デコード)可能という事です。今回は2つの手法でGeoJSONへのデコードを試みたいと思います。
tippecanoe-decode
mapbox社謹製のtippecanoeと言えばバイナリベクタータイルの生成で用いられる有名なライブラリですが、付随して、バイナリベクタータイルからGeoJSONへのデコード機能を持っています。
Install
brew install tippecanoe
Usage
tippecanoe-decode <target_pbf> <zoomlevel> <x> <y>
これが基本形で、特定のsource-layerのみを抽出する事も出来ます。
具体例
tippecanoe-decode sample.pbf 13 7121 3260 --layer=road
番外編
ベクタータイルをディレクトリで用意出来ている場合、ディレクトリを指定するだけでデコード可能です(この場合、minzoomとmaxzoomを指定してあげた方が安心な挙動をしそう)。しかし出力が少し使いにくいので参考まで(多分PBFファイルを個別に指定するよりこちらの方が速い)。
tippecanoe-decode bvtiles_dir -z 13 -Z 13 --layer=building
Node.js(vector-tile-js)
こちらもmapbox社製のnpmパッケージです。
Install
npm install @mapbox/vector-tile
npm install pbf
npm install fs
Usage
サンプルコードを示します。
sample.js
varPbf=require('pbf')varVectorTile=require('@mapbox/vector-tile')varfs=require('fs')letpbfdata=fs.readFileSync(PBFFILE_PATH)letpbf=newPbf(pbfdata)constlayer=newVectorTile.VectorTile(pbf).layers[SOURCE_LAYER_NAME];if(layer){for(leti=0;i<layer.length;i++){constfeature=layer.feature(i).toGeoJSON(X,Y,ZOOM_LEVEL);console.log(feature)//GeoJSON形式のFeature}}