我正在尝试使用python GDAL/OGR模块从OSM.PBF文件中提取数据。
目前,我的代码如下所示:
import gdal, ogr
osm = ogr.Open('file.osm.pbf')
## Select multipolygon from the layer
layer = osm.GetLayer(3)
# Create list to store pubs
pubs = []
for feat in layer:
if feat.GetField('amenity') == 'pub':
pubs.append(feat)虽然这一小段代码可以很好地处理small.pbf文件(15mb)。然而,当解析大于50mb的文件时,我得到以下错误:
ERROR 1: Too many features have accumulated in points layer. Use OGR_INTERLEAVED_READING=YES MODE当我使用以下命令打开此模式:
gdal.SetConfigOption('OGR_INTERLEAVED_READING', 'YES')ogr不再返回任何功能,即使在解析小文件时也是如此。
有人知道这是怎么回事吗?
发布于 2016-02-17 16:21:31
多亏了scai的回答,我才能把它弄明白。
gdal.org/1.11/ogr/drv_osm.html中提到的交叉阅读所需的特殊阅读模式被翻译成一个可以在下面找到的python工作示例。
这是一个如何提取.osm.pbf文件中具有'amenity=pub‘标签的所有要素的示例
import gdal, ogr
gdal.SetConfigOption('OGR_INTERLEAVED_READING', 'YES')
osm = ogr.Open('file.osm.pbf')
# Grab available layers in file
nLayerCount = osm.GetLayerCount()
thereIsDataInLayer = True
pubs = []
while thereIsDataInLayer:
thereIsDataInLayer = False
# Cycle through available layers
for iLayer in xrange(nLayerCount):
lyr=osm.GetLayer(iLayer)
# Get first feature from layer
feat = lyr.GetNextFeature()
while (feat is not None):
thereIsDataInLayer = True
#Do something with feature, in this case store them in a list
if feat.GetField('amenity') == 'pub':
pubs.append(feat)
#The destroy method is necessary for interleaved reading
feat.Destroy()
feat = lyr.GetNextFeature()据我所知,需要while循环而不是for循环,因为当使用交错读取方法时,不可能获得集合的特征计数。
更多地澄清这段代码为什么会这样工作,我们将不胜感激。
https://stackoverflow.com/questions/35439205
复制相似问题