Search in sources :

Example 1 with ConnectionNotFoundException

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

the class PathMerger method doWork.

public void doWork(PathWrapper altRsp, List<Path> paths, Translation tr) {
    int origPoints = 0;
    long fullTimeInMillis = 0;
    double fullWeight = 0;
    double fullDistance = 0;
    boolean allFound = true;
    InstructionList fullInstructions = new InstructionList(tr);
    PointList fullPoints = PointList.EMPTY;
    List<String> description = new ArrayList<String>();
    for (int pathIndex = 0; pathIndex < paths.size(); pathIndex++) {
        Path path = paths.get(pathIndex);
        description.addAll(path.getDescription());
        fullTimeInMillis += path.getTime();
        fullDistance += path.getDistance();
        fullWeight += path.getWeight();
        if (enableInstructions) {
            InstructionList il = path.calcInstructions(tr);
            if (!il.isEmpty()) {
                if (fullPoints.isEmpty()) {
                    PointList pl = il.get(0).getPoints();
                    // do a wild guess about the total number of points to avoid reallocation a bit
                    fullPoints = new PointList(il.size() * Math.min(10, pl.size()), pl.is3D());
                }
                for (Instruction i : il) {
                    if (simplifyResponse) {
                        origPoints += i.getPoints().size();
                        douglasPeucker.simplify(i.getPoints());
                    }
                    fullInstructions.add(i);
                    fullPoints.add(i.getPoints());
                }
                // if not yet reached finish replace with 'reached via'
                if (pathIndex + 1 < paths.size()) {
                    ViaInstruction newInstr = new ViaInstruction(fullInstructions.get(fullInstructions.size() - 1));
                    newInstr.setViaCount(pathIndex + 1);
                    fullInstructions.replaceLast(newInstr);
                }
            }
        } else if (calcPoints) {
            PointList tmpPoints = path.calcPoints();
            if (fullPoints.isEmpty())
                fullPoints = new PointList(tmpPoints.size(), tmpPoints.is3D());
            if (simplifyResponse) {
                origPoints = tmpPoints.getSize();
                douglasPeucker.simplify(tmpPoints);
            }
            fullPoints.add(tmpPoints);
        }
        allFound = allFound && path.isFound();
    }
    if (!fullPoints.isEmpty()) {
        String debug = altRsp.getDebugInfo() + ", simplify (" + origPoints + "->" + fullPoints.getSize() + ")";
        altRsp.addDebugInfo(debug);
        if (fullPoints.is3D)
            calcAscendDescend(altRsp, fullPoints);
    }
    if (enableInstructions)
        altRsp.setInstructions(fullInstructions);
    if (!allFound)
        altRsp.addError(new ConnectionNotFoundException("Connection between locations not found", Collections.<String, Object>emptyMap()));
    altRsp.setDescription(description).setPoints(fullPoints).setRouteWeight(fullWeight).setDistance(fullDistance).setTime(fullTimeInMillis);
}
Also used : Path(com.graphhopper.routing.Path) ArrayList(java.util.ArrayList) ConnectionNotFoundException(com.graphhopper.util.exceptions.ConnectionNotFoundException)

Example 2 with ConnectionNotFoundException

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

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

the class LandmarkStorage method initActiveLandmarks.

// From all available landmarks pick just a few active ones
boolean initActiveLandmarks(int fromNode, int toNode, int[] activeLandmarkIndices, int[] activeFroms, int[] activeTos, boolean reverse) {
    if (fromNode < 0 || toNode < 0)
        throw new IllegalStateException("from " + fromNode + " and to " + toNode + " nodes have to be 0 or positive to init landmarks");
    int subnetworkFrom = subnetworkStorage.getSubnetwork(fromNode);
    int subnetworkTo = subnetworkStorage.getSubnetwork(toNode);
    if (subnetworkFrom == UNCLEAR_SUBNETWORK || subnetworkTo == UNCLEAR_SUBNETWORK)
        return false;
    if (subnetworkFrom != subnetworkTo) {
        throw new ConnectionNotFoundException("Connection between locations not found. Different subnetworks " + subnetworkFrom + " vs. " + subnetworkTo, new HashMap<String, Object>());
    }
    int[] tmpIDs = landmarkIDs.get(subnetworkFrom);
    // kind of code duplication to approximate
    List<Map.Entry<Integer, Integer>> list = new ArrayList<>(tmpIDs.length);
    for (int lmIndex = 0; lmIndex < tmpIDs.length; lmIndex++) {
        int fromWeight = getFromWeight(lmIndex, toNode) - getFromWeight(lmIndex, fromNode);
        int toWeight = getToWeight(lmIndex, fromNode) - getToWeight(lmIndex, toNode);
        list.add(new MapEntry<>(reverse ? Math.max(-fromWeight, -toWeight) : Math.max(fromWeight, toWeight), lmIndex));
    }
    Collections.sort(list, SORT_BY_WEIGHT);
    if (activeLandmarkIndices[0] >= 0) {
        IntHashSet set = new IntHashSet(activeLandmarkIndices.length);
        set.addAll(activeLandmarkIndices);
        int existingLandmarkCounter = 0;
        final int COUNT = Math.min(activeLandmarkIndices.length - 2, 2);
        for (int i = 0; i < activeLandmarkIndices.length; i++) {
            if (i >= activeLandmarkIndices.length - COUNT + existingLandmarkCounter) {
                // keep at least two of the previous landmarks (pick the best)
                break;
            } else {
                activeLandmarkIndices[i] = list.get(i).getValue();
                if (set.contains(activeLandmarkIndices[i]))
                    existingLandmarkCounter++;
            }
        }
    } else {
        for (int i = 0; i < activeLandmarkIndices.length; i++) {
            activeLandmarkIndices[i] = list.get(i).getValue();
        }
    }
    // store weight values of active landmarks in 'cache' arrays
    for (int i = 0; i < activeLandmarkIndices.length; i++) {
        int lmIndex = activeLandmarkIndices[i];
        activeFroms[i] = getFromWeight(lmIndex, toNode);
        activeTos[i] = getToWeight(lmIndex, toNode);
    }
    return true;
}
Also used : ConnectionNotFoundException(com.graphhopper.util.exceptions.ConnectionNotFoundException) MapEntry(com.graphhopper.coll.MapEntry) IntArrayList(com.carrotsearch.hppc.IntArrayList) IntHashSet(com.carrotsearch.hppc.IntHashSet) GHPoint(com.graphhopper.util.shapes.GHPoint)

Aggregations

ConnectionNotFoundException (com.graphhopper.util.exceptions.ConnectionNotFoundException)3 GHPoint (com.graphhopper.util.shapes.GHPoint)2 ArrayList (java.util.ArrayList)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 IntHashSet (com.carrotsearch.hppc.IntHashSet)1 MapEntry (com.graphhopper.coll.MapEntry)1 Path (com.graphhopper.routing.Path)1 DetailedIllegalArgumentException (com.graphhopper.util.exceptions.DetailedIllegalArgumentException)1 DetailedRuntimeException (com.graphhopper.util.exceptions.DetailedRuntimeException)1 PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)1 PointOutOfBoundsException (com.graphhopper.util.exceptions.PointOutOfBoundsException)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1