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;
}
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;
}
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();
}
}
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);
}
}
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;
}
Aggregations