private void fixupTransitLeg(Leg leg, State state, TransitIndexService transitIndex) {
    Edge en = state.getBackEdge();
    leg.route = en.getName();
    Trip trip = state.getBackTrip();
    leg.headsign = state.getBackDirection();
    if (trip != null) {
      // this is the stop headsign
      // leg.headsign = "This is the headsign";
      // handle no stop headsign
      if (leg.headsign == null) leg.headsign = trip.getTripHeadsign();

      leg.tripId = trip.getId().getId();
      leg.agencyId = trip.getId().getAgencyId();
      leg.tripShortName = trip.getTripShortName();
      leg.routeShortName = trip.getRoute().getShortName();
      leg.routeLongName = trip.getRoute().getLongName();
      leg.routeColor = trip.getRoute().getColor();
      leg.routeTextColor = trip.getRoute().getTextColor();
      leg.routeId = trip.getRoute().getId().getId();
      if (transitIndex != null) {
        Agency agency = transitIndex.getAgency(leg.agencyId);
        leg.agencyName = agency.getName();
        leg.agencyUrl = agency.getUrl();
      }
    }
    leg.mode = state.getBackMode().toString();
    leg.startTime = makeCalendar(state.getBackState());
  }
  @Override
  public int compare(StopTime o1, StopTime o2) {
    Trip trip1 = o1.getTrip();
    Trip trip2 = o2.getTrip();
    int c = trip1.getId().compareTo(trip2.getId());

    if (c == 0) c = o1.getStopSequence() - o2.getStopSequence();

    return c;
  }
Exemplo n.º 3
0
  public GraphIndex(Graph graph) {
    LOG.info("Indexing graph...");

    for (String feedId : graph.getFeedIds()) {
      for (Agency agency : graph.getAgencies(feedId)) {
        Map<String, Agency> agencyForId = agenciesForFeedId.getOrDefault(feedId, new HashMap<>());
        agencyForId.put(agency.getId(), agency);
        this.agenciesForFeedId.put(feedId, agencyForId);
      }
    }

    Collection<Edge> edges = graph.getEdges();
    /* We will keep a separate set of all vertices in case some have the same label.
     * Maybe we should just guarantee unique labels. */
    Set<Vertex> vertices = Sets.newHashSet();
    for (Edge edge : edges) {
      vertices.add(edge.getFromVertex());
      vertices.add(edge.getToVertex());
      if (edge instanceof TablePatternEdge) {
        TablePatternEdge patternEdge = (TablePatternEdge) edge;
        TripPattern pattern = patternEdge.getPattern();
        patternForId.put(pattern.code, pattern);
      }
    }
    for (Vertex vertex : vertices) {
      vertexForId.put(vertex.getLabel(), vertex);
      if (vertex instanceof TransitStop) {
        TransitStop transitStop = (TransitStop) vertex;
        Stop stop = transitStop.getStop();
        stopForId.put(stop.getId(), stop);
        stopVertexForStop.put(stop, transitStop);
        stopsForParentStation.put(stop.getParentStation(), stop);
      }
    }
    for (TransitStop stopVertex : stopVertexForStop.values()) {
      Envelope envelope = new Envelope(stopVertex.getCoordinate());
      stopSpatialIndex.insert(envelope, stopVertex);
    }
    for (TripPattern pattern : patternForId.values()) {
      patternsForFeedId.put(pattern.getFeedId(), pattern);
      patternsForRoute.put(pattern.route, pattern);

      for (Trip trip : pattern.getTrips()) {
        patternForTrip.put(trip, pattern);
        tripForId.put(trip.getId(), trip);
      }
      for (Stop stop : pattern.getStops()) {
        patternsForStop.put(stop, pattern);
      }
    }
    for (Route route : patternsForRoute.asMap().keySet()) {
      routeForId.put(route.getId(), route);
    }

    // Copy these two service indexes from the graph until we have better ones.
    calendarService = graph.getCalendarService();
    serviceCodes = graph.serviceCodes;
    this.graph = graph;
    LOG.info("Done indexing graph.");
  }
Exemplo n.º 4
0
  @BeforeClass
  public static void setUp() throws Exception {

    context = GtfsLibrary.readGtfs(new File(ConstantsForTests.FAKE_GTFS));
    graph = new Graph();

    GTFSPatternHopFactory factory = new GTFSPatternHopFactory(context);
    factory.run(graph);
    graph.putService(
        CalendarServiceData.class, GtfsLibrary.createCalendarServiceData(context.getDao()));

    patternIndex = new HashMap<AgencyAndId, TripPattern>();
    for (TransitStopDepart tsd : filter(graph.getVertices(), TransitStopDepart.class)) {
      for (TransitBoardAlight tba : filter(tsd.getOutgoing(), TransitBoardAlight.class)) {
        if (!tba.isBoarding()) continue;
        TripPattern pattern = tba.getPattern();
        for (Trip trip : pattern.getTrips()) {
          patternIndex.put(trip.getId(), pattern);
        }
      }
    }

    pattern = patternIndex.get(new AgencyAndId("agency", "1.1"));
    timetable = pattern.scheduledTimetable;
  }
 /**
  * Get a list containing one AgencyAndId (trip id) for each vehicle boarded in this path.
  *
  * @return a list of the ids of trips used by this path
  */
 public List<AgencyAndId> getTrips() {
   List<AgencyAndId> ret = new LinkedList<AgencyAndId>();
   for (State s : states) {
     Edge e = s.getBackEdge();
     if (e == null) continue;
     Trip trip = s.getBackTrip();
     if (trip != null) ret.add(trip.getId());
   }
   return ret;
 }
