Search in sources :

Example 1 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao in project onebusaway-application-modules by camsys.

the class GtfsArchiveTask method run.

@Override
public void run() {
    if (!requestResponse.getRequest().getArchiveFlag()) {
        _log.info("archive flag not set, exiting");
        return;
    }
    long start = SystemTime.currentTimeMillis();
    _log.info("archiving gtfs");
    Configuration config = getConfiguration();
    if (config == null) {
        _log.error("missing configuration, GTFS will not be archived");
        return;
    }
    SessionFactory sessionFactory = config.buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    HibernateGtfsFactory factory = new HibernateGtfsFactory(sessionFactory);
    GtfsBundles gtfsBundles = getGtfsBundles(_applicationContext);
    Integer gtfsBundleInfoId = createMetaData(session, requestResponse);
    for (GtfsBundle gtfsBundle : gtfsBundles.getBundles()) {
        GtfsReader reader = new GtfsReader();
        reader.getEntityClasses().add(PatternPair.class);
        try {
            cleanTempTables(session);
            reader.setInputLocation(gtfsBundle.getPath());
            GtfsMutableRelationalDao dao = factory.getDao();
            reader.setEntityStore(dao);
            _log.info("running for gtfs=" + gtfsBundle.getPath());
            reader.run();
            reader.close();
            archiveData(session, gtfsBundleInfoId);
        } catch (IOException e) {
            _log.error("gtfs archive failure:", e);
        }
    }
    cleanTempTables(session);
    transaction.commit();
    session.flush();
    session.close();
    long stop = SystemTime.currentTimeMillis();
    _log.info("archiving gtfs complete in " + (stop - start) / 1000 + "s");
}
Also used : SessionFactory(org.hibernate.SessionFactory) GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) Configuration(org.hibernate.cfg.Configuration) Transaction(org.hibernate.Transaction) GtfsBundle(org.onebusaway.transit_data_federation.bundle.model.GtfsBundle) GtfsBundles(org.onebusaway.transit_data_federation.bundle.model.GtfsBundles) IOException(java.io.IOException) HibernateGtfsFactory(org.onebusaway.gtfs.services.HibernateGtfsFactory) Session(org.hibernate.classic.Session)

Example 2 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao 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 3 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsHibernateReaderExampleMain method main.

public static void main(String[] args) throws IOException {
    if (!(args.length == 1 || args.length == 2)) {
        System.err.println("usage: gtfsPath [hibernate-config.xml]");
        System.exit(-1);
    }
    String resource = "classpath:org/onebusaway/gtfs/examples/hibernate-configuration-examples.xml";
    if (args.length == 2)
        resource = args[1];
    HibernateGtfsFactory factory = createHibernateGtfsFactory(resource);
    GtfsReader reader = new GtfsReader();
    reader.setInputLocation(new File(args[0]));
    GtfsMutableRelationalDao dao = factory.getDao();
    reader.setEntityStore(dao);
    reader.run();
    Collection<Stop> stops = dao.getAllStops();
    for (Stop stop : stops) System.out.println(stop.getName());
    CalendarService calendarService = factory.getCalendarService();
    Set<AgencyAndId> serviceIds = calendarService.getServiceIds();
    for (AgencyAndId serviceId : serviceIds) {
        Set<ServiceDate> dates = calendarService.getServiceDatesForServiceId(serviceId);
        ServiceDate from = null;
        ServiceDate to = null;
        for (ServiceDate date : dates) {
            from = min(from, date);
            to = max(to, date);
        }
        System.out.println("serviceId=" + serviceId + " from=" + from + " to=" + to);
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) HibernateGtfsFactory(org.onebusaway.gtfs.services.HibernateGtfsFactory) File(java.io.File) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService)

Example 4 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao in project onebusaway-gtfs-modules by OneBusAway.

the class UpdateStopNameFromParentStationIfInvalidStrategyTest method test.

