gpt4 book ai didi

Java、GTFS Realtime、protocol buffers,简单搞定?

转载 作者:行者123 更新时间:2023-12-02 04:33:27 25 4
gpt4 key购买 nike

我正在使用这个东西: https://github.com/MobilityData/gtfs-realtime-bindings/tree/final-google-version/java

我可以按照给出的代码示例进行操作。但这只需要我到目前为止,我不知道如何提取更细粒度的信息。我想知道如何简单地提取某个行程的延迟或 stop_id。谁能帮我吗?

最佳答案

下面的java程序:

package com.google.transit.realtime;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Date;

import com.google.transit.realtime.GtfsRealtime.FeedEntity;
import com.google.transit.realtime.GtfsRealtime.FeedHeader;
import com.google.transit.realtime.GtfsRealtime.FeedMessage;
import com.google.transit.realtime.GtfsRealtime.TripDescriptor;
import com.google.transit.realtime.GtfsRealtime.TripUpdate;
import com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate;

public class GtfsRealtimeExample {
public static void main(String[] args) throws Exception {

URL url;
String outputFile;
if (args[0].startsWith("http")) {
url = new URL(args[0]);
outputFile = args[0].substring(args[0].lastIndexOf('/')+1).replaceFirst(".pb", ".txt");
}
else {
url = new File(args[0]).toURI().toURL();
outputFile = args[0].replaceFirst(".pb", ".txt");
}

FeedMessage feed = FeedMessage.parseFrom(url.openStream());
FeedHeader header = feed.getHeader();

OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(outputFile));
os.write(String.format("GTFS Realtime Version %s feed produced on %s with %d entities\n",
header.getGtfsRealtimeVersion(), new Date(header.getTimestamp()*1000).toString(), feed.getEntityCount()));

for (FeedEntity entity : feed.getEntityList()) {

if (entity.hasTripUpdate()) {
TripUpdate tripUpdate = entity.getTripUpdate();

int noStopTimeUpdates = tripUpdate.getStopTimeUpdateCount();
int tripDelay = tripUpdate.getDelay();
long tripTimestamp = tripUpdate.getTimestamp();

TripDescriptor tripDescriptor = tripUpdate.getTrip();
os.write(String.format("\nTRIP id=%s route=%s direction=%d, sch=%s, date=%s, time=%s, noStopUpdates=%d tripDelay=%d timestamp=%d\n",
tripDescriptor.getTripId(), tripDescriptor.getRouteId(), tripDescriptor.getDirectionId(),
tripDescriptor.getScheduleRelationship().getValueDescriptor().toString(), tripDescriptor.getStartDate(),
tripDescriptor.getStartTime(), noStopTimeUpdates, tripDelay, tripTimestamp));

for (StopTimeUpdate stoptimeUpdate : tripUpdate.getStopTimeUpdateList()) {
os.write(String.format(" STOP id=%s seq=%d sch=%s arrTime=%s arrDelay=%d depTime=%s depDelay=%d\n",
stoptimeUpdate.getStopId(), stoptimeUpdate.getStopSequence(),
stoptimeUpdate.getScheduleRelationship().getValueDescriptor().toString(),
new Date(stoptimeUpdate.getArrival().getTime()*1000).toString(), stoptimeUpdate.getArrival().getDelay(),
new Date(stoptimeUpdate.getDeparture().getTime()*1000).toString(), stoptimeUpdate.getDeparture().getDelay()));
}
}
}

os.close();
}
}

当使用参数运行时 http://api.nextlift.ca/gtfs-realtime/tripupdates.pb 将生成一个输出文件tripupdates.txt,其中包含:

GTFS Realtime Version 1.0 feed produced on Wed Jun 12 15:50:33 IST 2019 with 118 entities

TRIP id=10559:203976 route=1 direction=0, sch=SCHEDULED, date=20190612, time=, noStopUpdates=54 tripDelay=0 timestamp=0 STOP id=1150 seq=1 sch=SCHEDULED arrTime=Thu Jan 01 01:00:00 GMT 1970 arrDelay=0 depTime=Wed Jun 12 16:07:00 IST 2019 depDelay=0
STOP id=1152 seq=2 sch=SCHEDULED arrTime=Wed Jun 12 16:07:00 IST 2019 arrDelay=0 depTime=Wed Jun 12 16:07:00 IST 2019 depDelay=0
STOP id=1153 seq=3 sch=SCHEDULED arrTime=Wed Jun 12 16:08:00 IST 2019 arrDelay=0 depTime=Wed Jun 12 16:08:00 IST 2019 depDelay=0
...
...
STOP id=1005 seq=53 sch=SCHEDULED arrTime=Wed Jun 12 16:44:00 IST 2019 arrDelay=0 depTime=Wed Jun 12 16:44:00 IST 2019 depDelay=0 STOP id=1006 seq=54 sch=SCHEDULED arrTime=Wed Jun 12 16:47:00 IST 2019 arrDelay=0 depTime=Wed Jun 12 16:47:00 IST 2019 depDelay=0

TRIP id=10558:203580 route=1 direction=0, sch=SCHEDULED, date=20190612, time=, noStopUpdates=69 tripDelay=0 timestamp=0
STOP id=1177 seq=24 sch=SCHEDULED arrTime=Wed Jun 12 15:50:16 IST 2019 arrDelay=0 depTime=Wed Jun 12 15:50:16 IST 2019 depDelay=0 STOP id=1178 seq=25 sch=SCHEDULED arrTime=Wed Jun 12 15:50:35 IST 2019 arrDelay=0 depTime=Wed Jun 12 15:50:35 IST 2019 depDelay=0
STOP id=1179 seq=26 sch=SCHEDULED arrTime=Wed Jun 12 15:51:14 IST 2019 arrDelay=0 depTime=Wed Jun 12 15:51:22 IST 2019 depDelay=0 STOP id=1180 seq=27 sch=SCHEDULED arrTime=Wed Jun 12 15:52:21 IST 2019 arrDelay=0 depTime=Wed Jun 12 15:52:43 IST 2019 depDelay=0
...
...
...
...

关于Java、GTFS Realtime、protocol buffers,简单搞定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com