Search in sources :

Example 1 with StopSequence

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;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) StopSequence(org.onebusaway.transit_data_federation.model.StopSequence) HashSet(java.util.HashSet)

Example 2 with StopSequence

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);
}
Also used : DirectedGraph(org.onebusaway.transit_data_federation.impl.DirectedGraph) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) StopGraphComparator(org.onebusaway.transit_data_federation.impl.StopGraphComparator) StopSequence(org.onebusaway.transit_data_federation.model.StopSequence)

Example 3 with StopSequence

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;
}
Also used : HashMap(java.util.HashMap) Max(org.onebusaway.collections.Max) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ArrayList(java.util.ArrayList) StopSequence(org.onebusaway.transit_data_federation.model.StopSequence) Counter(org.onebusaway.collections.Counter) ArrayList(java.util.ArrayList) List(java.util.List) StopSequenceCollection(org.onebusaway.transit_data_federation.model.StopSequenceCollection) HashSet(java.util.HashSet) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) StopSequenceCollectionKey(org.onebusaway.transit_data_federation.model.StopSequenceCollectionKey) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) TripNarrative(org.onebusaway.transit_data_federation.model.narrative.TripNarrative)

Example 4 with StopSequence

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;
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) ArrayList(java.util.ArrayList) List(java.util.List) StopSequence(org.onebusaway.transit_data_federation.model.StopSequence)

Example 5 with StopSequence

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);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ArrayList(java.util.ArrayList) List(java.util.List) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) StopSequence(org.onebusaway.transit_data_federation.model.StopSequence)

Aggregations

StopSequence (org.onebusaway.transit_data_federation.model.StopSequence)10 ArrayList (java.util.ArrayList)6 List (java.util.List)5 BlockTripEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)5 HashMap (java.util.HashMap)4 FactoryMap (org.onebusaway.collections.FactoryMap)4 StopEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopEntry)4 Map (java.util.Map)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 TripEntry (org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)3 HashSet (java.util.HashSet)2 StopSequenceCollection (org.onebusaway.transit_data_federation.model.StopSequenceCollection)2 Counter (org.onebusaway.collections.Counter)1 Max (org.onebusaway.collections.Max)1 EncodedPolylineBean (org.onebusaway.geospatial.model.EncodedPolylineBean)1 NameBean (org.onebusaway.transit_data.model.NameBean)1 StopGroupBean (org.onebusaway.transit_data.model.StopGroupBean)1 StopGroupingBean (org.onebusaway.transit_data.model.StopGroupingBean)1 StopsForRouteBean (org.onebusaway.transit_data.model.StopsForRouteBean)1 DirectedGraph (org.onebusaway.transit_data_federation.impl.DirectedGraph)1