Exemplo n.º 6
0
 private void fixupTransitLeg(Leg leg, State state, TransitIndexService transitIndex) {
   EdgeNarrative en = state.getBackEdgeNarrative();
   leg.route = en.getName();
   Trip trip = en.getTrip();
   if (trip != null) {
     leg.headsign = trip.getTripHeadsign();
     leg.tripId = trip.getId().getId();
     leg.agencyId = trip.getId().getAgencyId();
     leg.tripShortName = trip.getTripShortName();
     leg.routeShortName = trip.getRoute().getShortName();
     leg.routeLongName = trip.getRoute().getLongName();
     leg.routeColor = trip.getRoute().getColor();
     leg.routeTextColor = trip.getRoute().getTextColor();
     if (transitIndex != null) {
       Agency agency = transitIndex.getAgency(leg.agencyId);
       leg.agencyName = agency.getName();
       leg.agencyUrl = agency.getUrl();
     }
   }
   leg.mode = en.getMode().toString();
   leg.startTime = makeCalendar(state.getBackState());
 }
Exemplo n.º 7
0
 /**
  * @return A list containing one AgencyAndId (trip_id) for each vehicle boarded in this path, in
  *     the chronological order they are boarded.
  */
 public List<AgencyAndId> getTrips() {
   List<AgencyAndId> ret = new LinkedList<AgencyAndId>();
   Trip lastTrip = null;
   for (State s : states) {
     if (s.getBackEdge() != null) {
       Trip trip = s.getBackTrip();
       if (trip != null && trip != lastTrip) {
         ret.add(trip.getId());
         lastTrip = trip;
       }
     }
   }
   return ret;
 }
Exemplo n.º 8
0
  public State traverse(State state0) {
    RoutingContext rctx = state0.getContext();
    RoutingRequest options = state0.getOptions();
    Trip trip = pattern.getTrip();

    if (options.isArriveBy()) {
      /* reverse traversal, not so much to do */
      // do not alight immediately when arrive-depart dwell has been eliminated
      // this affects multi-itinerary searches
      if (state0.getBackEdge() instanceof TransitBoardAlight
          && !((TransitBoardAlight) state0.getBackEdge()).isBoarding()) {
        return null;
      }
      StateEditor s1 = state0.edit(this);
      int type = pattern.getBoardType(stopIndex);
      if (TransitUtils.handleBoardAlightType(s1, type)) {
        return null;
      }
      s1.setTripId(null);
      s1.setLastAlightedTime(state0.getTime());
      s1.setBackMode(TraverseMode.BOARDING);
      s1.setPreviousStop(fromv);
      return s1.makeState();
    } else {
      /* forward traversal: look for a transit trip on this pattern */
      if (!options.getModes().get(modeMask)) {
        return null;
      }
      /* find next boarding time */
      /*
       * check lists of transit serviceIds running yesterday, today, and tomorrow (relative to
       * initial state) if this pattern's serviceId is running look for the next boarding time
       * choose the soonest boarding time among trips starting yesterday, today, or tomorrow
       */
      long currentTime = state0.getTime();
      int bestWait = -1;
      TraverseMode mode = state0.getNonTransitMode();
      if (options.bannedTrips.containsKey(trip.getId())) {
        // see comment in FrequencyAlight for details
        return null;
      }
      for (ServiceDay sd : rctx.serviceDays) {
        int secondsSinceMidnight = sd.secondsSinceMidnight(currentTime);
        // only check for service on days that are not in the future
        // this avoids unnecessarily examining tomorrow's services
        if (secondsSinceMidnight < 0) continue;
        if (sd.serviceIdRunning(serviceId)) {
          int startTime =
              pattern.getNextDepartureTime(
                  stopIndex,
                  secondsSinceMidnight,
                  options.wheelchairAccessible,
                  mode == TraverseMode.BICYCLE,
                  true);
          if (startTime >= 0) {
            // a trip was found, wait will be non-negative

            int wait = (int) (sd.time(startTime) - currentTime);
            if (wait < 0) _log.error("negative wait time on board");
            if (bestWait < 0 || wait < bestWait) {
              // track the soonest departure over all relevant schedules
              bestWait = wait;
            }
          }
        }
      }
      if (bestWait < 0) {
        return null;
      }

      /* check if trip is banned for this plan */
      if (options.tripIsBanned(trip)) return null;

      /* check if route is preferred for this plan */
      long preferences_penalty = options.preferencesPenaltyForTrip(trip);

      StateEditor s1 = state0.edit(this);
      int type = pattern.getBoardType(stopIndex);
      if (TransitUtils.handleBoardAlightType(s1, type)) {
        return null;
      }
      s1.incrementTimeInSeconds(bestWait);
      s1.incrementNumBoardings();
      s1.setTripId(trip.getId());
      s1.setZone(pattern.getZone(stopIndex));
      s1.setRoute(trip.getRoute().getId());
      s1.setBackMode(TraverseMode.BOARDING);

      long wait_cost = bestWait;
      if (state0.getNumBoardings() == 0) {
        wait_cost *= options.waitAtBeginningFactor;
      } else {
        wait_cost *= options.waitReluctance;
      }
      s1.incrementWeight(preferences_penalty);
      s1.incrementWeight(wait_cost + options.getBoardCost(mode));
      return s1.makeState();
    }
  }
Exemplo n.º 9
0
 @Override
 public String getMessage() {
   return String.format(FMT, speed, distance, trip.getRoute().getId(), trip.getId(), seq);
 }