java osm pbf_read format .osm.pbf from geofabrik

#--------------------------20200227更新----------------------------------------

德国人第二天邮件就反馈了。确实严谨,也学到了。

1 osm 全部坐标信息只在points里, 后面的way 和relation 只有点id的索引。

他给出的读取方法是:

l0 =osm.GetLayer(0)

f0=None

fcount=0while l0.GetNextFeature() is notNone:

fcount+= 1

必须顺次读取,先读完全部的points再读后面的

2 pbftoosm 不维护了。

Pbftoosm is not maintained any more. Please use Osmconvert, its direct

replacement, or Osmium instead.

osmium cat -o nk.osm north-korea-latest.osm.pbf

3 不兼容报错,主要是因为删除了上传者个人信息。 他们验证过的工具可以处理这个问题。

Our files follow the standard with the exception thatpersonal metadata

fields are set to 0 or empty strings to protect the privacy of

OpenStreetMap contributors. Our tests pointed out that this is not an

issue for established tools such as Osmium, Osmosis and Osmconvert.

4 处理是基于导入pg的

We do not plan to support pbftoosm in future because its development

stopped nine years ago. We do not use GDAL to read PBF files ourselves

because it is not performant. Instead, we import them into a PostgreSQL

database and run

毕竟咱是外行,还是学到很多啊

#----------------------------------------------------------------------------

项目需要分析过滤osm上的数据,  在

可以下载到按大洲,和国别 的 osm数据,    所有国家提供  .osm.pbf格式 和 raw osm压缩 .osm.bz2  格式,  数据少的国家 提供 . shp 格式的.

对shp的处理当然没问题. 但在数据大的国家动辄几G, pbf体积最小, 就下载了.但是读取可坑死我了.  前后折腾了N天.终于明白了.

一.症状

直接说结论:这个网站的pbf格式, 不是标准的pbf!  表现症状有2:

1.1 不被 gdal ogr 库支持

在py下

importgdal, ogr

gdal.SetConfigOption('OGR_INTERLEAVED_READING', 'YES')#gdal.SetConfigOption('OSM_MAX_TMPFILE_SIZE', '2000')

if __name__ == '__main__':

fname = './data/osm_pbf/north-korea-latest.osm.pbf'

osm =ogr.Open(fname)print(osm.GetLayerCount())

layer=osm.GetLayer(0)#layer = data.GetLayer('lines')

print(layer.GetFeatureCount())

feature=layer.GetNextFeature()

o1=feature.ExportToJson(as_object=True)print(o1)

1 所有的对 layer 0 也就是points  是可以运行的,但是 lines multipolygons 一个features都读不出来, 只返回None

layer.GetFeatureCount()

可以读出数量

在ubuntu下, 自己辛苦源码编译的gdal下,  全是-1

但是, gdal 测试里自带的pbf     GDAL/autotest/ogr/data/test.pbf

是没任何问题的  !

1.2 不被openstreetmap官网的pbf转换工具支持

官网提供了pbf2osm 直接一个可执行文件 比如x64 linux 直接下载一个pbftoosm64  放到bin 之类的路径下

对gdal自带的pbf是可以正常转型的 转成osm的 注意尖括号不能省略

pbftoosm64 test.osm

但是转他家下载的.osm.pbf就报错

➜ osm_pbf git:(master) ✗ pbftoosm64 nk.osm

pbftoosm Warning: header block element type unknown: 0x80 0x02.

pbftoosm Warning: header block element type unknown: 0xA6 0xC8.

pbftoosm: Format error: 0xA6.

pbftoosm: Number of bytes read: 24

这几天,为了编译gdal和他的依赖库, 兜了个大圈子,坑死我了.

2 解决

但是,这毕竟是个天天更新全球数据的网站, 德国人开的网站,总不至于发布完全无法读取的数据呀.

Commonly Used Formats

asia-latest.osm.pbf, suitable for Osmium, Osmosis, imposm, osm2pgsql, mkgmap, and others. This file was last modified 6 hours ago and contains all OSM data up to 2020-02-25T21:59:02Z. File size: 7.7 GB; MD5 sum:

sudo apt-get install build-essential cmake libboost-dev \

libexpat1-dev zlib1g-dev libbz2-dev

pip3.8 install osmium

写个新的py脚本read_osm_pbf.py

'''https://download.geofabrik.de/asia.html

asia-latest.osm.pbf, suitable for Osmium, Osmosis, imposm, osm2pgsql, mkgmap, and others.'''

importosmiumclassCounterHandler(osmium.SimpleHandler):def __init__(self):

osmium.SimpleHandler.__init__(self)

self.num_nodes=0defnode(self, n):

self.num_nodes+= 1

if __name__ == '__main__':

fname= './data/osm_pbf/north-korea-latest.osm.pbf'h=CounterHandler()

h.apply_file(fname)print("Number of nodes: %d" % h.num_nodes)

运行一下:

python3.8 read_osm_pbf.py

6385b05b65a1aa3c15ce0e80564b3fad.png

果然能读, 没报错

下面继续研究具体怎么读坐标,怎么关联shapely

有时间真想问问德佬, 你们这pbf和osm官网的pbf到底啥关系?!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值