Search in sources :

Example 16 with GtfsBundle

use of org.onebusaway.transit_data_federation.bundle.model.GtfsBundle in project onebusaway-application-modules by camsys.

the class GtfsMergeTask method run.

public void run() {
    if (!requestResponse.getRequest().getConsolidateFlag()) {
        _log.info("consolidate flag not set, extiting");
        return;
    }
    _log.info("GtfsMergeTask Starting with outputDirectory=" + getOutputDirectory());
    try {
        _log.info("Started merging modified GTFS feeds.");
        GtfsBundles gtfsBundles = getGtfsBundles(_applicationContext);
        List<File> inputPaths = new ArrayList<File>();
        // note this will be overridden if properly configured
        String outputLocation = System.getProperty("java.io.tmpdir") + File.separator + "gtfs_puget_sound_consolidated.zip";
        if (getOutputDirectory() != null) {
            String consolidatedPath = getOutputDirectory() + File.separator + CONSOLIDATED_DIR;
            File consolidatedDir = new File(consolidatedPath);
            consolidatedDir.mkdirs();
            outputLocation = consolidatedPath + File.separator + "gtfs_puget_sound_consolidated.zip";
        }
        _log.info("Consolidated file output location: " + outputLocation);
        int i = 0;
        for (GtfsBundle gtfsBundle : gtfsBundles.getBundles()) {
            if (gtfsBundle.getPath() != null) {
                _log.info("addiing agency data file path for agency[" + i + "]=" + gtfsBundle.getPath());
                inputPaths.add(gtfsBundle.getPath());
            } else {
                _log.info("null file path for agency.");
            }
        }
        // Now call GTFS merger
        GtfsMerger feedMerger = new GtfsMerger();
        AgencyMergeStrategy agencyStrategy = new AgencyMergeStrategy();
        // agencies aren't duplicates, its by design
        agencyStrategy.setDuplicateDetectionStrategy(EDuplicateDetectionStrategy.FUZZY);
        feedMerger.setAgencyStrategy(agencyStrategy);
        StopMergeStrategy stopStrategy = new StopMergeStrategy();
        stopStrategy.setDuplicateRenamingStrategy(EDuplicateRenamingStrategy.AGENCY);
        feedMerger.setStopStrategy(stopStrategy);
        RouteMergeStrategy routeStrategy = new RouteMergeStrategy();
        routeStrategy.setDuplicateRenamingStrategy(EDuplicateRenamingStrategy.AGENCY);
        feedMerger.setRouteStrategy(routeStrategy);
        ServiceCalendarMergeStrategy serviceCalendarStrategy = new ServiceCalendarMergeStrategy();
        serviceCalendarStrategy.setDuplicateRenamingStrategy(EDuplicateRenamingStrategy.AGENCY);
        feedMerger.setServiceCalendarStrategy(serviceCalendarStrategy);
        TripMergeStrategy tripStrategy = new TripMergeStrategy();
        tripStrategy.setDuplicateRenamingStrategy(EDuplicateRenamingStrategy.AGENCY);
        feedMerger.setTripStrategy(tripStrategy);
        File outputFile = new File(outputLocation);
        outputFile.createNewFile();
        feedMerger.run(inputPaths, new File(outputLocation));
    } catch (Throwable ex) {
        _log.error("Error merging gtfs:", ex);
    } finally {
        _log.info("GtfsMergeTask Exiting");
    }
}
Also used : TripMergeStrategy(org.onebusaway.gtfs_merge.strategies.TripMergeStrategy) AgencyMergeStrategy(org.onebusaway.gtfs_merge.strategies.AgencyMergeStrategy) RouteMergeStrategy(org.onebusaway.gtfs_merge.strategies.RouteMergeStrategy) GtfsBundles(org.onebusaway.transit_data_federation.bundle.model.GtfsBundles) ArrayList(java.util.ArrayList) StopMergeStrategy(org.onebusaway.gtfs_merge.strategies.StopMergeStrategy) ServiceCalendarMergeStrategy(org.onebusaway.gtfs_merge.strategies.ServiceCalendarMergeStrategy) GtfsBundle(org.onebusaway.transit_data_federation.bundle.model.GtfsBundle) GtfsMerger(org.onebusaway.gtfs_merge.GtfsMerger) File(java.io.File)

Example 17 with GtfsBundle

use of org.onebusaway.transit_data_federation.bundle.model.GtfsBundle in project onebusaway-application-modules by camsys.

the class GtfsModTask method run.

