程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python reads osm PBF data and store each feature in TXT

編輯:Python
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()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved