Search in sources :

Example 51 with GHResponse

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

the class RouteResourceTest method testInitInstructionsWithTurnDescription.

@Test
public void testInitInstructionsWithTurnDescription() {
    GraphHopperWeb hopper = new GraphHopperWeb(clientUrl(app, "/route"));
    GHRequest request = new GHRequest(42.554851, 1.536198, 42.510071, 1.548128);
    request.setProfile("my_car");
    GHResponse rsp = hopper.route(request);
    assertFalse(rsp.hasErrors(), rsp.getErrors().toString());
    assertEquals("Continue onto Carrer Antoni Fiter i Rossell", rsp.getBest().getInstructions().get(3).getName());
    request.getHints().putObject("turn_description", false);
    rsp = hopper.route(request);
    assertFalse(rsp.hasErrors());
    assertEquals("Carrer Antoni Fiter i Rossell", rsp.getBest().getInstructions().get(3).getName());
}
Also used : GHRequest(com.graphhopper.GHRequest) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) GHResponse(com.graphhopper.GHResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 52 with GHResponse

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

the class RouteResourceTest method testGraphHopperWeb.

@Test
public void testGraphHopperWeb() {
    GraphHopperWeb hopper = new GraphHopperWeb(clientUrl(app, "/route"));
    GHResponse rsp = hopper.route(new GHRequest(42.554851, 1.536198, 42.510071, 1.548128).setProfile("my_car"));
    assertFalse(rsp.hasErrors(), rsp.getErrors().toString());
    assertTrue(rsp.getErrors().isEmpty(), rsp.getErrors().toString());
    ResponsePath res = rsp.getBest();
    assertTrue(res.getDistance() > 9000, "distance wasn't correct:" + res.getDistance());
    assertTrue(res.getDistance() < 9500, "distance wasn't correct:" + res.getDistance());
    rsp = hopper.route(new GHRequest().setProfile("my_car").addPoint(new GHPoint(42.554851, 1.536198)).addPoint(new GHPoint(42.531896, 1.553278)).addPoint(new GHPoint(42.510071, 1.548128)));
    assertTrue(rsp.getErrors().isEmpty(), rsp.getErrors().toString());
    res = rsp.getBest();
    assertTrue(res.getDistance() > 20000, "distance wasn't correct:" + res.getDistance());
    assertTrue(res.getDistance() < 21000, "distance wasn't correct:" + res.getDistance());
    InstructionList instructions = res.getInstructions();
    assertEquals(24, instructions.size());
    assertEquals("Continue onto la Callisa", instructions.get(0).getTurnDescription(null));
    assertEquals("At roundabout, take exit 2", instructions.get(4).getTurnDescription(null));
    assertEquals(true, instructions.get(4).getExtraInfoJSON().get("exited"));
    assertEquals(false, instructions.get(22).getExtraInfoJSON().get("exited"));
}
Also used : ResponsePath(com.graphhopper.ResponsePath) InstructionList(com.graphhopper.util.InstructionList) GHRequest(com.graphhopper.GHRequest) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) GHResponse(com.graphhopper.GHResponse) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 53 with GHResponse

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

the class GraphHopperWeb method route.

