Search in sources :

Example 71 with GHResponse

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

the class Router method routeVia.

protected GHResponse routeVia(GHRequest request, Solver solver) {
    GHResponse ghRsp = new GHResponse();
    StopWatch sw = new StopWatch().start();
    DirectedEdgeFilter directedEdgeFilter = solver.createDirectedEdgeFilter();
    List<Snap> snaps = ViaRouting.lookup(encodingManager, request.getPoints(), solver.createSnapFilter(), locationIndex, request.getSnapPreventions(), request.getPointHints(), directedEdgeFilter, request.getHeadings());
    ghRsp.addDebugInfo("idLookup:" + sw.stop().getSeconds() + "s");
    // (base) query graph used to resolve headings, curbsides etc. this is not necessarily the same thing as
    // the (possibly implementation specific) query graph used by PathCalculator
    QueryGraph queryGraph = QueryGraph.create(ghStorage, snaps);
    PathCalculator pathCalculator = solver.createPathCalculator(queryGraph);
    boolean passThrough = getPassThrough(request.getHints());
    boolean forceCurbsides = getForceCurbsides(request.getHints());
    ViaRouting.Result result = ViaRouting.calcPaths(request.getPoints(), queryGraph, snaps, directedEdgeFilter, pathCalculator, request.getCurbsides(), forceCurbsides, request.getHeadings(), passThrough);
    if (request.getPoints().size() != result.paths.size() + 1)
        throw new RuntimeException("There should be exactly one more point than paths. points:" + request.getPoints().size() + ", paths:" + result.paths.size());
    // here each path represents one leg of the via-route and we merge them all together into one response path
    ResponsePath responsePath = concatenatePaths(request, solver.weighting, queryGraph, result.paths, getWaypoints(snaps));
    responsePath.addDebugInfo(result.debug);
    ghRsp.add(responsePath);
    ghRsp.getHints().putObject("visited_nodes.sum", result.visitedNodes);
    ghRsp.getHints().putObject("visited_nodes.average", (float) result.visitedNodes / (snaps.size() - 1));
    return ghRsp;
}
Also used : ResponsePath(com.graphhopper.ResponsePath) GHResponse(com.graphhopper.GHResponse) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Example 72 with GHResponse

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

the class RoutingExample method customizableRouting.

public static void customizableRouting(String ghLoc) {
    GraphHopper hopper = new GraphHopper();
    hopper.setOSMFile(ghLoc);
    hopper.setGraphHopperLocation("target/routing-custom-graph-cache");
    hopper.setProfiles(new CustomProfile("car_custom").setCustomModel(new CustomModel()).setVehicle("car"));
    // The hybrid mode uses the "landmark algorithm" and is up to 15x faster than the flexible mode (Dijkstra).
    // Still it is slower than the speed mode ("contraction hierarchies algorithm") ...
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("car_custom"));
    hopper.importOrLoad();
    // ... but for the hybrid mode we can customize the route calculation even at request time:
    // 1. a request with default preferences
    GHRequest req = new GHRequest().setProfile("car_custom").addPoint(new GHPoint(42.506472, 1.522475)).addPoint(new GHPoint(42.513108, 1.536005));
    GHResponse res = hopper.route(req);
    if (res.hasErrors())
        throw new RuntimeException(res.getErrors().toString());
    assert Math.round(res.getBest().getTime() / 1000d) == 96;
    // 2. now avoid primary roads and reduce maximum speed, see docs/core/custom-models.md for an in-depth explanation
    // and also the blog posts https://www.graphhopper.com/?s=customizable+routing
    CustomModel model = new CustomModel();
    model.addToPriority(If("road_class == PRIMARY", MULTIPLY, 0.5));
    // unconditional limit to 100km/h
    model.addToPriority(If("true", LIMIT, 100));
    req.setCustomModel(model);
    res = hopper.route(req);
    if (res.hasErrors())
        throw new RuntimeException(res.getErrors().toString());
    assert Math.round(res.getBest().getTime() / 1000d) == 165;
}
Also used : GHRequest(com.graphhopper.GHRequest) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) LMProfile(com.graphhopper.config.LMProfile) GraphHopper(com.graphhopper.GraphHopper) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse)

Example 73 with GHResponse

use of com.graphhopper.GHResponse 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 74 with GHResponse

use of com.graphhopper.GHResponse 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 75 with GHResponse

use of com.graphhopper.GHResponse 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)

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