使用正确的.proto文件时,无法解析python中的协议缓冲区文件 [英] Cannot parse a protocol buffers file in python when using the correct .proto file

查看:282
本文介绍了使用正确的.proto文件时,无法解析python中的协议缓冲区文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(请参阅底部的更新)

瓷砖制造商 OpenStreetMap 程序以生成 Mapbox矢量图块(本身是OSM pbf数据文件中的协议缓冲区(pbf)文件).我已经编译了它,并用它来创建矢量图块的目录.我无法在Python中解析这些文件.

Tilemaker is an OpenStreetMap programme to generate Mapbox vector tiles (which are themselves protocol buffers (pbf) files) from an OSM pbf data file. I have compiled it and used it to create a directory of vector tiles. I cannot parse those files in Python.

我创建了带有以下内容的矢量图块:

I created the vector tiles with:

tilemaker input.pbf --output=tiles/

然后我基于 Google的协议创建了一个简单的python程序以这种方式缓冲Python教程:

编译.proto文件:

mkdir py
touch py/__init__.py
protoc --proto_path=include --python_out=./py ./include/osmformat.proto
protoc --proto_path=include --python_out=./py ./include/vector_tile.proto

此python程序pyread.py不起作用:

This python programme pyread.py doesn't work:

import sys
import py.vector_tile_pb2

with open(sys.argv[1]) as fp:
    pbf_file_contents = fp.read()

tile = py.vector_tile_pb2.Tile()
tile.ParseFromString(pbf_file_contents)

这是尝试运行它时的错误:

This is the error when trying to run it:

$ python pyread.py ./tiles/13/3932/2588.pbf
Traceback (most recent call last):
  File "pyread.py", line 8, in <module>
    tile.ParseFromString(pbf_file_contents)
  File "/home/rory/.local/lib/python2.7/site-packages/google/protobuf/message.py", line 186, in ParseFromString
    self.MergeFromString(serialized)
  File "/home/rory/.local/lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 841, in MergeFromString
    if self._InternalParse(serialized, 0, length) != length:
  File "/home/rory/.local/lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 866, in InternalParse
    new_pos = local_SkipField(buffer, new_pos, end, tag_bytes)
  File "/home/rory/.local/lib/python2.7/site-packages/google/protobuf/internal/decoder.py", line 827, in SkipField
    return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)
  File "/home/rory/.local/lib/python2.7/site-packages/google/protobuf/internal/decoder.py", line 797, in _RaiseInvalidWireType
    raise _DecodeError('Tag had invalid wire type.')
google.protobuf.message.DecodeError: Tag had invalid wire type.

protoc命令来自protcol缓冲区库.我从Google的页面(链接到Github)下载了最新版本(2.6.1),并编译了&安装了它.该协议调用就像 Tilemaker Makefile所做的一样.

The protoc command is from the protcol buffers library. I downloaded the latest release (2.6.1) from Google's page (which links to Github) and compiled & installed it. That protoc invocation is just like what the Tilemaker Makefile does.

这是怎么回事?如何在python中读取此协议缓冲区文件?

What's going on? How can I read this protocol buffers file in python?

更新进一步的调查使我认为我的一个假设可能是错误的.即,tilemaker命令已产生有效的protobuf文件.我从Mapzen那里得到了一些矢量图块,该图块应具有相同的格式和非常相似的数据. 但是这种格式适用于python pyread.py命令以及protoc --decode_rawprotoc --decode=vector_tile.Tile ./include/vector_tile.proto.因此,我认为问题出在我正在查看的文件上.

UPDATE Further investigation makes me think that one of my assumptions might be wrong. Namely, that the tilemaker command has produced a valid protobuf file. I got some vector tiles from Mapzen, which should have the same format and very similar data. But this format works with the python pyread.py command, and with protoc --decode_raw and protoc --decode=vector_tile.Tile ./include/vector_tile.proto. Hence I think the problem is with the file I was looking at.

推荐答案

我认为问题在于OpenStreetMap的.pbf格式不是是原始protobuf.查看我对您其他问题的回答:

I think the problem is that OpenStreetMap's .pbf format is not a raw protobuf. See my answer to your other question:

https://stackoverflow.com/a/35384238/2686899

这篇关于使用正确的.proto文件时,无法解析python中的协议缓冲区文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