Mapbox由於配合其矢量瓦片pbf格式文件,有突出的渲染性能,被很多公司和開發者所使用。矢量瓦片的格式有很多種,其中pbf格式是一種二進制文件,直接把數據信息壓縮位二進制了。
矢量瓦片優勢
主要優勢有
- 數據傳輸量小
相對與柵格瓦片數據量大大縮小 - 不受分辨率影響
矢量瓦片無論在地圖放大到各種級別都是清晰的,都不會出現柵格放大後顯示像元的問題。 - 動態樣式
矢量瓦片會根據用戶的定製的樣式動態繪製,也可以動態改變 - 支持前端查詢
由於使用的是矢量瓦片,可以在前端直接進行信息查詢
矢量瓦片的問題
矢量瓦片的優勢雖然多,但問題也很明顯,座標和屬性信息是可以被用戶解析的,也就是安全性的問題。
Mapbox的pbf瓦片解析
今天就展示一下,如何解析Mapbox的pbf瓦片文件。這裏我是用node.js進行開發和測試的。從Mabox地圖的url中抓取了一個pbf文件。
依賴
{
"name": "testpbf",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"fs": "^0.0.1-security",
"pbf": "^3.0.5",
"vector-tile": "^1.3.0"
}
}
js解析代碼
var Pbf = require('pbf');
var fs = require('fs');
var VectorTile = require('vector-tile').VectorTile;
var data = fs.readFileSync('response.pbf');
var tile = new VectorTile(new Pbf(data));
var layerNames = Object.keys(tile.layers);
console.log("圖層名稱", layerNames);
if (layerNames.length > 0) {
//獲取其中一個圖層的一個要素的geoJson格式;
var orig = tile.layers[layerNames[0]].feature(0).toGeoJSON(0, 0, 1);
console.log("geojson", orig);
//查看座標
console.log("部分座標",orig.geometry.coordinates[0]);
}
運行代碼
node index.js
輸出結果
"C:\Program Files\nodejs\node.exe" index.js
圖層名稱 [ 'dt_xzy' ]
geojson {
type: 'Feature',
geometry: { type: 'MultiPolygon', coordinates: [ [Array], [Array], [Array] ] },
properties: {
objectid: 5050,
qhdm: '411525112',
qhmc: '陳集鄉',
sjxzqdm: '411525',
bz: ' ',
provincecode: '41',
citycode: '4115',
countycode: '411525',
uuid: ' ',
mj: 148351.45
}
}
部分座標 [
[
[ -110.6103515625, 85.34532513469131 ],
[ -110.8740234375, 85.19679703289734 ],
[ -109.775390625, 85.15985908749309 ],
[ -109.3798828125, 85.05112877980659 ],
..........................................
[ -149.58984375, 84.67351256610525 ],
[ -148.53515625, 84.68980559853495 ],
[ -148.447265625, 84.79051706762746 ],
[ -151.0400390625, 85.01689202162626 ],
[ -151.0400390625, 85.18205572307536 ],
[ -154.3359375, 85.34532513469131 ],
[ -110.6103515625, 85.34532513469131 ]
]
]
Process finished with exit code 0
矢量瓦片使用注意
從上面的代碼可以看出pbf文件是可以被解析出來的,所以在使用時一定要注意
- 數據脫敏
- 座標數據脫密
- 防爬蟲抓取