Search in sources :

Example 1 with TreeUnionFind

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

the class StopConsolidationSuggestionsTask method loadExistingConsolidatedStops.

private TreeUnionFind<AgencyAndId> loadExistingConsolidatedStops(String path) throws IOException {
    InputStream in = IOLibrary.getPathAsInputStream(path);
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    TreeUnionFind<AgencyAndId> consolidated = new TreeUnionFind<AgencyAndId>();
    String line = null;
    while ((line = reader.readLine()) != null) {
        if (line.startsWith("#") || line.startsWith("{{{") || line.startsWith("}}}") || line.length() == 0)
            continue;
        String[] tokens = line.split("\\s+");
        if (tokens == null || tokens.length == 0)
            continue;
        AgencyAndId stopId = AgencyAndIdLibrary.convertFromString(tokens[0]);
        for (int i = 1; i < tokens.length; i++) {
            AgencyAndId otherStopId = AgencyAndIdLibrary.convertFromString(tokens[i]);
            consolidated.union(stopId, otherStopId);
        }
    }
    reader.close();
    return consolidated;
}
Also used : InputStreamReader(java.io.InputStreamReader) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TreeUnionFind(org.onebusaway.utility.collections.TreeUnionFind) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader)

Example 2 with TreeUnionFind

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

the class GtfsStopsForMergingMain method loadExistingConsolidatedStops.

private static TreeUnionFind<AgencyAndId> loadExistingConsolidatedStops(String path) throws IOException {
    InputStream in = IOLibrary.getPathAsInputStream(path);
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    TreeUnionFind<AgencyAndId> consolidated = new TreeUnionFind<AgencyAndId>();
    String line = null;
    while ((line = reader.readLine()) != null) {
        if (line.startsWith("#") || line.startsWith("{{{") || line.startsWith("}}}") || line.length() == 0)
            continue;
        String[] tokens = line.split("\\s+");
        AgencyAndId stopId = AgencyAndIdLibrary.convertFromString(tokens[0]);
        for (int i = 1; i < tokens.length; i++) {
            AgencyAndId otherStopId = AgencyAndIdLibrary.convertFromString(tokens[i]);
            consolidated.union(stopId, otherStopId);
        }
    }
    reader.close();
    return consolidated;
}
Also used : InputStreamReader(java.io.InputStreamReader) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TreeUnionFind(org.onebusaway.utility.collections.TreeUnionFind) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader)

Example 3 with TreeUnionFind

use of org.onebusaway.utility.collections.TreeUnionFind 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 4 with TreeUnionFind

use of org.onebusaway.utility.collections.TreeUnionFind 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)

Example 5 with TreeUnionFind

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

the class StopSequenceCollectionServiceImpl method groupStopSequencesByNotDirectionIds.

private Map<String, List<StopSequence>> groupStopSequencesByNotDirectionIds(Iterable<StopSequence> sequences) {
    TreeUnionFind<StopSequence> unionFind = new TreeUnionFind<StopSequence>();
    for (StopSequence stopSequenceA : sequences) {
        unionFind.find(stopSequenceA);
        for (StopSequence stopSequenceB : sequences) {
            if (stopSequenceA == stopSequenceB)
                continue;
            double ratio = getMaxCommonStopSequenceRatio(stopSequenceA, stopSequenceB);
            if (ratio >= STOP_SEQUENCE_MIN_COMMON_RATIO)
                unionFind.union(stopSequenceA, stopSequenceB);
        }
    }
    Map<String, List<StopSequence>> results = new HashMap<String, List<StopSequence>>();
    int index = 0;
    for (Set<StopSequence> sequencesByDirection : unionFind.getSetMembers()) {
        String key = Integer.toString(index);
        List<StopSequence> asList = new ArrayList<StopSequence>(sequencesByDirection);
        results.put(key, asList);
        index++;
    }
    return results;
}
Also used : TreeUnionFind(org.onebusaway.utility.collections.TreeUnionFind) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StopSequence(org.onebusaway.transit_data_federation.model.StopSequence)

Aggregations

TreeUnionFind (org.onebusaway.utility.collections.TreeUnionFind)5 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)4 ArrayList (java.util.ArrayList)3 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 PrintWriter (java.io.PrintWriter)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Stop (org.onebusaway.gtfs.model.Stop)2 Sentry (org.onebusaway.utility.collections.TreeUnionFind.Sentry)2 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HashMap (java.util.HashMap)1 List (java.util.List)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