@Test
public void test() throws IOException {
    _gtfs.putAgencies(1);
    _gtfs.putLines("stops.txt", "stop_id,stop_name,stop_lat,stop_lon,location_type,parent_station,platform_code", "stop0,2,1,1,0,,", "stop1,Station A Platform 1,0,0,0,station0,1", "stop2,2,0,0,0,station0,2", "station0,Station A,0,0,1,,");
    _gtfs.putCalendars(1);
    _gtfs.putRoutes(1);
    _gtfs.putTrips(2, "r0", "sid0");
    _gtfs.putStopTimes("t0", "stop0,stop1");
    _gtfs.putStopTimes("t0", "stop2,stop0");
    GtfsMutableRelationalDao dao = _gtfs.read();
    TransformContext tc = new TransformContext();
    tc.setDefaultAgencyId("a0");
    GtfsTransformStrategy strategy = new UpdateStopNameFromParentStationIfInvalidStrategy();
    strategy.run(tc, dao);
    UpdateLibrary.clearDaoCache(dao);
    assertEquals("2", getStopName(dao, "stop0"));
    assertEquals("Station A Platform 1", getStopName(dao, "stop1"));
    assertEquals("Station A", getStopName(dao, "stop2"));
    assertEquals("Station A", getStopName(dao, "station0"));
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) TransformContext(org.onebusaway.gtfs_transformer.services.TransformContext) GtfsTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy) Test(org.junit.Test)

Example 5 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao in project onebusaway-gtfs-modules by OneBusAway.

the class TrimTripTransformStrategyTest method testStopTimeTrimming.

@Test
public void testStopTimeTrimming() throws IOException {
    _gtfs.putAgencies(1);
    _gtfs.putStops(6);
    _gtfs.putRoutes(1);
    _gtfs.putTrips(1, "r0", "sid0");
    _gtfs.putStopTimes("t0", "s0,s1,s2,s3,s4,s5");
    GtfsMutableRelationalDao dao = _gtfs.read();
    TrimOperation operation = new TrimOperation();
    operation.setMatch(new TypedEntityMatch(Trip.class, new AlwaysMatch()));
    operation.setToStopId("s1");
    operation.setFromStopId("s4");
    _strategy.addOperation(operation);
    _strategy.run(_context, dao);
    Collection<Trip> allTrips = dao.getAllTrips();
    assertEquals(1, allTrips.size());
    Trip trip = allTrips.iterator().next();
    assertEquals(new AgencyAndId("a0", "t0-s1-s4"), trip.getId());
    List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
    assertEquals(2, stopTimes.size());
    assertEquals("s2", stopTimes.get(0).getStop().getId().getId());
    assertEquals("s3", stopTimes.get(1).getStop().getId().getId());
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TrimOperation(org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy.TrimOperation) AlwaysMatch(org.onebusaway.gtfs_transformer.match.AlwaysMatch) StopTime(org.onebusaway.gtfs.model.StopTime) Test(org.junit.Test)

Aggregations

GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)57 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)25 Test (org.junit.Test)24 Trip (org.onebusaway.gtfs.model.Trip)22 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)14 Stop (org.onebusaway.gtfs.model.Stop)11 StopTime (org.onebusaway.gtfs.model.StopTime)11 TransformContext (org.onebusaway.gtfs_transformer.services.TransformContext)11 HashMap (java.util.HashMap)10 ArrayList (java.util.ArrayList)9 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)9 ExternalServices (org.onebusaway.cloud.api.ExternalServices)8 ExternalServicesBridgeFactory (org.onebusaway.cloud.api.ExternalServicesBridgeFactory)8 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)7 GtfsRelationalDao (org.onebusaway.gtfs.services.GtfsRelationalDao)7 GtfsTransformStrategy (org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy)7 Route (org.onebusaway.gtfs.model.Route)6 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)6 GtfsReader (org.onebusaway.gtfs.serialization.GtfsReader)5 HashSet (java.util.HashSet)4