Search in sources :

Example 1 with TourStrategy

use of com.graphhopper.routing.util.tour.TourStrategy in project graphhopper by graphhopper.

the class RoundTripRoutingTemplate method lookup.

@Override
public List<QueryResult> lookup(List<GHPoint> points, FlagEncoder encoder) {
    if (points.isEmpty())
        throw new IllegalStateException("For round trip calculation one point is required");
    final double distanceInMeter = ghRequest.getHints().getDouble(RoundTrip.DISTANCE, 10000);
    final long seed = ghRequest.getHints().getLong(RoundTrip.SEED, 0L);
    final double initialHeading = ghRequest.getHints().getDouble(RoundTrip.HEADING, Double.NaN);
    final int roundTripPointCount = Math.min(20, ghRequest.getHints().getInt(Algorithms.ROUND_TRIP + ".points", 2 + (int) (distanceInMeter / 50000)));
    final GHPoint start = ghRequest.getPoints().get(0);
    TourStrategy strategy = new MultiPointTour(new Random(seed), distanceInMeter, roundTripPointCount, initialHeading);
    queryResults = new ArrayList<>(2 + strategy.getNumberOfGeneratedPoints());
    EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder);
    QueryResult startQR = locationIndex.findClosest(start.lat, start.lon, edgeFilter);
    if (!startQR.isValid())
        throw new PointNotFoundException("Cannot find point 0: " + start, 0);
    queryResults.add(startQR);
    GHPoint last = points.get(0);
    for (int i = 0; i < strategy.getNumberOfGeneratedPoints(); i++) {
        double heading = strategy.getHeadingForIteration(i);
        QueryResult result = generateValidPoint(last, strategy.getDistanceForIteration(i), heading, edgeFilter);
        if (result == null) {
            ghResponse.addError(new IllegalStateException("Could not find a valid point after " + maxRetries + " tries, for the point:" + last));
            return Collections.emptyList();
        }
        last = result.getSnappedPoint();
        queryResults.add(result);
    }
    queryResults.add(startQR);
    return queryResults;
}
Also used : PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) TourStrategy(com.graphhopper.routing.util.tour.TourStrategy) GHPoint(com.graphhopper.util.shapes.GHPoint) DefaultEdgeFilter(com.graphhopper.routing.util.DefaultEdgeFilter) QueryResult(com.graphhopper.storage.index.QueryResult) MultiPointTour(com.graphhopper.routing.util.tour.MultiPointTour) Random(java.util.Random) EdgeFilter(com.graphhopper.routing.util.EdgeFilter) DefaultEdgeFilter(com.graphhopper.routing.util.DefaultEdgeFilter) GHPoint(com.graphhopper.util.shapes.GHPoint)

Aggregations

DefaultEdgeFilter (com.graphhopper.routing.util.DefaultEdgeFilter)1 EdgeFilter (com.graphhopper.routing.util.EdgeFilter)1 MultiPointTour (com.graphhopper.routing.util.tour.MultiPointTour)1 TourStrategy (com.graphhopper.routing.util.tour.TourStrategy)1 QueryResult (com.graphhopper.storage.index.QueryResult)1 PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)1 GHPoint (com.graphhopper.util.shapes.GHPoint)1 Random (java.util.Random)1