Search in sources :

Example 1 with PointNotFoundException

use of com.graphhopper.util.exceptions.PointNotFoundException in project graphhopper by graphhopper.

the class ViaRoutingTemplate method lookup.

@Override
public List<QueryResult> lookup(List<GHPoint> points, FlagEncoder encoder) {
    if (points.size() < 2)
        throw new IllegalArgumentException("At least 2 points have to be specified, but was:" + points.size());
    EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder);
    queryResults = new ArrayList<>(points.size());
    for (int placeIndex = 0; placeIndex < points.size(); placeIndex++) {
        GHPoint point = points.get(placeIndex);
        QueryResult res;
        if (ghRequest.hasPointHints()) {
            res = locationIndex.findClosest(point.lat, point.lon, new NameSimilarityEdgeFilter(edgeFilter, ghRequest.getPointHints().get(placeIndex)));
            if (!res.isValid()) {
                res = locationIndex.findClosest(point.lat, point.lon, edgeFilter);
            }
        } else {
            res = locationIndex.findClosest(point.lat, point.lon, edgeFilter);
        }
        if (!res.isValid())
            ghResponse.addError(new PointNotFoundException("Cannot find point " + placeIndex + ": " + point, placeIndex));
        queryResults.add(res);
    }
    return queryResults;
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 2 with PointNotFoundException

use of com.graphhopper.util.exceptions.PointNotFoundException in project graphhopper by graphhopper.

the class GraphHopperWeb method readErrors.

public static List<Throwable> readErrors(JSONObject json) {
    List<Throwable> errors = new ArrayList<>();
    JSONArray errorJson;
    if (json.has("message")) {
        if (json.has("hints")) {
            errorJson = json.getJSONArray("hints");
        } else {
            // should not happen
            errors.add(new RuntimeException(json.getString("message")));
            return errors;
        }
    } else
        return errors;
    for (int i = 0; i < errorJson.length(); i++) {
        JSONObject error = errorJson.getJSONObject(i);
        String exClass = "";
        if (error.has("details"))
            exClass = error.getString("details");
        String exMessage = error.getString("message");
        if (exClass.equals(UnsupportedOperationException.class.getName()))
            errors.add(new UnsupportedOperationException(exMessage));
        else if (exClass.equals(IllegalStateException.class.getName()))
            errors.add(new IllegalStateException(exMessage));
        else if (exClass.equals(RuntimeException.class.getName()))
            errors.add(new DetailedRuntimeException(exMessage, toMap(error)));
        else if (exClass.equals(IllegalArgumentException.class.getName()))
            errors.add(new DetailedIllegalArgumentException(exMessage, toMap(error)));
        else if (exClass.equals(ConnectionNotFoundException.class.getName())) {
            errors.add(new ConnectionNotFoundException(exMessage, toMap(error)));
        } else if (exClass.equals(PointNotFoundException.class.getName())) {
            int pointIndex = error.getInt("point_index");
            errors.add(new PointNotFoundException(exMessage, pointIndex));
        } else if (exClass.equals(PointOutOfBoundsException.class.getName())) {
            int pointIndex = error.getInt("point_index");
            errors.add(new PointOutOfBoundsException(exMessage, pointIndex));
        } else if (exClass.isEmpty())
            errors.add(new DetailedRuntimeException(exMessage, toMap(error)));
        else
            errors.add(new DetailedRuntimeException(exClass + " " + exMessage, toMap(error)));
    }
    if (json.has("message") && errors.isEmpty())
        errors.add(new RuntimeException(json.getString("message")));
    return errors;
}
Also used : PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) GHPoint(com.graphhopper.util.shapes.GHPoint) ConnectionNotFoundException(com.graphhopper.util.exceptions.ConnectionNotFoundException) DetailedRuntimeException(com.graphhopper.util.exceptions.DetailedRuntimeException) JSONObject(org.json.JSONObject) DetailedIllegalArgumentException(com.graphhopper.util.exceptions.DetailedIllegalArgumentException) PointOutOfBoundsException(com.graphhopper.util.exceptions.PointOutOfBoundsException) DetailedRuntimeException(com.graphhopper.util.exceptions.DetailedRuntimeException)

Example 3 with PointNotFoundException

use of com.graphhopper.util.exceptions.PointNotFoundException 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

PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)3 GHPoint (com.graphhopper.util.shapes.GHPoint)3 QueryResult (com.graphhopper.storage.index.QueryResult)2 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 ConnectionNotFoundException (com.graphhopper.util.exceptions.ConnectionNotFoundException)1 DetailedIllegalArgumentException (com.graphhopper.util.exceptions.DetailedIllegalArgumentException)1 DetailedRuntimeException (com.graphhopper.util.exceptions.DetailedRuntimeException)1 PointOutOfBoundsException (com.graphhopper.util.exceptions.PointOutOfBoundsException)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1