Search in sources :

Example 1 with StopLocation

use of org.opentripplanner.model.StopLocation in project OpenTripPlanner by opentripplanner.

the class NearbyStopFinder method findNearbyStopsConsideringPatterns.

/**
 * Find all unique nearby stops that are the closest stop on some trip pattern or flex trip.
 * Note that the result will include the origin vertex if it is an instance of StopVertex.
 * This is intentional: we don't want to return the next stop down the line for trip patterns that pass through the
 * origin vertex.
 */
public Set<StopAtDistance> findNearbyStopsConsideringPatterns(Vertex vertex, boolean reverseDirection) {
    /* Track the closest stop on each pattern passing nearby. */
    MinMap<TripPattern, StopAtDistance> closestStopForPattern = new MinMap<TripPattern, StopAtDistance>();
    /* Track the closest stop on each flex trip nearby. */
    MinMap<FlexTrip, StopAtDistance> closestStopForFlexTrip = new MinMap<>();
    /* Iterate over nearby stops via the street network or using straight-line distance, depending on the graph. */
    for (StopAtDistance stopAtDistance : findNearbyStops(vertex, reverseDirection)) {
        StopLocation ts1 = stopAtDistance.stop;
        if (ts1 instanceof Stop) {
            /* Consider this destination stop as a candidate for every trip pattern passing through it. */
            for (TripPattern pattern : graph.index.getPatternsForStop(ts1)) {
                closestStopForPattern.putMin(pattern, stopAtDistance);
            }
        }
        if (OTPFeature.FlexRouting.isOn()) {
            for (FlexTrip trip : graph.index.getFlexIndex().flexTripsByStop.get(ts1)) {
                closestStopForFlexTrip.putMin(trip, stopAtDistance);
            }
        }
    }
    /* Make a transfer from the origin stop to each destination stop that was the closest stop on any pattern. */
    Set<StopAtDistance> uniqueStops = Sets.newHashSet();
    uniqueStops.addAll(closestStopForFlexTrip.values());
    uniqueStops.addAll(closestStopForPattern.values());
    return uniqueStops;
}
Also used : FlexTrip(org.opentripplanner.ext.flex.trip.FlexTrip) MinMap(org.opentripplanner.common.MinMap) Stop(org.opentripplanner.model.Stop) StopAtDistance(org.opentripplanner.routing.graphfinder.StopAtDistance) StopLocation(org.opentripplanner.model.StopLocation) FlexStopLocation(org.opentripplanner.model.FlexStopLocation) TripPattern(org.opentripplanner.model.TripPattern)

Example 2 with StopLocation

use of org.opentripplanner.model.StopLocation in project OpenTripPlanner by opentripplanner.

the class GeometryAndBlockProcessor method getLinearLocations.

private List<LinearLocation> getLinearLocations(List<StopTime> stopTimes, LineString shape) {
    // This trip does not have shape_dist in stop_times, but does have an associated shape.
    ArrayList<IndexedLineSegment> segments = new ArrayList<>();
    for (int i = 0; i < shape.getNumPoints() - 1; ++i) {
        segments.add(new IndexedLineSegment(i, shape.getCoordinateN(i), shape.getCoordinateN(i + 1)));
    }
    // Find possible segment matches for each stop.
    List<List<IndexedLineSegment>> possibleSegmentsForStop = new ArrayList<>();
    int minSegmentIndex = 0;
    for (int i = 0; i < stopTimes.size(); ++i) {
        StopLocation stop = stopTimes.get(i).getStop();
        Coordinate coord = stop.getCoordinate().asJtsCoordinate();
        List<IndexedLineSegment> stopSegments = new ArrayList<>();
        double bestDistance = Double.MAX_VALUE;
        IndexedLineSegment bestSegment = null;
        int maxSegmentIndex = -1;
        int index = -1;
        int minSegmentIndexForThisStop = -1;
        for (IndexedLineSegment segment : segments) {
            index++;
            if (segment.index < minSegmentIndex) {
                continue;
            }
            double distance = segment.distance(coord);
            if (distance < maxStopToShapeSnapDistance) {
                stopSegments.add(segment);
                maxSegmentIndex = index;
                if (minSegmentIndexForThisStop == -1)
                    minSegmentIndexForThisStop = index;
            } else if (distance < bestDistance) {
                bestDistance = distance;
                bestSegment = segment;
                if (maxSegmentIndex != -1) {
                    maxSegmentIndex = index;
                }
            }
        }
        if (stopSegments.size() == 0 && bestSegment != null) {
            // no segments within 150m
            // fall back to nearest segment
            stopSegments.add(bestSegment);
            minSegmentIndex = bestSegment.index;
        } else {
            minSegmentIndex = minSegmentIndexForThisStop;
            stopSegments.sort(new IndexedLineSegmentComparator(coord));
        }
        for (int j = i - 1; j >= 0; j--) {
            for (Iterator<IndexedLineSegment> it = possibleSegmentsForStop.get(j).iterator(); it.hasNext(); ) {
                IndexedLineSegment segment = it.next();
                if (segment.index > maxSegmentIndex) {
                    it.remove();
                }
            }
        }
        possibleSegmentsForStop.add(stopSegments);
    }
    return getStopLocations(possibleSegmentsForStop, stopTimes, 0, -1);
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) StopLocation(org.opentripplanner.model.StopLocation) ShapePoint(org.opentripplanner.model.ShapePoint)

Example 3 with StopLocation

use of org.opentripplanner.model.StopLocation in project OpenTripPlanner by opentripplanner.

the class GeometryAndBlockProcessor method getStopLocations.

/**
 * Find a consistent, increasing list of LinearLocations along a shape for a set of stops.
 * Handles loops routes.
 */
private List<LinearLocation> getStopLocations(List<List<IndexedLineSegment>> possibleSegmentsForStop, List<StopTime> stopTimes, int index, int prevSegmentIndex) {
    if (index == stopTimes.size()) {
        return new LinkedList<>();
    }
    StopTime st = stopTimes.get(index);
    StopLocation stop = st.getStop();
    Coordinate stopCoord = stop.getCoordinate().asJtsCoordinate();
    for (IndexedLineSegment segment : possibleSegmentsForStop.get(index)) {
        if (segment.index < prevSegmentIndex) {
            // can't go backwards along line
            continue;
        }
        List<LinearLocation> locations = getStopLocations(possibleSegmentsForStop, stopTimes, index + 1, segment.index);
        if (locations != null) {
            LinearLocation location = new LinearLocation(0, segment.index, segment.fraction(stopCoord));
            locations.add(0, location);
            // we found one!
            return locations;
        }
    }
    return null;
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) LinearLocation(org.locationtech.jts.linearref.LinearLocation) StopLocation(org.opentripplanner.model.StopLocation) LinkedList(java.util.LinkedList) StopTime(org.opentripplanner.model.StopTime)

Aggregations

StopLocation (org.opentripplanner.model.StopLocation)3 LinkedList (java.util.LinkedList)2 Coordinate (org.locationtech.jts.geom.Coordinate)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 LinearLocation (org.locationtech.jts.linearref.LinearLocation)1 MinMap (org.opentripplanner.common.MinMap)1 FlexTrip (org.opentripplanner.ext.flex.trip.FlexTrip)1 FlexStopLocation (org.opentripplanner.model.FlexStopLocation)1 ShapePoint (org.opentripplanner.model.ShapePoint)1 Stop (org.opentripplanner.model.Stop)1 StopTime (org.opentripplanner.model.StopTime)1 TripPattern (org.opentripplanner.model.TripPattern)1 StopAtDistance (org.opentripplanner.routing.graphfinder.StopAtDistance)1