Search in sources :

Example 1 with EntityMergeStrategy

use of org.onebusaway.gtfs_merge.strategies.EntityMergeStrategy 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 2 with EntityMergeStrategy

use of org.onebusaway.gtfs_merge.strategies.EntityMergeStrategy in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsMerger method getEntityMergeStrategyForEntityType.

public EntityMergeStrategy getEntityMergeStrategyForEntityType(Class<?> entityType) {
    List<EntityMergeStrategy> strategies = new ArrayList<EntityMergeStrategy>();
    buildStrategies(strategies);
    for (EntityMergeStrategy strategy : strategies) {
        Set<Class<?>> entityTypes = new HashSet<Class<?>>();
        strategy.getEntityTypes(entityTypes);
        if (entityTypes.contains(entityType)) {
            return strategy;
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) EntityMergeStrategy(org.onebusaway.gtfs_merge.strategies.EntityMergeStrategy) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)2 EntityMergeStrategy (org.onebusaway.gtfs_merge.strategies.EntityMergeStrategy)2 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)1 GtfsReader (org.onebusaway.gtfs.serialization.GtfsReader)1 GtfsWriter (org.onebusaway.gtfs.serialization.GtfsWriter)1