Search in sources :

Example 1 with ResponsePath

use of com.graphhopper.ResponsePath in project graphhopper by graphhopper.

the class GraphHopperWeb method route.

public GHResponse route(GHRequest ghRequest) {
    ResponseBody rspBody = null;
    try {
        boolean tmpElevation = ghRequest.getHints().getBool("elevation", elevation);
        boolean tmpTurnDescription = ghRequest.getHints().getBool("turn_description", true);
        // do not include in request
        ghRequest.getHints().remove("turn_description");
        Request okRequest = postRequest ? createPostRequest(ghRequest) : createGetRequest(ghRequest);
        rspBody = getClientForRequest(ghRequest).newCall(okRequest).execute().body();
        JsonNode json = objectMapper.reader().readTree(rspBody.byteStream());
        GHResponse res = new GHResponse();
        res.addErrors(ResponsePathDeserializer.readErrors(objectMapper, json));
        if (res.hasErrors())
            return res;
        JsonNode paths = json.get("paths");
        for (JsonNode path : paths) {
            ResponsePath altRsp = ResponsePathDeserializer.createResponsePath(objectMapper, path, tmpElevation, tmpTurnDescription);
            res.add(altRsp);
        }
        return res;
    } catch (Exception ex) {
        throw new RuntimeException("Problem while fetching path " + ghRequest.getPoints() + ": " + ex.getMessage(), ex);
    } finally {
        Helper.close(rspBody);
    }
}
Also used : ResponsePath(com.graphhopper.ResponsePath) Request(okhttp3.Request) GHRequest(com.graphhopper.GHRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) GHResponse(com.graphhopper.GHResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResponseBody(okhttp3.ResponseBody)

Example 2 with ResponsePath

use of com.graphhopper.ResponsePath in project graphhopper by graphhopper.

the class ResponsePathSerializer method jsonObject.

public static ObjectNode jsonObject(GHResponse ghRsp, boolean enableInstructions, boolean calcPoints, boolean enableElevation, boolean pointsEncoded, float took) {
    ObjectNode json = JsonNodeFactory.instance.objectNode();
    json.putPOJO("hints", ghRsp.getHints().toMap());
    final ObjectNode info = json.putObject("info");
    info.putPOJO("copyrights", COPYRIGHTS);
    info.put("took", Math.round(took));
    ArrayNode jsonPathList = json.putArray("paths");
    for (ResponsePath p : ghRsp.getAll()) {
        ObjectNode jsonPath = jsonPathList.addObject();
        jsonPath.put("distance", Helper.round(p.getDistance(), 3));
        jsonPath.put("weight", Helper.round6(p.getRouteWeight()));
        jsonPath.put("time", p.getTime());
        jsonPath.put("transfers", p.getNumChanges());
        if (!p.getDescription().isEmpty()) {
            jsonPath.putPOJO("description", p.getDescription());
        }
        if (calcPoints) {
            jsonPath.put("points_encoded", pointsEncoded);
            jsonPath.putPOJO("bbox", p.calcBBox2D());
            jsonPath.putPOJO("points", pointsEncoded ? encodePolyline(p.getPoints(), enableElevation, 1e5) : p.getPoints().toLineString(enableElevation));
            if (enableInstructions) {
                jsonPath.putPOJO("instructions", p.getInstructions());
            }
            jsonPath.putPOJO("legs", p.getLegs());
            jsonPath.putPOJO("details", p.getPathDetails());
            jsonPath.put("ascend", p.getAscend());
            jsonPath.put("descend", p.getDescend());
        }
        jsonPath.putPOJO("snapped_waypoints", pointsEncoded ? encodePolyline(p.getWaypoints(), enableElevation, 1e5) : p.getWaypoints().toLineString(enableElevation));
        if (p.getFare() != null) {
            jsonPath.put("fare", NumberFormat.getCurrencyInstance(Locale.ROOT).format(p.getFare()));
        }
    }
    return json;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ResponsePath(com.graphhopper.ResponsePath) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 3 with ResponsePath

use of com.graphhopper.ResponsePath in project graphhopper by graphhopper.

the class RouteResourceRepresentationTest method testUnknownInstructionSign.

@Test
public void testUnknownInstructionSign() throws IOException {
    // Modified the sign though
    ObjectMapper objectMapper = Jackson.newObjectMapper();
    JsonNode json = objectMapper.readTree("{\"instructions\":[{\"distance\":1.073,\"sign\":741,\"interval\":[0,1],\"text\":\"Continue onto A 81\",\"time\":32,\"street_name\":\"A 81\"},{\"distance\":0,\"sign\":4,\"interval\":[1,1],\"text\":\"Finish!\",\"time\":0,\"street_name\":\"\"}],\"descend\":0,\"ascend\":0,\"distance\":1.073,\"bbox\":[8.676286,48.354446,8.676297,48.354453],\"weight\":0.032179,\"time\":32,\"points_encoded\":true,\"points\":\"gfcfHwq}s@}c~AAA?\",\"snapped_waypoints\":\"gfcfHwq}s@}c~AAA?\"}");
    ResponsePath responsePath = ResponsePathDeserializer.createResponsePath(objectMapper, json, true, true);
    assertEquals(741, responsePath.getInstructions().get(0).getSign());
    assertEquals("Continue onto A 81", responsePath.getInstructions().get(0).getName());
}
Also used : ResponsePath(com.graphhopper.ResponsePath) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 4 with ResponsePath

use of com.graphhopper.ResponsePath in project graphhopper by graphhopper.

the class RoutingAlgorithmWithOSMTest method checkResponse.

private void checkResponse(GHResponse res, Query query) {
    assertFalse(res.hasErrors(), res.getErrors().toString());
    ResponsePath responsePath = res.getBest();
    assertFalse(responsePath.hasErrors(), responsePath.getErrors().toString());
    assertEquals(distCalc.calcDistance(responsePath.getPoints()), responsePath.getDistance(), 2, "responsePath.getDistance does not equal point list distance");
    assertEquals(query.getPoints().stream().mapToDouble(a -> a.expectedDistance).sum(), responsePath.getDistance(), 2, "unexpected distance");
    // We check the number of points to make sure we found the expected route.
    // There are real world instances where A-B-C is identical to A-C (in meter precision).
    assertEquals(query.getPoints().stream().mapToInt(a -> a.expectedPoints).sum(), responsePath.getPoints().size(), 1, "unexpected point list size");
}
Also used : ResponsePath(com.graphhopper.ResponsePath)

Example 5 with ResponsePath

use of com.graphhopper.ResponsePath in project graphhopper by graphhopper.

the class RoutingExample method routing.

public static void routing(GraphHopper hopper) {
    // simple configuration of the request object
    GHRequest req = new GHRequest(42.508552, 1.532936, 42.507508, 1.528773).setProfile("car").setLocale(Locale.US);
    GHResponse rsp = hopper.route(req);
    // handle errors
    if (rsp.hasErrors())
        throw new RuntimeException(rsp.getErrors().toString());
    // use the best path, see the GHResponse class for more possibilities.
    ResponsePath path = rsp.getBest();
    // points, distance in meters and time in millis of the full path
    PointList pointList = path.getPoints();
    double distance = path.getDistance();
    long timeInMs = path.getTime();
    Translation tr = hopper.getTranslationMap().getWithFallBack(Locale.UK);
    InstructionList il = path.getInstructions();
    // iterate over all turn instructions
    for (Instruction instruction : il) {
    // System.out.println("distance " + instruction.getDistance() + " for instruction: " + instruction.getTurnDescription(tr));
    }
    assert il.size() == 6;
    assert Helper.round(path.getDistance(), -2) == 900;
}
Also used : ResponsePath(com.graphhopper.ResponsePath) GHRequest(com.graphhopper.GHRequest) GHResponse(com.graphhopper.GHResponse)

Aggregations

ResponsePath (com.graphhopper.ResponsePath)29 GHResponse (com.graphhopper.GHResponse)15 GHRequest (com.graphhopper.GHRequest)13 GHPoint (com.graphhopper.util.shapes.GHPoint)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 Test (org.junit.jupiter.api.Test)7 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 MapMatching (com.graphhopper.matching.MapMatching)4 MatchResult (com.graphhopper.matching.MatchResult)4 Observation (com.graphhopper.matching.Observation)4 EnumSource (org.junit.jupiter.params.provider.EnumSource)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Path (com.graphhopper.routing.Path)3 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)3 Snap (com.graphhopper.storage.index.Snap)3 PathDetail (com.graphhopper.util.details.PathDetail)3 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2