首页 > 解决方案 > Java、GTFS Realtime、协议缓冲区、简单获取?

问题描述

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

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

标签: javaprotocol-buffersgtfs

解决方案


以下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
  ...
  ...
...
...

推荐阅读