Esempio n. 1
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.");
  }
 // this could be handled by method overloading on Vertex
 public boolean isWheelchairAccessible(Vertex v) {
   if (v instanceof TransitStop) {
     TransitStop ts = (TransitStop) v;
     return ts.hasWheelchairEntrance();
   } else if (v instanceof StreetLocation) {
     StreetLocation sl = (StreetLocation) v;
     return sl.isWheelchairAccessible();
   }
   return true;
 }
Esempio n. 3
0
 /**
  * FIXME OBA parentStation field is a string, not an AgencyAndId, so it has no agency/feed scope
  * But the DC regional graph has no parent stations pre-defined, so no use dealing with them for
  * now. However Trimet stops have "landmark" or Transit Center parent stations, so we don't use
  * the parent stop field.
  *
  * <p>Ideally in the future stop clusters will replicate and/or share implementation with GTFS
  * parent stations.
  *
  * <p>We can't use a similarity comparison, we need exact matches. This is because many street
  * names differ by only one letter or number, e.g. 34th and 35th or Avenue A and Avenue B.
  * Therefore normalizing the names before the comparison is essential. The agency must provide
  * either parent station information or a well thought out stop naming scheme to cluster stops --
  * no guessing is reasonable without that information.
  */
 public void clusterStops() {
   int psIdx = 0; // unique index for next parent stop
   LOG.info("Clustering stops by geographic proximity and name...");
   // Each stop without a cluster will greedily claim other stops without clusters.
   for (Stop s0 : stopForId.values()) {
     if (stopClusterForStop.containsKey(s0))
       continue; // skip stops that have already been claimed by a cluster
     String s0normalizedName = StopNameNormalizer.normalize(s0.getName());
     StopCluster cluster = new StopCluster(String.format("C%03d", psIdx++), s0normalizedName);
     // LOG.info("stop {}", s0normalizedName);
     // No need to explicitly add s0 to the cluster. It will be found in the spatial index query
     // below.
     Envelope env = new Envelope(new Coordinate(s0.getLon(), s0.getLat()));
     env.expandBy(
         SphericalDistanceLibrary.metersToLonDegrees(CLUSTER_RADIUS, s0.getLat()),
         SphericalDistanceLibrary.metersToDegrees(CLUSTER_RADIUS));
     for (TransitStop ts1 : stopSpatialIndex.query(env)) {
       Stop s1 = ts1.getStop();
       double geoDistance =
           SphericalDistanceLibrary.fastDistance(
               s0.getLat(), s0.getLon(), s1.getLat(), s1.getLon());
       if (geoDistance < CLUSTER_RADIUS) {
         String s1normalizedName = StopNameNormalizer.normalize(s1.getName());
         // LOG.info("   --> {}", s1normalizedName);
         // LOG.info("       geodist {} stringdist {}", geoDistance, stringDistance);
         if (s1normalizedName.equals(s0normalizedName)) {
           // Create a bidirectional relationship between the stop and its cluster
           cluster.children.add(s1);
           stopClusterForStop.put(s1, cluster);
         }
       }
     }
     cluster.computeCenter();
     stopClusterForId.put(cluster.id, cluster);
   }
   //        LOG.info("Done clustering stops.");
   //        for (StopCluster cluster : stopClusterForId.values()) {
   //            LOG.info("{} at {} {}", cluster.name, cluster.lat, cluster.lon);
   //            for (Stop stop : cluster.children) {
   //                LOG.info("   {}", stop.getName());
   //            }
   //        }
 }
  @Test
  public final void testOnBoardDepartureAtArrivalTime() {
    Coordinate[] coordinates = new Coordinate[2];
    coordinates[0] = new Coordinate(0.0, 0.0);
    coordinates[1] = new Coordinate(0.0, 1.0);

    TransitStop station0 = mock(TransitStop.class);
    TransitStop station1 = mock(TransitStop.class);
    PatternDepartVertex depart = mock(PatternDepartVertex.class);
    PatternArriveVertex arrive = mock(PatternArriveVertex.class);
    Graph graph = mock(Graph.class);
    RoutingRequest routingRequest = mock(RoutingRequest.class);
    ServiceDay serviceDay = mock(ServiceDay.class);

    when(graph.getTimeZone()).thenReturn(TimeZone.getTimeZone("GMT"));
    when(station0.getX()).thenReturn(coordinates[0].x);
    when(station0.getY()).thenReturn(coordinates[0].y);
    when(station1.getX()).thenReturn(coordinates[1].x);
    when(station1.getY()).thenReturn(coordinates[1].y);

    RoutingContext routingContext = new RoutingContext(routingRequest, graph, null, arrive);
    AgencyAndId agencyAndId = new AgencyAndId("Agency", "ID");
    Route route = new Route();
    ArrayList<StopTime> stopTimes = new ArrayList<StopTime>(2);
    StopTime stopDepartTime = new StopTime();
    StopTime stopArriveTime = new StopTime();
    Stop stopDepart = new Stop();
    Stop stopArrive = new Stop();
    Trip trip = new Trip();

    routingContext.serviceDays = new ArrayList<ServiceDay>(Collections.singletonList(serviceDay));
    route.setId(agencyAndId);
    stopDepart.setId(new AgencyAndId("Station", "0"));
    stopArrive.setId(new AgencyAndId("Station", "1"));
    stopDepartTime.setStop(stopDepart);
    stopDepartTime.setDepartureTime(0);
    stopArriveTime.setArrivalTime(10);
    stopArriveTime.setStop(stopArrive);
    stopTimes.add(stopDepartTime);
    stopTimes.add(stopArriveTime);
    trip.setId(agencyAndId);

    TripTimes tripTimes = new TripTimes(trip, stopTimes, new Deduplicator());
    StopPattern stopPattern = new StopPattern(stopTimes);
    TripPattern tripPattern = new TripPattern(route, stopPattern);

    when(depart.getTripPattern()).thenReturn(tripPattern);

    PatternHop patternHop = new PatternHop(depart, arrive, stopDepart, stopArrive, 0);

    when(graph.getEdges()).thenReturn(Collections.<Edge>singletonList(patternHop));
    when(depart.getCoordinate()).thenReturn(new Coordinate(0, 0));
    when(arrive.getCoordinate()).thenReturn(new Coordinate(0, 0));
    when(routingRequest.getFrom()).thenReturn(new GenericLocation());
    when(routingRequest.getStartingTransitTripId()).thenReturn(agencyAndId);
    when(serviceDay.secondsSinceMidnight(anyInt())).thenReturn(10);
    when(graph.getVertex("Station_0")).thenReturn(station0);
    when(graph.getVertex("Station_1")).thenReturn(station1);

    tripPattern.add(tripTimes);
    graph.index = new GraphIndex(graph);

    Vertex vertex = onBoardDepartServiceImpl.setupDepartOnBoard(routingContext);

    assertEquals(coordinates[1].x, vertex.getX(), 0.0);
    assertEquals(coordinates[1].y, vertex.getY(), 0.0);
  }
  public 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()));

    String[] stops = {
      "agency:A",
      "agency:B",
      "agency:C",
      "agency:D",
      "agency:E",
      "agency:entrance_a",
      "agency:entrance_b"
    };
    for (int i = 0; i < stops.length; ++i) {
      TransitStop stop = (TransitStop) (graph.getVertex(stops[i]));

      IntersectionVertex front =
          new IntersectionVertex(
              graph, "near_1_" + stop.getStopId(), stop.getX() + 0.0001, stop.getY() + 0.0001);
      IntersectionVertex back =
          new IntersectionVertex(
              graph, "near_2_" + stop.getStopId(), stop.getX() - 0.0001, stop.getY() - 0.0001);

      StreetEdge street1 =
          new StreetEdge(
              front,
              back,
              GeometryUtils.makeLineString(
                  stop.getX() + 0.0001,
                  stop.getY() + 0.0001,
                  stop.getX() - 0.0001,
                  stop.getY() - 0.0001),
              "street",
              100,
              StreetTraversalPermission.ALL,
              false);
      StreetEdge street2 =
          new StreetEdge(
              back,
              front,
              GeometryUtils.makeLineString(
                  stop.getX() - 0.0001,
                  stop.getY() - 0.0001,
                  stop.getX() + 0.0001,
                  stop.getY() + 0.0001),
              "street",
              100,
              StreetTraversalPermission.ALL,
              true);
    }

    NetworkLinker nl = new NetworkLinker(graph);
    nl.createLinkage();
  }
  public void testTransfers() throws Exception {
    TransferTable transferTable = graph.getTransferTable();

    // create dummy routes and trips
    Route fromRoute = new Route();
    fromRoute.setId(new AgencyAndId("agency", "1"));
    Trip fromTrip = new Trip();
    fromTrip.setId(new AgencyAndId("agency", "1.1"));
    fromTrip.setRoute(fromRoute);
    Route toRoute = new Route();
    toRoute.setId(new AgencyAndId("agency", "2"));
    Trip toTrip = new Trip();
    toTrip.setId(new AgencyAndId("agency", "2.1"));
    toTrip.setRoute(toRoute);
    Trip toTrip2 = new Trip();
    toTrip2.setId(new AgencyAndId("agency", "2.2"));
    toTrip2.setRoute(toRoute);

    // find stops
    Stop stopK = ((TransitStopArrive) graph.getVertex("agency:K_arrive")).getStop();
    Stop stopN = ((TransitStopDepart) graph.getVertex("agency:N_depart")).getStop();
    Stop stopM = ((TransitStopDepart) graph.getVertex("agency:M_depart")).getStop();

    assertTrue(transferTable.hasPreferredTransfers());
    assertEquals(
        StopTransfer.UNKNOWN_TRANSFER,
        transferTable.getTransferTime(stopN, stopM, fromTrip, toTrip, true));
    assertEquals(
        StopTransfer.FORBIDDEN_TRANSFER,
        transferTable.getTransferTime(stopK, stopM, fromTrip, toTrip, true));
    assertEquals(
        StopTransfer.PREFERRED_TRANSFER,
        transferTable.getTransferTime(stopN, stopK, toTrip, toTrip2, true));
    assertEquals(
        StopTransfer.TIMED_TRANSFER,
        transferTable.getTransferTime(stopN, stopK, fromTrip, toTrip, true));
    assertEquals(15, transferTable.getTransferTime(stopN, stopK, fromTrip, toTrip2, true));

    TransitStop e_arrive = (TransitStop) graph.getVertex("agency:E");
    TransitStop f_depart = (TransitStop) graph.getVertex("agency:F");
    Edge edge = new TransferEdge(e_arrive, f_depart, 10000, 10000);

    long startTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 18, 0, 50, 0);
    Vertex stop_b = graph.getVertex("agency:B_depart");
    Vertex stop_g = graph.getVertex("agency:G_arrive");
    RoutingRequest options = new RoutingRequest();
    options.dateTime = startTime;
    options.setRoutingContext(graph, stop_b, stop_g);
    ShortestPathTree spt = aStar.getShortestPathTree(options);

    GraphPath path = spt.getPath(stop_g, false);
    assertNotNull(path);

    assertTrue(
        "expected to use much later trip due to min transfer time",
        path.getEndTime() - startTime > 4.5 * 60 * 60);

    /* cleanup */
    e_arrive.removeOutgoing(edge);
    f_depart.removeIncoming(edge);
  }
  @Override
  public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
    final Parser parser[] = new Parser[] {new Parser()};
    GeometryFactory geometryFactory = GeometryUtils.getGeometryFactory();
    EarliestArrivalSPTService earliestArrivalSPTService = new EarliestArrivalSPTService();
    earliestArrivalSPTService.maxDuration = (maxDuration);

    for (TransitStop ts : Iterables.filter(graph.getVertices(), TransitStop.class)) {
      // Only link street linkable stops
      if (!ts.isStreetLinkable()) continue;
      LOG.trace("linking stop '{}' {}", ts.getStop(), ts);

      // Determine the set of pathway/transfer destinations
      Set<TransitStop> pathwayDestinations = new HashSet<TransitStop>();
      for (Edge e : ts.getOutgoing()) {
        if (e instanceof PathwayEdge || e instanceof SimpleTransfer) {
          if (e.getToVertex() instanceof TransitStop) {
            TransitStop to = (TransitStop) e.getToVertex();
            pathwayDestinations.add(to);
          }
        }
      }

      int n = 0;
      RoutingRequest routingRequest = new RoutingRequest(TraverseMode.WALK);
      routingRequest.clampInitialWait = (0L);
      routingRequest.setRoutingContext(graph, ts, null);
      routingRequest.rctx.pathParsers = parser;
      ShortestPathTree spt = earliestArrivalSPTService.getShortestPathTree(routingRequest);

      if (spt != null) {
        for (State state : spt.getAllStates()) {
          Vertex vertex = state.getVertex();
          if (ts == vertex) continue;

          if (vertex instanceof TransitStop) {
            TransitStop other = (TransitStop) vertex;
            if (!other.isStreetLinkable()) continue;
            if (pathwayDestinations.contains(other)) {
              LOG.trace("Skipping '{}', {}, already connected.", other.getStop(), other);
              continue;
            }
            double distance = 0.0;
            GraphPath graphPath = new GraphPath(state, false);
            CoordinateArrayListSequence coordinates = new CoordinateArrayListSequence();

            for (Edge edge : graphPath.edges) {
              if (edge instanceof StreetEdge) {
                LineString geometry = edge.getGeometry();

                if (geometry != null) {
                  if (coordinates.size() == 0) {
                    coordinates.extend(geometry.getCoordinates());
                  } else {
                    coordinates.extend(geometry.getCoordinates(), 1);
                  }
                }

                distance += edge.getDistance();
              }
            }

            if (coordinates.size() < 2) { // Otherwise the walk step generator breaks.
              ArrayList<Coordinate> coordinateList = new ArrayList<Coordinate>(2);
              coordinateList.add(graphPath.states.get(1).getVertex().getCoordinate());
              State lastState = graphPath.states.getLast().getBackState();
              coordinateList.add(lastState.getVertex().getCoordinate());
              coordinates = new CoordinateArrayListSequence(coordinateList);
            }

            LineString geometry =
                geometryFactory.createLineString(
                    new PackedCoordinateSequence.Double(coordinates.toCoordinateArray()));
            LOG.trace("  to stop: '{}' {} ({}m) [{}]", other.getStop(), other, distance, geometry);
            new SimpleTransfer(ts, other, distance, geometry);
            n++;
          }
        }
      }

      LOG.trace("linked to {} others.", n);
      if (n == 0) {
        LOG.warn(graph.addBuilderAnnotation(new StopNotLinkedForTransfers(ts)));
      }
    }
  }