@Override
public void run() {
    try {
        _log.info("GtfsModTask Starting");
        GtfsBundles gtfsBundles = getGtfsBundles(_applicationContext);
        for (GtfsBundle gtfsBundle : gtfsBundles.getBundles()) {
            String agencyId = parseAgencyDir(gtfsBundle.getPath().getPath());
            if (agencyId != null) {
                // lookup meta info for agency
                String modUrl = getModUrl(agencyId);
                _log.info("using modUrl=" + modUrl + " for agency " + agencyId + " and bundle " + gtfsBundle.getPath());
                if (modUrl != null) {
                    // run the mod script on this gtfsBundle
                    String oldFilename = gtfsBundle.getPath().getPath();
                    String transform = getTransform(agencyId, gtfsBundle.getPath().toString());
                    String newFilename = runModifications(gtfsBundle, agencyId, modUrl, transform);
                    logger.changelog("Transformed " + oldFilename + " to " + newFilename + " according to url " + getModUrl(agencyId));
                } else {
                    _log.info("no modUrl found for agency " + agencyId + " and bundle " + gtfsBundle.getPath());
                }
            }
        }
    } catch (Throwable ex) {
        _log.error("error modifying gtfs:", ex);
    } finally {
        _log.info("GtfsModTask Exiting");
    }
}
Also used : GtfsBundle(org.onebusaway.transit_data_federation.bundle.model.GtfsBundle) GtfsBundles(org.onebusaway.transit_data_federation.bundle.model.GtfsBundles)

Example 18 with GtfsBundle

use of org.onebusaway.transit_data_federation.bundle.model.GtfsBundle in project onebusaway-application-modules by camsys.

the class MissingBlockForTrips method run.

@Override
public void run() {
    _log.info("MissingBlockForTrips Task Starting (v2)");
    Set<String> linkTrips = new HashSet<String>();
    Set<String> linkBlocks = new HashSet<String>();
    Set<String> missingBlocks = new HashSet<String>();
    List<String> unlinkedTrips = new ArrayList<String>();
    Map<String, String> linkTripEntries = new HashMap<String, String>();
    GtfsBundles gtfsBundles = getGtfsBundles(_applicationContext);
    for (GtfsBundle gtfsBundle : gtfsBundles.getBundles()) {
        String gtfsFilePath = gtfsBundle.getPath().toString();
        if (gtfsBundle.getAgencyIdMappings().containsKey(LINK_AGENCY)) {
            CSVData blockCsvData = getCSVData(gtfsFilePath, GTFS_BLOCK);
            String blockHeaders = blockCsvData.getHeader();
            List<String> blockRows = blockCsvData.getRows();
            int routeIndex = getIndexForValue(blockHeaders, "block_route_num");
            int blockSeqIndex = getIndexForValue(blockHeaders, "block_seq_num");
            for (String row : blockRows) {
                String[] cols = row.split(",");
                if (LINK_ROUTE.equals(cols[routeIndex])) {
                    String linkBlock = cols[blockSeqIndex].trim();
                    linkBlocks.add(linkBlock);
                }
            }
            CSVData tripCsvData = getCSVData(gtfsFilePath, GTFS_TRIPS);
            String tripHeaders = tripCsvData.getHeader();
            List<String> tripRows = tripCsvData.getRows();
            int blockIndex = getIndexForValue(tripHeaders, "block_id");
            int routeIdIndex = getIndexForValue(tripHeaders, "route_id");
            int tripIndex = getIndexForValue(tripHeaders, "trip_id");
            for (String row : tripRows) {
                String[] cols = row.split(",");
                String tripBlock = cols[blockIndex].trim();
                String routeId = cols[routeIdIndex].trim();
                String tripId = cols[tripIndex];
                if (LINK_ROUTE_ID.equals(routeId)) {
                    linkTrips.add(tripId);
                    _log.info("adding LINK block=" + tripBlock + " for trip= " + tripId);
                    linkTripEntries.put(tripBlock, row);
                    if (linkBlocks.contains(tripBlock)) {
                        _log.info("found block " + tripBlock + " for trip= " + tripId);
                    } else {
                        _log.info("missing block " + tripBlock + " for trip= " + tripId);
                        missingBlocks.add(tripBlock);
                        unlinkedTrips.add(row);
                    }
                }
            }
            _log.info(linkTrips.size() + " trips found in trips.txt for LINK data with " + unlinkedTrips.size() + " unmatched.  " + missingBlocks.size() + " blocks not matched.");
        }
    }
    // Write out results to .csv file
    _logger.header(FILENAME_TRIP, "route_id,service_id,trip_id,trip_headsign,trip_short_name,direction_id,block_id,shape_id,peak_flag,fare_id");
    for (String trip : unlinkedTrips) {
        _logger.logCSV(FILENAME_TRIP, trip);
    }
    _logger.header(FILENAME_BLOCK, "block_id");
    for (String block : missingBlocks) {
        _logger.logCSV(FILENAME_BLOCK, block);
    }
    _log.info("MissingBlockForTrips Task Exiting");
}
Also used : GtfsBundle(org.onebusaway.transit_data_federation.bundle.model.GtfsBundle) GtfsBundles(org.onebusaway.transit_data_federation.bundle.model.GtfsBundles)

