Search in sources :

Example 1 with GtfsBundle

use of org.opentripplanner.graph_builder.model.GtfsBundle in project OpenTripPlanner by opentripplanner.

the class GtfsModule method buildGraph.

@Override
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
    // we're about to add another agency to the graph, so clear the cached timezone
    // in case it should change
    // OTP doesn't currently support multiple time zones in a single graph;
    // at least this way we catch the error and log it instead of silently ignoring
    // because the time zone from the first agency is cached
    graph.clearTimeZone();
    MultiCalendarServiceImpl service = new MultiCalendarServiceImpl();
    GtfsStopContext stopContext = new GtfsStopContext();
    try {
        for (GtfsBundle gtfsBundle : gtfsBundles) {
            // apply global defaults to individual GTFSBundles (if globals have been set)
            if (cacheDirectory != null && gtfsBundle.cacheDirectory == null)
                gtfsBundle.cacheDirectory = cacheDirectory;
            if (useCached != null && gtfsBundle.useCached == null)
                gtfsBundle.useCached = useCached;
            GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl();
            GtfsContext context = GtfsLibrary.createContext(gtfsBundle.getFeedId(), dao, service);
            GTFSPatternHopFactory hf = new GTFSPatternHopFactory(context);
            hf.setStopContext(stopContext);
            hf.setFareServiceFactory(_fareServiceFactory);
            hf.setMaxStopToShapeSnapDistance(gtfsBundle.getMaxStopToShapeSnapDistance());
            loadBundle(gtfsBundle, graph, dao);
            CalendarServiceDataFactoryImpl csfactory = new CalendarServiceDataFactoryImpl();
            csfactory.setGtfsDao(dao);
            CalendarServiceData data = csfactory.createData();
            service.addData(data, dao);
            hf.subwayAccessTime = gtfsBundle.subwayAccessTime;
            hf.maxInterlineDistance = gtfsBundle.maxInterlineDistance;
            hf.run(graph);
            if (gtfsBundle.doesTransfersTxtDefineStationPaths()) {
                hf.createTransfersTxtTransfers();
            }
            if (gtfsBundle.linkStopsToParentStations) {
                hf.linkStopsToParentStations(graph);
            }
            if (gtfsBundle.parentStationTransfers) {
                hf.createParentStationTransfers();
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    // We need to save the calendar service data so we can use it later
    CalendarServiceData data = service.getData();
    graph.putService(CalendarServiceData.class, data);
    graph.updateTransitFeedValidity(data);
    graph.hasTransit = true;
    graph.calculateTransitCenter();
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) GtfsBundle(org.opentripplanner.graph_builder.model.GtfsBundle) GtfsContext(org.opentripplanner.gtfs.GtfsContext) GtfsStopContext(org.opentripplanner.routing.edgetype.factory.GtfsStopContext) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) GTFSPatternHopFactory(org.opentripplanner.routing.edgetype.factory.GTFSPatternHopFactory) IOException(java.io.IOException) CalendarServiceDataFactoryImpl(org.opentripplanner.calendar.impl.CalendarServiceDataFactoryImpl) MultiCalendarServiceImpl(org.opentripplanner.calendar.impl.MultiCalendarServiceImpl)

Example 2 with GtfsBundle

use of org.opentripplanner.graph_builder.model.GtfsBundle in project OpenTripPlanner by opentripplanner.

the class FakeGraph method addTransitBidirectional.

/**
 * Add transit (in both directions) to a Columbus graph
 */
public static void addTransitBidirectional(Graph gg) throws Exception {
    // using conveyal GTFS lib to build GTFS so a lot of code does not have to be rewritten later
    // once we're using the conveyal GTFS lib for everything we ought to be able to do this
    // without even writing out the GTFS to a file.
    GTFSFeed feed = new GTFSFeed();
    Agency a = createDummyAgency("agency", "Agency", "America/New_York");
    feed.agency.put("agency", a);
    Route r = new Route();
    r.route_short_name = "1";
    r.route_long_name = "High Street";
    r.route_type = 3;
    r.agency = a;
    r.route_id = "route";
    feed.routes.put(r.route_id, r);
    Service s = createDummyService();
    feed.services.put(s.service_id, s);
    com.conveyal.gtfs.model.Stop s1 = new com.conveyal.gtfs.model.Stop();
    s1.stop_id = s1.stop_name = "s1";
    s1.stop_lat = 40.2182;
    s1.stop_lon = -83.0889;
    feed.stops.put(s1.stop_id, s1);
    com.conveyal.gtfs.model.Stop s2 = new com.conveyal.gtfs.model.Stop();
    s2.stop_id = s2.stop_name = "s2";
    s2.stop_lat = 39.9621;
    s2.stop_lon = -83.0007;
    feed.stops.put(s2.stop_id, s2);
    // make timetabled trips
    for (int departure = 7 * 3600; departure < 20 * 3600; departure += FREQUENCY) {
        Trip t = new Trip();
        t.trip_id = "trip" + departure;
        t.service = s;
        t.route = r;
        t.direction_id = 0;
        feed.trips.put(t.trip_id, t);
        StopTime st1 = new StopTime();
        st1.trip_id = t.trip_id;
        st1.arrival_time = departure;
        st1.departure_time = departure;
        st1.stop_id = s1.stop_id;
        st1.stop_sequence = 1;
        feed.stop_times.put(new Fun.Tuple2(st1.trip_id, st1.stop_sequence), st1);
        StopTime st2 = new StopTime();
        st2.trip_id = t.trip_id;
        st2.arrival_time = departure + TRAVEL_TIME;
        st2.departure_time = departure + TRAVEL_TIME;
        st2.stop_sequence = 2;
        st2.stop_id = s2.stop_id;
        feed.stop_times.put(new Fun.Tuple2(st2.trip_id, st2.stop_sequence), st2);
        // opposite direction
        t = new Trip();
        t.trip_id = "trip_back" + departure;
        t.service = s;
        t.route = r;
        t.direction_id = 1;
        feed.trips.put(t.trip_id, t);
        st1 = new StopTime();
        st1.trip_id = t.trip_id;
        st1.arrival_time = departure;
        st1.departure_time = departure;
        st1.stop_id = s2.stop_id;
        st1.stop_sequence = 1;
        feed.stop_times.put(new Fun.Tuple2(st1.trip_id, st1.stop_sequence), st1);
        st2 = new StopTime();
        st2.trip_id = t.trip_id;
        st2.arrival_time = departure + TRAVEL_TIME;
        st2.departure_time = departure + TRAVEL_TIME;
        st2.stop_sequence = 2;
        st2.stop_id = s1.stop_id;
        feed.stop_times.put(new Fun.Tuple2(st2.trip_id, st2.stop_sequence), st2);
    }
    File tempFile = File.createTempFile("gtfs", ".zip");
    feed.toFile(tempFile.getAbsolutePath());
    // phew. load it into the graph.
    GtfsModule gtfs = new GtfsModule(Arrays.asList(new GtfsBundle(tempFile)));
    gtfs.buildGraph(gg, new HashMap<>());
}
Also used : TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) GtfsBundle(org.opentripplanner.graph_builder.model.GtfsBundle) com.conveyal.gtfs.model(com.conveyal.gtfs.model) GTFSFeed(com.conveyal.gtfs.GTFSFeed) File(java.io.File) Fun(org.mapdb.Fun)