@Override
public GHResponse route(GHRequest request) {
    try {
        String places = "";
        for (GHPoint p : request.getPoints()) {
            places += "point=" + p.lat + "," + p.lon + "&";
        }
        boolean tmpInstructions = request.getHints().getBool("instructions", instructions);
        boolean tmpCalcPoints = request.getHints().getBool("calc_points", calcPoints);
        boolean tmpTurnDescription = request.getHints().getBool("turn_description", turnDescription);
        if (tmpInstructions && !tmpCalcPoints)
            throw new IllegalStateException("Cannot calculate instructions without points (only points without instructions). " + "Use calc_points=false and instructions=false to disable point and instruction calculation");
        boolean tmpElevation = request.getHints().getBool("elevation", elevation);
        String url = routeServiceUrl + "?" + places + "&type=json" + "&instructions=" + tmpInstructions + "&points_encoded=true" + "&calc_points=" + tmpCalcPoints + "&algorithm=" + request.getAlgorithm() + "&locale=" + request.getLocale().toString() + "&elevation=" + tmpElevation;
        if (!request.getVehicle().isEmpty())
            url += "&vehicle=" + request.getVehicle();
        if (!key.isEmpty())
            url += "&key=" + key;
        for (Entry<String, String> entry : request.getHints().toMap().entrySet()) {
            String urlKey = entry.getKey();
            String urlValue = entry.getValue();
            // use lower case conversion for check only!
            if (ignoreSet.contains(urlKey.toLowerCase()))
                continue;
            if (urlValue != null && !urlValue.isEmpty())
                url += "&" + WebHelper.encodeURL(urlKey) + "=" + WebHelper.encodeURL(urlValue);
        }
        String str = downloader.downloadAsString(url, true);
        JSONObject json = new JSONObject(str);
        GHResponse res = new GHResponse();
        res.addErrors(readErrors(json));
        if (res.hasErrors())
            return res;
        JSONArray paths = json.getJSONArray("paths");
        for (int index = 0; index < paths.length(); index++) {
            JSONObject path = paths.getJSONObject(index);
            PathWrapper altRsp = createPathWrapper(path, tmpCalcPoints, tmpInstructions, tmpElevation, tmpTurnDescription);
            res.add(altRsp);
        }
        return res;
    } catch (Exception ex) {
        throw new RuntimeException("Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex);
    }
}
Also used : DetailedRuntimeException(com.graphhopper.util.exceptions.DetailedRuntimeException) JSONObject(org.json.JSONObject) PathWrapper(com.graphhopper.PathWrapper) JSONArray(org.json.JSONArray) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) GHPoint(com.graphhopper.util.shapes.GHPoint) DetailedRuntimeException(com.graphhopper.util.exceptions.DetailedRuntimeException) PointOutOfBoundsException(com.graphhopper.util.exceptions.PointOutOfBoundsException) PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) DetailedIllegalArgumentException(com.graphhopper.util.exceptions.DetailedIllegalArgumentException) JSONException(org.json.JSONException) ConnectionNotFoundException(com.graphhopper.util.exceptions.ConnectionNotFoundException)

Example 54 with GHResponse

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

the class GraphHopperWebIT method testRetrieveOnlyStreetname.

@Test
public void testRetrieveOnlyStreetname() {
    GHRequest req = new GHRequest().addPoint(new GHPoint(52.261434, 13.485718)).addPoint(new GHPoint(52.399067, 13.469238));
    GHResponse res = gh.route(req);
    assertEquals("Continue onto B 96", res.getBest().getInstructions().get(4).getName());
    req.getHints().put("turn_description", false);
    res = gh.route(req);
    assertEquals("B 96", res.getBest().getInstructions().get(4).getName());
}
Also used : GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Example 55 with GHResponse

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

the class GraphHopperWebIT method testTimeout.

@Test
public void testTimeout() {
    GHRequest req = new GHRequest().addPoint(new GHPoint(49.6724, 11.3494)).addPoint(new GHPoint(49.6550, 11.4180));
    GHResponse res = gh.route(req);
    assertFalse("errors:" + res.getErrors().toString(), res.hasErrors());
    req.getHints().put(GraphHopperWeb.TIMEOUT, 1);
    try {
        res = gh.route(req);
        fail();
    } catch (RuntimeException e) {
        assertEquals(SocketTimeoutException.class, e.getCause().getClass());
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Aggregations

GHResponse (com.graphhopper.GHResponse)100 GHRequest (com.graphhopper.GHRequest)86 GHPoint (com.graphhopper.util.shapes.GHPoint)52 Test (org.junit.Test)31 Test (org.junit.jupiter.api.Test)31 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)20 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 ResponsePath (com.graphhopper.ResponsePath)15 EnumSource (org.junit.jupiter.params.provider.EnumSource)11 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 PathWrapper (com.graphhopper.PathWrapper)9 GraphHopper (com.graphhopper.GraphHopper)7 InstructionList (com.graphhopper.util.InstructionList)7 PathDetail (com.graphhopper.util.details.PathDetail)7 GraphHopperAPI (com.graphhopper.GraphHopperAPI)6 Profile (com.graphhopper.config.Profile)5 Graph (com.graphhopper.storage.Graph)5 NodeAccess (com.graphhopper.storage.NodeAccess)5 PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)5