GDAL矢量驱动程序(Vector drivers)中有一种MVT: Mapbox Vector Tiles,使用该驱动器程序,即可读取pbf格式的文件。
“MVT驱动程序可以读写Mapbox Vector Tile文件,支持单独的文件,非压缩的或gzip压缩的,典型的像.pbf,.mvt,.mvt.gz或者给定zoom层级下的一个tileset。要支持对这些类型文件的写操作,则GDAL编译时需要libsqlite3和GEOS的支持。”
from osgeo import ogr
from osgeo.gdalconst import GA_ReadOnly
driver = ogr.GetDriverByName("MVT")
pbf_file = r"./data/test.pbf"
data_source = driver.Open(pbf_file, GA_ReadOnly) # get data source
layer_num = data_source.GetLayerCount()
for i in range(layer_num):
layer = data_source.GetLayer(i) # get layers in data source
layer_name = layer.GetName() # get layer name
for feature in layer: # get feature in layer
geometry = feature.GetGeometryRef() # get geometry in feature