Search in sources :

Example 1 with GtfsReader

use of org.onebusaway.gtfs.serialization.GtfsReader in project onebusaway-gtfs-modules by OneBusAway.

the class LongRouteDescriptionTest method setup.

@BeforeClass
public static void setup() throws IOException {
    Configuration config = new Configuration();
    config = config.configure("org/onebusaway/gtfs/hibernate-configuration.xml");
    _sessionFactory = config.buildSessionFactory();
    _dao = new HibernateGtfsRelationalDaoImpl(_sessionFactory);
    GtfsReader reader = new GtfsReader();
    reader.setInputLocation(new File("src/test/resources/org/onebusaway/gtfs/caltrain-long-route.zip"));
    reader.setEntityStore(_dao);
    reader.setDefaultAgencyId(_agencyId);
    List<Class<?>> entityClasses = reader.getEntityClasses();
    entityClasses.clear();
    entityClasses.add(Agency.class);
    entityClasses.add(Route.class);
    reader.run();
}
Also used : GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) Configuration(org.hibernate.cfg.Configuration) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 2 with GtfsReader

use of org.onebusaway.gtfs.serialization.GtfsReader in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsMerger method run.

public void run(List<File> inputPaths, File outputPath) throws IOException {
    GtfsRelationalDaoImpl mergedDao = new GtfsRelationalDaoImpl();
    mergedDao.setPackShapePoints(true);
    mergedDao.setPackStopTimes(true);
    List<EntityMergeStrategy> strategies = new ArrayList<EntityMergeStrategy>();
    buildStrategies(strategies);
    /**
     * For each entity merge strategy, we keep track of a mapping from raw GTFS
     * ids to entities, if the particular entity type has an identifier. This
     * will be used to detect id conflicts between subsequent runs of each merge
     * strategy on different feeds. We can't use the AgencyAndId ids in the DAO
     * because it might be possible for two entities with the same id but
     * different agency prefixes to sneak in. Since we ultimately serialize the
     * data to a GTFS feed with no agency prefixes, we need to track the raw id.
     */
    Map<EntityMergeStrategy, Map<String, Object>> rawEntityIdMapsByMergeStrategy = new HashMap<EntityMergeStrategy, Map<String, Object>>();
    for (EntityMergeStrategy strategy : strategies) {
        rawEntityIdMapsByMergeStrategy.put(strategy, new HashMap<String, Object>());
    }
    /**
     * We iterate over the input feeds in reverse order, such that entities from
     * the newest feeds are added first and older entities are potentially
     * dropped.
     */
    for (int index = inputPaths.size() - 1; index >= 0; --index) {
        File inputPath = inputPaths.get(index);
        String prefix = getIndexAsPrefix(index, inputPaths.size());
        _log.info("reading input: " + inputPath);
        GtfsReader reader = new GtfsReader();
        reader.setInputLocation(inputPath);
        GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
        dao.setPackShapePoints(true);
        dao.setPackStopTimes(true);
        reader.setEntityStore(dao);
        reader.run();
        for (EntityMergeStrategy strategy : strategies) {
            _log.info("strategy=" + strategy.getClass());
            GtfsMergeContext context = new GtfsMergeContext(dao, mergedDao, prefix, rawEntityIdMapsByMergeStrategy.get(strategy));
            strategy.merge(context);
        }
    }
    _log.info("writing merged output: " + outputPath);
    GtfsWriter writer = new GtfsWriter();
    writer.setOutputLocation(outputPath);
    writer.run(mergedDao);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) EntityMergeStrategy(org.onebusaway.gtfs_merge.strategies.EntityMergeStrategy) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) GtfsWriter(org.onebusaway.gtfs.serialization.GtfsWriter)

Example 3 with GtfsReader

use of org.onebusaway.gtfs.serialization.GtfsReader 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 GtfsReader

use of org.onebusaway.gtfs.serialization.GtfsReader in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsMappingTest method setup.

@Before
public void setup() throws IOException {
    Configuration config = new Configuration();
    config = config.configure("org/onebusaway/gtfs/hibernate-configuration.xml");
    _sessionFactory = config.buildSessionFactory();
    _dao = new HibernateGtfsRelationalDaoImpl(_sessionFactory);
    _dao.open();
    _reader = new GtfsReader();
    _reader.setEntityStore(_dao);
}
Also used : GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) Configuration(org.hibernate.cfg.Configuration) Before(org.junit.Before)

Example 5 with GtfsReader

use of org.onebusaway.gtfs.serialization.GtfsReader in project onebusaway-gtfs-modules by OneBusAway.

the class HibernateGtfsRelationalImplBartTest method setup.

@BeforeClass
public static void setup() throws IOException {
    Configuration config = new Configuration();
    config = config.configure("org/onebusaway/gtfs/hibernate-configuration.xml");
    _sessionFactory = config.buildSessionFactory();
    _dao = new HibernateGtfsRelationalDaoImpl(_sessionFactory);
    GtfsReader reader = new GtfsReader();
    reader.setInputLocation(new File("src/test/resources/org/onebusaway/gtfs/bart.zip"));
    reader.setEntityStore(_dao);
    reader.setDefaultAgencyId(_agencyId);
    reader.run();
}
Also used : GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) Configuration(org.hibernate.cfg.Configuration) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Aggregations

GtfsReader (org.onebusaway.gtfs.serialization.GtfsReader)16 File (java.io.File)10 Configuration (org.hibernate.cfg.Configuration)5 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)5 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)4 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)4 ArrayList (java.util.ArrayList)3 Before (org.junit.Before)3 BeforeClass (org.junit.BeforeClass)3 Stop (org.onebusaway.gtfs.model.Stop)3 Test (org.junit.Test)2 DefaultEntitySchemaFactory (org.onebusaway.csv_entities.schema.DefaultEntitySchemaFactory)2 GtfsWriter (org.onebusaway.gtfs.serialization.GtfsWriter)2 GtfsWriterTest (org.onebusaway.gtfs.serialization.GtfsWriterTest)2 HibernateGtfsFactory (org.onebusaway.gtfs.services.HibernateGtfsFactory)2 MockGtfs (org.onebusaway.gtfs.services.MockGtfs)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CommandLine (org.apache.commons.cli.CommandLine)1