Java, GTFS Realtime, буферы протокола, просто получить? - PullRequest
0 голосов
/ 12 июня 2019

Я использую эту вещь: https://github.com/MobilityData/gtfs-realtime-bindings/tree/final-google-version/java

И я могу следовать приведенному примеру кода.Но это только занимает меня, я не понимаю, как извлечь более детальную информацию.Я хотел бы знать, как просто извлечь задержку для определенной поездки или stop_id.Кто-нибудь может мне помочь?

1 Ответ

0 голосов
/ 12 июня 2019

Следующая 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
  ...
  ...
...
...
...