use of org.onebusaway.transit_data_federation.model.StopSequence in project onebusaway-application-modules by camsys.
the class RouteBeanServiceImpl method getShapeIdsForStopSequenceBlock.
private Set<AgencyAndId> getShapeIdsForStopSequenceBlock(StopSequenceCollection block) {
Set<AgencyAndId> shapeIds = new HashSet<AgencyAndId>();
for (StopSequence sequence : block.getStopSequences()) {
for (BlockTripEntry blockTrip : sequence.getTrips()) {
TripEntry trip = blockTrip.getTrip();
AgencyAndId shapeId = trip.getShapeId();
if (shapeId != null && shapeId.hasValues())
shapeIds.add(shapeId);
}
}
return shapeIds;
}
use of org.onebusaway.transit_data_federation.model.StopSequence in project onebusaway-application-modules by camsys.
the class RouteBeanServiceImpl method getStopsInOrder.
private List<StopEntry> getStopsInOrder(StopSequenceCollection block) {
DirectedGraph<StopEntry> graph = new DirectedGraph<StopEntry>();
for (StopSequence sequence : block.getStopSequences()) {
StopEntry prev = null;
for (StopEntry stop : sequence.getStops()) {
if (prev != null) {
// We do this to avoid cycles
if (!graph.isConnected(stop, prev))
graph.addEdge(prev, stop);
}
prev = stop;
}
}
StopGraphComparator c = new StopGraphComparator(graph);
return graph.getTopologicalSort(c);
}
use of org.onebusaway.transit_data_federation.model.StopSequence in project onebusaway-application-modules by camsys.
the class StopSequenceCollectionServiceImpl method constructCollections.
/**
* @param route
* @param sequenceStats
* @param sequencesByGroupId
* @return
*/
private List<StopSequenceCollection> constructCollections(Map<StopSequence, PatternStats> sequenceStats, Map<String, List<StopSequence>> sequencesByGroupId) {
computeContinuations(sequenceStats, sequencesByGroupId);
Set<String> allNames = new HashSet<String>();
Map<String, String> directionToName = new HashMap<String, String>();
Map<String, Segment> segments = new HashMap<String, Segment>();
for (Map.Entry<String, List<StopSequence>> entry : sequencesByGroupId.entrySet()) {
String direction = entry.getKey();
List<StopSequence> sequences = entry.getValue();
Max<StopSequence> maxTripCount = new Max<StopSequence>();
Counter<String> names = new Counter<String>();
for (StopSequence sequence : sequences) {
maxTripCount.add(sequence.getTripCount(), sequence);
for (BlockTripEntry blockTrip : sequence.getTrips()) {
TripEntry trip = blockTrip.getTrip();
TripNarrative tripNarrative = _narrativeService.getTripForId(trip.getId());
String headsign = tripNarrative.getTripHeadsign();
if (headsign != null && headsign.length() > 0)
names.increment(headsign);
}
}
String dName = names.getMax();
RecursiveStats rs = new RecursiveStats();
rs.maxTripCount = (long) maxTripCount.getMaxValue();
exploreStopSequences(rs, sequenceStats, sequences, "");
allNames.add(dName);
directionToName.put(direction, dName);
segments.put(direction, rs.longestSegment.getMaxElement());
}
if (allNames.size() < directionToName.size()) {
for (Map.Entry<String, String> entry : directionToName.entrySet()) {
String direction = entry.getKey();
String name = entry.getValue();
direction = direction.charAt(0) + direction.substring(1).toLowerCase();
entry.setValue(name + " - " + direction);
}
}
List<StopSequenceCollection> blocks = new ArrayList<StopSequenceCollection>();
for (Map.Entry<String, String> entry : directionToName.entrySet()) {
String direction = entry.getKey();
String name = entry.getValue();
List<StopSequence> patterns = sequencesByGroupId.get(direction);
Segment segment = segments.get(direction);
// System.out.println(" " + direction + " => " + name);
StopSequenceCollection block = new StopSequenceCollection();
if (segment.fromLat == 0.0)
throw new IllegalStateException("what?");
StopSequenceCollectionKey key = new StopSequenceCollectionKey(null, direction);
block.setId(key);
block.setPublicId(direction);
block.setDescription(name);
block.setStopSequences(patterns);
block.setStartLat(segment.fromLat);
block.setStartLon(segment.fromLon);
block.setEndLat(segment.toLat);
block.setEndLon(segment.toLon);
blocks.add(block);
}
return blocks;
}
use of org.onebusaway.transit_data_federation.model.StopSequence in project onebusaway-application-modules by camsys.
the class StopSequenceCollectionServiceImpl method groupStopSequencesByDirectionIds.
/**
* Group the StopSequences by their direction ids.
*
* @param sequences
* @return
*/
private Map<String, List<StopSequence>> groupStopSequencesByDirectionIds(Iterable<StopSequence> sequences) {
Map<String, List<StopSequence>> groups = new FactoryMap<String, List<StopSequence>>(new ArrayList<StopSequence>());
for (StopSequence sequence : sequences) {
String directionId = sequence.getDirectionId();
groups.get(directionId).add(sequence);
}
return groups;
}
use of org.onebusaway.transit_data_federation.model.StopSequence in project onebusaway-application-modules by camsys.
the class StopSequenceCollectionServiceImpl method computeContinuations.
/**
* For each given StopSequence, we wish to compute the set of StopSequences
* that continue the given StopSequence. We say one StopSequence continues
* another if the two stops sequences have the same route and direction id and
* each trip in the first StopSequence is immediately followed by a Trip from
* the second StopSequence, as defined by a block id.
*
* @param sequenceStats
* @param sequencesByGroupId
*/
private void computeContinuations(Map<StopSequence, PatternStats> sequenceStats, Map<String, List<StopSequence>> sequencesByGroupId) {
Map<BlockTripEntry, StopSequence> stopSequencesByTrip = new HashMap<BlockTripEntry, StopSequence>();
Map<StopSequence, String> stopSequenceGroupIds = new HashMap<StopSequence, String>();
for (Map.Entry<String, List<StopSequence>> entry : sequencesByGroupId.entrySet()) {
String id = entry.getKey();
for (StopSequence sequence : entry.getValue()) stopSequenceGroupIds.put(sequence, id);
}
for (StopSequence sequence : sequenceStats.keySet()) {
String groupId = stopSequenceGroupIds.get(sequence);
for (BlockTripEntry trip : sequence.getTrips()) {
BlockTripEntry prevTrip = trip.getPreviousTrip();
if (prevTrip == null)
continue;
StopSequence prevSequence = stopSequencesByTrip.get(prevTrip);
// No continuations if incoming is not part of the sequence collection
if (prevSequence == null)
continue;
// No continuation if it's the same stop sequence
if (prevSequence.equals(sequence))
continue;
// No contination if the the block group ids don't match
String prevGroupId = stopSequenceGroupIds.get(prevSequence);
if (!groupId.equals(prevGroupId))
continue;
StopEntry prevStop = prevSequence.getStops().get(prevSequence.getStops().size() - 1);
StopEntry nextStop = sequence.getStops().get(0);
double d = SphericalGeometryLibrary.distance(prevStop.getStopLat(), prevStop.getStopLon(), nextStop.getStopLat(), nextStop.getStopLon());
if (d < 5280 / 4) {
/*
* System.out.println("distance=" + d + " from=" + prevStop.getId() +
* " to=" + nextStop.getId() + " ssFrom=" + prevSequence.getId() +
* " ssTo=" + stopSequence.getId());
*/
PatternStats stats = sequenceStats.get(prevSequence);
stats.continuations.add(sequence);
}
}
}
}
Aggregations