Search in sources :

Example 1 with InstructionList

use of com.graphhopper.util.InstructionList in project graphhopper by graphhopper.

the class GraphHopperWebIT method readFinishInstruction.

@Test
public void readFinishInstruction() {
    GHRequest req = new GHRequest().addPoint(new GHPoint(52.261434, 13.485718)).addPoint(new GHPoint(52.399067, 13.469238));
    GHResponse res = gh.route(req);
    InstructionList instructions = res.getBest().getInstructions();
    String finishInstructionName = instructions.get(instructions.size() - 1).getName();
    assertEquals("Arrive at destination", finishInstructionName);
}
Also used : InstructionList(com.graphhopper.util.InstructionList) GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Example 2 with InstructionList

use of com.graphhopper.util.InstructionList 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());
            jsonPath.put("transfers", ar.getNumChanges());
            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("legs", ar.getLegs());
                jsonPath.put("details", ar.getPathDetails());
                jsonPath.put("ascend", ar.getAscend());
                jsonPath.put("descend", ar.getDescend());
            }
            jsonPath.put("snapped_waypoints", createPoints(ar.getWaypoints(), pointsEncoded, includeElevation));
            if (ar.getFare() != null) {
                jsonPath.put("fare", NumberFormat.getCurrencyInstance(Locale.ROOT).format(ar.getFare()));
            }
            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)

Example 3 with InstructionList

use of com.graphhopper.util.InstructionList 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 4 with InstructionList

use of com.graphhopper.util.InstructionList in project graphhopper by graphhopper.

the class GraphHopperWebIT method doNotReadFinishInstruction.

@Test
public void doNotReadFinishInstruction() {
    GHRequest req = new GHRequest().addPoint(new GHPoint(52.261434, 13.485718)).addPoint(new GHPoint(52.399067, 13.469238));
    req.getHints().put("turn_description", false);
    GHResponse res = gh.route(req);
    InstructionList instructions = res.getBest().getInstructions();
    String finishInstructionName = instructions.get(instructions.size() - 1).getName();
    assertEquals("", finishInstructionName);
}
Also used : InstructionList(com.graphhopper.util.InstructionList) GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Example 5 with InstructionList

use of com.graphhopper.util.InstructionList in project graphhopper by graphhopper.

the class RouteResourceClientHCTest method doNotReadFinishInstruction.

@ParameterizedTest
@EnumSource(value = TestParam.class)
public void doNotReadFinishInstruction(TestParam p) {
    GraphHopperWeb gh = createGH(p);
    GHRequest req = new GHRequest().addPoint(new GHPoint(42.507065, 1.529846)).addPoint(new GHPoint(42.510383, 1.533392)).putHint("vehicle", "car").putHint("turn_description", false);
    GHResponse res = gh.route(req);
    InstructionList instructions = res.getBest().getInstructions();
    String finishInstructionName = instructions.get(instructions.size() - 1).getName();
    assertEquals("", finishInstructionName);
}
Also used : InstructionList(com.graphhopper.util.InstructionList) GHRequest(com.graphhopper.GHRequest) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

InstructionList (com.graphhopper.util.InstructionList)7 GHRequest (com.graphhopper.GHRequest)6 GHResponse (com.graphhopper.GHResponse)6 GHPoint (com.graphhopper.util.shapes.GHPoint)6 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ResponsePath (com.graphhopper.ResponsePath)2 PointList (com.graphhopper.util.PointList)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 EnumSource (org.junit.jupiter.params.provider.EnumSource)2 PathWrapper (com.graphhopper.PathWrapper)1 Instruction (com.graphhopper.util.Instruction)1 PathDetail (com.graphhopper.util.details.PathDetail)1 GHException (com.graphhopper.util.exceptions.GHException)1 BBox (com.graphhopper.util.shapes.BBox)1