Search in sources :

Example 81 with GHRequest

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

the class RoutingExample method headingAndAlternativeRoute.

public static void headingAndAlternativeRoute(GraphHopper hopper) {
    // define a heading (direction) at start and destination
    GHRequest req = new GHRequest().setProfile("car").addPoint(new GHPoint(42.508774, 1.535414)).addPoint(new GHPoint(42.506595, 1.528795)).setHeadings(Arrays.asList(180d, 90d)).putHint(Parameters.CH.DISABLE, true);
    // if you have via points you can avoid U-turns there with
    // req.getHints().putObject(Parameters.Routing.PASS_THROUGH, true);
    GHResponse res = hopper.route(req);
    if (res.hasErrors())
        throw new RuntimeException(res.getErrors().toString());
    assert res.getAll().size() == 1;
    assert Helper.round(res.getBest().getDistance(), -2) == 800;
    // calculate alternative routes between two points (supported with and without CH)
    req = new GHRequest().setProfile("car").addPoint(new GHPoint(42.502904, 1.514714)).addPoint(new GHPoint(42.511953, 1.535914)).setAlgorithm(Parameters.Algorithms.ALT_ROUTE);
    req.getHints().putObject(Parameters.Algorithms.AltRoute.MAX_PATHS, 3);
    res = hopper.route(req);
    if (res.hasErrors())
        throw new RuntimeException(res.getErrors().toString());
    assert res.getAll().size() == 2;
    assert Helper.round(res.getBest().getDistance(), -2) == 2300;
}
Also used : GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse)

Example 82 with GHRequest

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

the class RoutingAlgorithmWithOSMTest method checkQueries.

/**
 * Runs the given queries on the given GraphHopper instance and checks the expectations.
 * All queries will use the first profile.
 */
private void checkQueries(GraphHopper hopper, List<Query> queries) {
    for (Function<Query, GHRequest> requestFactory : createRequestFactories()) {
        for (Query query : queries) {
            GHRequest request = requestFactory.apply(query);
            Profile profile = hopper.getProfiles().get(0);
            request.setProfile(profile.getName());
            GHResponse res = hopper.route(request);
            checkResponse(res, query);
            String expectedAlgo = request.getHints().getString("expected_algo", "no_expected_algo");
            // for edge-based routing we expect a slightly different algo name for CH
            if (profile.isTurnCosts())
                expectedAlgo = expectedAlgo.replaceAll("\\|ch-routing", "|ch|edge_based|no_sod-routing");
            assertTrue(res.getBest().getDebugInfo().contains(expectedAlgo), "Response does not contain expected algo string. Expected: '" + expectedAlgo + "', got: '" + res.getBest().getDebugInfo() + "'");
        }
    }
}
Also used : GHRequest(com.graphhopper.GHRequest) GHResponse(com.graphhopper.GHResponse) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile)

Example 83 with GHRequest

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

the class RoutingAlgorithmWithOSMTest method testDisconnectedAreaAndMultiplePoints.

@Test
public void testDisconnectedAreaAndMultiplePoints() {
    Query query = new Query();
    query.add(53.753177, 9.435968, 10, 10);
    query.add(53.751299, 9.386959, 10, 10);
    query.add(53.751299, 9.3869, 10, 10);
    GraphHopper hopper = createHopper(DIR + "/krautsand.osm.gz", new Profile("car").setVehicle("car").setWeighting("fastest"));
    hopper.setElevationProvider(new SRTMProvider(DIR));
    hopper.importOrLoad();
    for (Function<Query, GHRequest> requestFactory : createRequestFactories()) {
        GHRequest request = requestFactory.apply(query);
        request.setProfile(hopper.getProfiles().get(0).getName());
        GHResponse res = hopper.route(request);
        assertTrue(res.hasErrors());
        assertTrue(res.getErrors().toString().contains("ConnectionNotFound"), res.getErrors().toString());
    }
}
Also used : GHRequest(com.graphhopper.GHRequest) GraphHopper(com.graphhopper.GraphHopper) GHResponse(com.graphhopper.GHResponse) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) SRTMProvider(com.graphhopper.reader.dem.SRTMProvider) Test(org.junit.jupiter.api.Test)

Example 84 with GHRequest

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

the class HeadingRoutingTest method headingTest6.

@Test
public void headingTest6() {
    // Test if snaps at tower nodes are ignored
    GraphHopperStorage graph = createSquareGraph();
    Router router = createRouter(graph);
    // QueryPoints directly on TowerNodes
    GHPoint start = new GHPoint(0, 0);
    GHPoint via = new GHPoint(0.002, 0.000);
    GHPoint end = new GHPoint(0.002, 0.002);
    GHRequest req = new GHRequest().setPoints(Arrays.asList(start, via, end)).setHeadings(Arrays.asList(90., 270., 270.)).setProfile("profile").setPathDetails(Collections.singletonList("edge_key"));
    GHResponse response = router.route(req);
    assertFalse(response.hasErrors());
    assertArrayEquals(new int[] { 0, 1, 2, 3, 4 }, calcNodes(graph, response.getAll().get(0)));
}
Also used : GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) Test(org.junit.jupiter.api.Test)

Example 85 with GHRequest

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

the class HeadingRoutingTest method headingTest3.

@Test
public void headingTest3() {
    GraphHopperStorage graph = createSquareGraph();
    Router router = createRouter(graph);
    // Start in middle of edge 4-5
    GHPoint start = new GHPoint(0.0015, 0.002);
    // Via Point between 8-7
    GHPoint via = new GHPoint(0.0005, 0.001);
    // End at middle of edge 2-3
    GHPoint end = new GHPoint(0.002, 0.0005);
    GHRequest req = new GHRequest().setPoints(Arrays.asList(start, via, end)).setHeadings(Arrays.asList(Double.NaN, 0., Double.NaN)).setProfile("profile").setPathDetails(Collections.singletonList("edge_key"));
    GHResponse response = router.route(req);
    assertFalse(response.hasErrors());
    assertArrayEquals(new int[] { 4, 5, 6, 7, 8, 3, 2 }, calcNodes(graph, response.getAll().get(0)));
}
Also used : GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) Test(org.junit.jupiter.api.Test)

Aggregations

GHRequest (com.graphhopper.GHRequest)106 GHResponse (com.graphhopper.GHResponse)86 GHPoint (com.graphhopper.util.shapes.GHPoint)59 Test (org.junit.Test)35 Test (org.junit.jupiter.api.Test)35 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)22 ResponsePath (com.graphhopper.ResponsePath)13 EnumSource (org.junit.jupiter.params.provider.EnumSource)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 PathWrapper (com.graphhopper.PathWrapper)8 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 LMProfile (com.graphhopper.config.LMProfile)4