Example 3 with GtfsBundle

use of org.opentripplanner.graph_builder.model.GtfsBundle in project OpenTripPlanner by opentripplanner.

the class FakeGraph method addPerpendicularRoutes.

public static void addPerpendicularRoutes(Graph graph) throws Exception {
    GTFSFeed feed = new GTFSFeed();
    Agency agencyA = createDummyAgency("agencyA", "Agency A", "America/New_York");
    feed.agency.put("agencyA", agencyA);
    Agency agencyB = createDummyAgency("agencyB", "Agency B", "America/New_York");
    feed.agency.put("agencyB", agencyB);
    Service s = createDummyService();
    feed.services.put(s.service_id, s);
    int stopIdX = 0;
    int stopIdY = 0;
    for (double lat = 39.9058; lat < 40.0281; lat += 0.005) {
        stopIdX = 0;
        for (double lon = -83.1341; lon < -82.8646; lon += 0.005) {
            com.conveyal.gtfs.model.Stop stop = new com.conveyal.gtfs.model.Stop();
            stop.stop_id = stop.stop_name = String.format("s-%d-%d", stopIdX, stopIdY);
            stop.stop_lat = lat;
            stop.stop_lon = lon;
            feed.stops.put(stop.stop_id, stop);
            stopIdX++;
        }
        stopIdY++;
    }
    for (int i = 0; i < stopIdY; i++) {
        Route route = new Route();
        route.route_short_name = "hr" + i;
        route.route_long_name = i + "th Horizontal Street";
        route.route_type = Route.BUS;
        route.agency = agencyA;
        route.route_id = "horizontalroute" + i;
        feed.routes.put(route.route_id, route);
    }
    for (int i = 0; i < stopIdX; i++) {
        Route route = new Route();
        route.route_short_name = "vr" + i;
        route.route_long_name = i + "th Vertical Street";
        route.route_type = Route.TRAM;
        route.agency = agencyB;
        route.route_id = "verticalroute" + i;
        feed.routes.put(route.route_id, route);
    }
    Map<String, Route> routes = feed.routes;
    com.conveyal.gtfs.model.Stop stop;
    for (Route route : routes.values()) {
        int routeId = Integer.parseInt(route.route_short_name.substring(2));
        int x, y;
        boolean isHorizontal = route.route_short_name.startsWith("hr");
        for (int departure = 7 * 3600; departure < 20 * 3600; departure += FREQUENCY) {
            Trip t = new Trip();
            t.trip_id = "trip:" + route.route_id + ":" + departure;
            t.service = s;
            t.route = route;
            feed.trips.put(t.trip_id, t);
            int departureTime = departure;
            int nrOfStops = (isHorizontal ? stopIdX : stopIdY);
            for (int stopSequenceNr = 0; stopSequenceNr < nrOfStops; stopSequenceNr++) {
                x = (isHorizontal ? stopSequenceNr : routeId);
                y = (isHorizontal ? routeId : stopSequenceNr);
                stop = feed.stops.get(String.format("s-%d-%d", x, y));
                StopTime st1 = new StopTime();
                st1.trip_id = t.trip_id;
                st1.arrival_time = departureTime;
                st1.departure_time = departureTime;
                st1.stop_id = stop.stop_id;
                st1.stop_sequence = stopSequenceNr + 1;
                feed.stop_times.put(new Fun.Tuple2(st1.trip_id, st1.stop_sequence), st1);
                // connect last stop to first so graph is fully reachable
                if (stopSequenceNr == 0) {
                    StopTime stopTime = new StopTime();
                    stopTime.trip_id = t.trip_id;
                    stopTime.arrival_time = departureTime + nrOfStops * 120 + 30 * 60;
                    stopTime.departure_time = departureTime + nrOfStops * 120 + 30 * 60;
                    stopTime.stop_id = stop.stop_id;
                    stopTime.stop_sequence = stopSequenceNr + 1 + nrOfStops;
                    feed.stop_times.put(new Fun.Tuple2(stopTime.trip_id, stopTime.stop_sequence), stopTime);
                }
                departureTime += 120;
            }
        }
    }
    File tempFile = File.createTempFile("gtfs", ".zip");
    feed.toFile(tempFile.getAbsolutePath());
    // phew. load it into the graph.
    GtfsModule gtfs = new GtfsModule(Arrays.asList(new GtfsBundle(tempFile)));
    gtfs.buildGraph(graph, new HashMap<>());
}
Also used : TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) GtfsBundle(org.opentripplanner.graph_builder.model.GtfsBundle) com.conveyal.gtfs.model(com.conveyal.gtfs.model) GTFSFeed(com.conveyal.gtfs.GTFSFeed) File(java.io.File) Fun(org.mapdb.Fun)