Example 19 with GtfsBundle

use of org.onebusaway.transit_data_federation.bundle.model.GtfsBundle in project onebusaway-application-modules by camsys.

the class MissingTripsForBlock method run.

@Override
public void run() {
    _log.info("MissingTripsForBlock Task Starting");
    Set<String> linkBlocks = new HashSet<String>();
    Map<String, String> linkBlockEntries = new HashMap<String, String>();
    GtfsBundles gtfsBundles = getGtfsBundles(_applicationContext);
    for (GtfsBundle gtfsBundle : gtfsBundles.getBundles()) {
        String gtfsFilePath = gtfsBundle.getPath().toString();
        if (gtfsBundle.getAgencyIdMappings().containsKey(LINK_AGENCY)) {
            CSVData blockCsvData = getCSVData(gtfsFilePath, GTFS_BLOCK);
            String blockHeaders = blockCsvData.getHeader();
            List<String> blockRows = blockCsvData.getRows();
            int blockSeqIndex = getIndexForValue(blockHeaders, "block_seq_num");
            int routeIndex = getIndexForValue(blockHeaders, "block_route_num");
            for (String row : blockRows) {
                String[] cols = row.split(",");
                if (LINK_ROUTE.equals(cols[routeIndex])) {
                    linkBlocks.add(cols[blockSeqIndex]);
                    linkBlockEntries.put(cols[blockSeqIndex], row);
                }
            }
            _log.info(linkBlocks.size() + " blocks found in blocks.txt");
            CSVData tripCsvData = getCSVData(gtfsFilePath, GTFS_TRIPS);
            String tripHeaders = tripCsvData.getHeader();
            List<String> tripRows = tripCsvData.getRows();
            int blockIndex = getIndexForValue(tripHeaders, "block_id");
            int routeIdIndex = getIndexForValue(tripHeaders, "route_id");
            int tripCount = 0;
            for (String row : tripRows) {
                String[] cols = row.split(",");
                String tripBlock = cols[blockIndex];
                if (linkBlocks.contains(tripBlock)) {
                    linkBlocks.remove(tripBlock);
                }
            }
            _log.info(linkBlocks.size() + " unmatched blocks!");
        }
    }
    // Write out results to .csv file
    _logger.header(FILENAME, "block_seq_num,block_var_num,block_route_num,block_run_num");
    for (String block : linkBlocks) {
        _logger.logCSV(FILENAME, linkBlockEntries.get(block));
    }
    _log.info("MissingTripsForBlock Task Exiting");
}
Also used : HashMap(java.util.HashMap) GtfsBundle(org.onebusaway.transit_data_federation.bundle.model.GtfsBundle) GtfsBundles(org.onebusaway.transit_data_federation.bundle.model.GtfsBundles) HashSet(java.util.HashSet)

Aggregations

GtfsBundle (org.onebusaway.transit_data_federation.bundle.model.GtfsBundle)19 GtfsBundles (org.onebusaway.transit_data_federation.bundle.model.GtfsBundles)13 ArrayList (java.util.ArrayList)7 File (java.io.File)6 GtfsReader (org.onebusaway.gtfs.serialization.GtfsReader)4 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 Map (java.util.Map)2 Properties (java.util.Properties)2 ZipFile (java.util.zip.ZipFile)2 CommandLine (org.apache.commons.cli.CommandLine)2 GnuParser (org.apache.commons.cli.GnuParser)2 Options (org.apache.commons.cli.Options)2 ParseException (org.apache.commons.cli.ParseException)2 Parser (org.apache.commons.cli.Parser)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 LineString (com.vividsolutions.jts.geom.LineString)1 BufferedReader (java.io.BufferedReader)1