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()


Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