Search in sources :

Example 1 with Sentry

use of org.onebusaway.utility.collections.TreeUnionFind.Sentry in project onebusaway-application-modules by camsys.

the class GtfsStopsForMergingMain method main.

public static void main(String[] args) throws IOException, ParseException {
    Options options = new Options();
    options.addOption(ARG_DISTANCE, true, "");
    options.addOption(ARG_CONSOLIDATED, true, "");
    Parser parser = new GnuParser();
    CommandLine cli = parser.parse(options, args);
    args = cli.getArgs();
    if (args.length < 5) {
        usage();
        System.exit(-1);
    }
    double distanceThreshold = 20;
    if (cli.hasOption(ARG_DISTANCE))
        distanceThreshold = Double.parseDouble(cli.getOptionValue(ARG_DISTANCE));
    TreeUnionFind<AgencyAndId> existingConsolidatedStops = new TreeUnionFind<AgencyAndId>();
    if (cli.hasOption(ARG_CONSOLIDATED))
        existingConsolidatedStops = loadExistingConsolidatedStops(cli.getOptionValue(ARG_CONSOLIDATED));
    PrintWriter writer = getOutputAsWriter(args[args.length - 1]);
    TreeUnionFind<AgencyAndId> union = new TreeUnionFind<AgencyAndId>();
    List<Collection<Stop>> allStops = new ArrayList<Collection<Stop>>();
    for (int i = 0; i < args.length - 1; i += 2) {
        String path = args[i];
        String agencyId = args[i + 1];
        Collection<Stop> stops = readStopsFromGtfsPath(path, agencyId);
        for (Collection<Stop> previousStops : allStops) {
            for (Stop stopA : previousStops) {
                for (Stop stopB : stops) {
                    double d = SphericalGeometryLibrary.distance(stopA.getLat(), stopA.getLon(), stopB.getLat(), stopB.getLon());
                    if (d < distanceThreshold && !existingConsolidatedStops.isSameSet(stopA.getId(), stopB.getId())) {
                        union.union(stopA.getId(), stopB.getId());
                    }
                }
            }
        }
        allStops.add(stops);
    }
    for (Set<AgencyAndId> set : union.getSetMembers()) {
        Set<Sentry> existing = new HashSet<Sentry>();
        boolean first = true;
        for (AgencyAndId stopId : set) {
            if (first)
                first = false;
            else
                writer.print(' ');
            if (existingConsolidatedStops.contains(stopId))
                existing.add(existingConsolidatedStops.find(stopId));
            writer.print(AgencyAndIdLibrary.convertToString(stopId));
        }
        writer.println();
        for (Sentry sentry : existing) writer.println("  => " + existingConsolidatedStops.members(sentry));
        writer.flush();
    }
}
Also used : Options(org.apache.commons.cli.Options) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TreeUnionFind(org.onebusaway.utility.collections.TreeUnionFind) Stop(org.onebusaway.gtfs.model.Stop) GnuParser(org.apache.commons.cli.GnuParser) ArrayList(java.util.ArrayList) GnuParser(org.apache.commons.cli.GnuParser) Parser(org.apache.commons.cli.Parser) CommandLine(org.apache.commons.cli.CommandLine) Collection(java.util.Collection) Sentry(org.onebusaway.utility.collections.TreeUnionFind.Sentry) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Example 2 with Sentry

use of org.onebusaway.utility.collections.TreeUnionFind.Sentry in project onebusaway-application-modules by camsys.

the class StopConsolidationSuggestionsTask method run.

@Override
public void run() {
    try {
        double distanceThreshold = 20;
        _log.info("begin stop consolidation suggestions with distanceThreshold=" + distanceThreshold);
        TreeUnionFind<AgencyAndId> existingConsolidatedStops = new TreeUnionFind<AgencyAndId>();
        existingConsolidatedStops = loadExistingConsolidatedStops(CONSOLIDATED_URL);
        TreeUnionFind<AgencyAndId> union = new TreeUnionFind<AgencyAndId>();
        List<Collection<Stop>> allStops = new ArrayList<Collection<Stop>>();
        for (Agency agency : _dao.getAllAgencies()) {
            Collection<Stop> stops = getStopsForAgency(_dao, agency);
            for (Collection<Stop> previousStops : allStops) {
                for (Stop stopA : previousStops) {
                    for (Stop stopB : stops) {
                        double d = SphericalGeometryLibrary.distance(stopA.getLat(), stopA.getLon(), stopB.getLat(), stopB.getLon());
                        if (d < distanceThreshold && !existingConsolidatedStops.isSameSet(stopA.getId(), stopB.getId())) {
                            union.union(stopA.getId(), stopB.getId());
                        }
                    }
                }
            }
            allStops.add(stops);
        }
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
        _logger.header("stop_consolidation_suggestions.csv", "new_stops,existing_stops");
        for (Set<AgencyAndId> set : union.getSetMembers()) {
            StringBuffer newStopBuffer = new StringBuffer();
            StringBuffer existingStopBuffer = new StringBuffer();
            Set<Sentry> existing = new HashSet<Sentry>();
            boolean first = true;
            for (AgencyAndId stopId : set) {
                if (first)
                    first = false;
                else {
                    writer.print(' ');
                    newStopBuffer.append(" ");
                }
                if (existingConsolidatedStops.contains(stopId)) {
                    existing.add(existingConsolidatedStops.find(stopId));
                }
                writer.print(AgencyAndIdLibrary.convertToString(stopId));
                newStopBuffer.append(AgencyAndIdLibrary.convertToString(stopId));
            }
            writer.println();
            for (Sentry sentry : existing) {
                writer.println("  => " + existingConsolidatedStops.members(sentry));
                existingStopBuffer.append(existingConsolidatedStops.members(sentry));
            }
            _logger.log("stop_consolidation_suggestions.csv", newStopBuffer.toString(), existingStopBuffer.toString());
            writer.flush();
        }
        _log.info("end stop consolidation suggestions");
    } catch (Exception any) {
        _log.error("exception:", any);
    }
}
Also used : Agency(org.onebusaway.gtfs.model.Agency) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TreeUnionFind(org.onebusaway.utility.collections.TreeUnionFind) Stop(org.onebusaway.gtfs.model.Stop) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Collection(java.util.Collection) OutputStreamWriter(java.io.OutputStreamWriter) Sentry(org.onebusaway.utility.collections.TreeUnionFind.Sentry) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Aggregations

PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 Stop (org.onebusaway.gtfs.model.Stop)2 TreeUnionFind (org.onebusaway.utility.collections.TreeUnionFind)2 Sentry (org.onebusaway.utility.collections.TreeUnionFind.Sentry)2 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 CommandLine (org.apache.commons.cli.CommandLine)1 GnuParser (org.apache.commons.cli.GnuParser)1 Options (org.apache.commons.cli.Options)1 Parser (org.apache.commons.cli.Parser)1 Agency (org.onebusaway.gtfs.model.Agency)1