Search in sources :

Example 1 with GHException

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

the class SimpleRouteSerializer method toJSON.

@Override
public Map<String, Object> toJSON(GHResponse rsp, boolean calcPoints, boolean pointsEncoded, boolean includeElevation, boolean enableInstructions) {
    Map<String, Object> json = new HashMap<String, Object>();
    if (rsp.hasErrors()) {
        json.put("message", getMessage(rsp.getErrors().get(0)));
        List<Map<String, Object>> errorHintList = new ArrayList<>();
        for (Throwable t : rsp.getErrors()) {
            Map<String, Object> map = new HashMap<>();
            map.put("message", getMessage(t));
            map.put("details", t.getClass().getName());
            if (t instanceof GHException) {
                map.putAll(((GHException) t).getDetails());
            }
            errorHintList.add(map);
        }
        json.put("hints", errorHintList);
    } else {
        Map<String, Object> jsonInfo = new HashMap<String, Object>();
        json.put("info", jsonInfo);
        json.put("hints", rsp.getHints().toMap());
        // If you replace GraphHopper with your own brand name, this is fine. 
        // Still it would be highly appreciated if you mention us in your about page!
        jsonInfo.put("copyrights", Arrays.asList("GraphHopper", "OpenStreetMap contributors"));
        List<Map<String, Object>> jsonPathList = new ArrayList<Map<String, Object>>();
        for (PathWrapper ar : rsp.getAll()) {
            Map<String, Object> jsonPath = new HashMap<String, Object>();
            jsonPath.put("distance", Helper.round(ar.getDistance(), 3));
            jsonPath.put("weight", Helper.round6(ar.getRouteWeight()));
            jsonPath.put("time", ar.getTime());
            if (!ar.getDescription().isEmpty())
                jsonPath.put("description", ar.getDescription());
            if (calcPoints) {
                jsonPath.put("points_encoded", pointsEncoded);
                PointList points = ar.getPoints();
                if (points.getSize() >= 2) {
                    BBox maxBounds2D = new BBox(maxBounds.minLon, maxBounds.maxLon, maxBounds.minLat, maxBounds.maxLat);
                    jsonPath.put("bbox", ar.calcRouteBBox(maxBounds2D).toGeoJson());
                }
                jsonPath.put("points", createPoints(points, pointsEncoded, includeElevation));
                if (enableInstructions) {
                    InstructionList instructions = ar.getInstructions();
                    jsonPath.put("instructions", instructions.createJson());
                }
                jsonPath.put("ascend", ar.getAscend());
                jsonPath.put("descend", ar.getDescend());
            }
            jsonPath.put("snapped_waypoints", createPoints(ar.getWaypoints(), pointsEncoded, includeElevation));
            jsonPathList.add(jsonPath);
        }
        json.put("paths", jsonPathList);
    }
    return json;
}
Also used : PointList(com.graphhopper.util.PointList) InstructionList(com.graphhopper.util.InstructionList) PathWrapper(com.graphhopper.PathWrapper) BBox(com.graphhopper.util.shapes.BBox) GHException(com.graphhopper.util.exceptions.GHException)

Aggregations

PathWrapper (com.graphhopper.PathWrapper)1 InstructionList (com.graphhopper.util.InstructionList)1 PointList (com.graphhopper.util.PointList)1 GHException (com.graphhopper.util.exceptions.GHException)1 BBox (com.graphhopper.util.shapes.BBox)1