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");
}
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();
}
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);
}
}
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"));
}
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());
}
Aggregations