将gtfs实时数据流化为人类可读格式

将gtfs实时数据流化为人类可读格式

本文介绍了将gtfs实时数据流化为人类可读格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java下载可读的gtfs实时数据(协议缓冲区格式),以便可以在文本文件中查看.

I'm trying to download readable gtfs real time data (protocol buffer format) using Java so I can view it in a text file.

我尝试了几种方法:

方法1:

URL url = new URL(uri);
byte[] buffer = new byte[4096];
InputStream is = url.openStream();
byte[] buffer = new byte[4096];
InputStream is = url.openStream();
File file = new File("c:/protobuf_data.txt");
OutputStream output = new FileOutputStream(file);
int numOfBytesReadIntoBuffer = -1;
while((numOfBytesReadIntoBuffer = is.read(buffer)) != -1){
    output.write(buffer, 0, numOfBytesReadIntoBuffer);
}

results (snippet):
099700_L..S20150102*LÊ>0L 1637 8AV/RPY!¯¬œ¥¾¬œ¥"L22S(

方法2(与方法1相同的结果):导入org.apache.commons.io.IOUtils;

Approach #2 (same results as approach #1):import org.apache.commons.io.IOUtils;

URL url = new URL(uri);
InputStream is = url.openStream();
File file = new File("c:/protobuf_data.txt");
OutputStream output = new FileOutputStream(file);
byte[] bytes = IOUtils.toByteArray(is);
output.write(bytes);

我猜是因为它们都以相同的方式写入OutputStream,结果是相同的.

I guess because they both write to the OutputStream the same way, the results are the same.

我也尝试了在这里找到的建议,但最终却遇到了错误:使用Google协议缓冲区传输字符串时字符,弄乱了代码

I also tried the suggestion found here but I just ended up getting errors:When using google protocol buffers to transfer String character,got messy code

我阅读了协议缓冲区文档,但感到更加困惑. https://developers.google.com/protocol-buffers/docs/encoding

I read through protocol buffer docs but I got more confused. https://developers.google.com/protocol-buffers/docs/encoding

我使用了com.sun.org.apache.xml.internal.security.utils.Base64,但出现错误.方法#3

I used com.sun.org.apache.xml.internal.security.utils.Base64 but I get an error.Approach #3

URL url = new URL(uri);
InputStream is = url.openStream();

File file = new File("c:/users/Workstation/protobuf_data_bytes.txt");

OutputStream output = new FileOutputStream(file);

byte[] bytes = IOUtils.toByteArray(is);
Init.init();
byte[] decoded_bytes = Base64.decode(bytes);

error:
Exception in thread "main" com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException: Error while decoding

我还尝试使用java.util.Base64的wrap方法创建一个InputStream来解码Base64编码的字节流,但是数据变得更加混乱了.

I also tried using java.util.Base64's wrap method to create an InputStream for decoding Base64 encoded byte stream but the data just got even more mangled.

推荐答案

正如其他提到的那样,gtfs实时文件是二进制文件,需要特殊的Protocol Buffer编译代码来解析.使用Base64解码将无济于事.

As others mentioned, the gtfs-realtime files are binary and require the special Protocol Buffer compiled code to parse. You're not going to get anywhere with Base64 decoding.

但是,如果您仅尝试将gtfs实时文件解析为人类可读的格式,我编写了一个独立工具,可将GTFS实时文件转换为JSON: https://github.com/harrytruong/gtfs_realtime_json

However, if you're ONLY trying to parse gtfs-realtime files into a human-readable format, I wrote a standalone tool that converts GTFS-realtime into JSON: https://github.com/harrytruong/gtfs_realtime_json

只需下载(无需安装),然后运行:gtfs_realtime_json <feed_url>

Just download (no install), and run: gtfs_realtime_json <feed_url>

这是示例JSON输出.

这篇关于将gtfs实时数据流化为人类可读格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:15