python 读取osm.pbf数据,并以txt存储每个feature

from osgeo import gdal, ogr
 
gdal.SetConfigOption('OGR_INTERLEAVED_READING', 'YES')
osm = ogr.Open('indonesia-latest.osm.pbf')
# Grab available layers in file
nLayerCount = osm.GetLayerCount()
print('count', nLayerCount)
thereIsDataInLayer = True
flag = True
while thereIsDataInLayer:
  thereIsDataInLayer = False
  # Cycle through available layers
  flag = True
  for iLayer in range(nLayerCount):
    print(iLayer)
    lyr=osm.GetLayer(iLayer)
    # Get first feature from layer
    print(lyr.GetName())
    # print(lyr.GetDescription())
    # print(lyr.GetSpatialRef())
    # print(lyr.GetStyleTable())
    # print(lyr.GetGeomType())
    # print(lyr.GetMetadata())
    # print(lyr.GetGeometryColumn())
    feat = lyr.GetNextFeature()
    # for k, v in enumerate(lyr):
    #   print("Key: {0}, Value: {1}".format(k, v))
    with open('ID_level_filter_level5_' + str(iLayer) + '.txt', 'a') as f:
      while (feat is not None):
        thereIsDataInLayer = True
        # print(feat.__dict__)
        #Do something with feature, in this case store them in a list
        # if feat.GetField('amenity') == 'pub':
        if (flag):
          # print(dir(feat))
          # print(feat.GetFID())
          # print(feat.GetGeomFieldCount())
          # print(feat.GetNativeData())
          # print(feat.GetNativeMediaType())
          # print(feat.GetDefnRef())
          print(feat.GetField('name'))
          jsonObj = feat.ExportToJson()
          if iLayer == 3:
            print(feat.GetField('boundary'))
            print(feat.GetField('admin_level'))
          print(jsonObj)
          flag = False
        # print(feat.GetGeometryRef())
        if iLayer == 3 and feat.GetField('name') and feat.GetField('admin_level') and feat.GetField('boundary') == 'administrative' and feat.GetField('admin_level') == '5':
          f.write(feat.ExportToJson() + '\n')
        #The destroy method is necessary for interleaved reading
        feat.Destroy()
        feat = lyr.GetNextFeature()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风xs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值