Example 4 with GtfsBundle

use of org.opentripplanner.graph_builder.model.GtfsBundle in project OpenTripPlanner by opentripplanner.

the class GtfsGraphBuilderModuleTest method getGtfsAsBundleList.

private static List<GtfsBundle> getGtfsAsBundleList(MockGtfs gtfs) {
    GtfsBundle bundle = new GtfsBundle();
    bundle.setFeedId(new GtfsFeedId.Builder().id("FEED").build());
    bundle.setPath(gtfs.getPath());
    List<GtfsBundle> list = Lists.newArrayList();
    list.add(bundle);
    return list;
}
Also used : GtfsBundle(org.opentripplanner.graph_builder.model.GtfsBundle)

Example 5 with GtfsBundle

use of org.opentripplanner.graph_builder.model.GtfsBundle in project OpenTripPlanner by opentripplanner.

the class GtfsGraphBuilderModuleTest method testBikesByDefault.

@Test
public void testBikesByDefault() throws IOException {
    // We configure two trip: one with unknown bikes_allowed and the second with no bikes
    // allowed.
    MockGtfs gtfs = getSimpleGtfs();
    gtfs.putTrips(2, "r0", "sid0", "bikes_allowed=0,2");
    gtfs.putStopTimes("t0,t1", "s0,s1");
    List<GtfsBundle> bundleList = getGtfsAsBundleList(gtfs);
    bundleList.get(0).setDefaultBikesAllowed(true);
    _builder = new GtfsModule(bundleList);
    Graph graph = new Graph();
    _builder.buildGraph(graph, _extra);
    graph.index(new DefaultStreetVertexIndexFactory());
    // Feed id is used instead of the agency id for OBA entities.
    GtfsBundle gtfsBundle = bundleList.get(0);
    GtfsFeedId feedId = gtfsBundle.getFeedId();
    Trip trip = graph.index.tripForId.get(new AgencyAndId(feedId.getId(), "t0"));
    TripPattern pattern = graph.index.patternForTrip.get(trip);
    List<Trip> trips = pattern.getTrips();
    assertEquals(BikeAccess.ALLOWED, BikeAccess.fromTrip(withId(trips, new AgencyAndId(feedId.getId(), "t0"))));
    assertEquals(BikeAccess.NOT_ALLOWED, BikeAccess.fromTrip(withId(trips, new AgencyAndId(feedId.getId(), "t1"))));
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) Graph(org.opentripplanner.routing.graph.Graph) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) GtfsBundle(org.opentripplanner.graph_builder.model.GtfsBundle) MockGtfs(org.onebusaway.gtfs.services.MockGtfs) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) Test(org.junit.Test)

Aggregations

GtfsBundle (org.opentripplanner.graph_builder.model.GtfsBundle)11 File (java.io.File)7 GTFSFeed (com.conveyal.gtfs.GTFSFeed)5 com.conveyal.gtfs.model (com.conveyal.gtfs.model)5 Fun (org.mapdb.Fun)5 Stop (org.onebusaway.gtfs.model.Stop)5 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)5 Graph (org.opentripplanner.routing.graph.Graph)3 DefaultStreetVertexIndexFactory (org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)3 Test (org.junit.Test)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 Trip (org.onebusaway.gtfs.model.Trip)2 MockGtfs (org.onebusaway.gtfs.services.MockGtfs)2 GtfsModule (org.opentripplanner.graph_builder.module.GtfsModule)2 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)1 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)1 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)1 FileInputStream (java.io.FileInputStream)1