Search in sources :

Example 26 with BaseGraph

use of com.graphhopper.storage.BaseGraph in project graphhopper by graphhopper.

the class HeadingRoutingTest method testHeadingWithSnapFilter.

@Test
public void testHeadingWithSnapFilter() {
    FlagEncoder carEncoder = FlagEncoders.createCar();
    EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).add(Subnetwork.create("profile")).build();
    BaseGraph graph = createSquareGraphWithTunnel(encodingManager);
    Router router = createRouter(graph, encodingManager);
    // Start at 8 (slightly north to make it independent on some edge ordering and always use 8-3 or 3-8 as fallback)
    GHPoint start = new GHPoint(0.0011, 0.001);
    // End at middle of edge 2-3
    GHPoint end = new GHPoint(0.002, 0.0005);
    // no heading
    GHRequest req = new GHRequest().setPoints(Arrays.asList(start, end)).setProfile("profile").setPathDetails(Collections.singletonList("edge_key"));
    req.putHint(Parameters.Routing.PASS_THROUGH, true);
    GHResponse response = router.route(req);
    assertFalse(response.hasErrors());
    assertArrayEquals(new int[] { 8, 3, 2 }, calcNodes(graph, response.getAll().get(0)));
    // same start + end but heading=0, parallel to 3-8-7
    req = new GHRequest().setPoints(Arrays.asList(start, end)).setHeadings(Arrays.asList(0.)).setProfile("profile").setPathDetails(Collections.singletonList("edge_key"));
    req.putHint(Parameters.Routing.PASS_THROUGH, true);
    response = router.route(req);
    assertFalse(response.hasErrors());
    assertArrayEquals(new int[] { 8, 3, 2 }, calcNodes(graph, response.getAll().get(0)));
    // heading=90 parallel to 1->5
    req = new GHRequest().setPoints(Arrays.asList(start, end)).setHeadings(Arrays.asList(90., Double.NaN)).setProfile("profile").setPathDetails(Collections.singletonList("edge_key"));
    req.putHint(Parameters.Routing.PASS_THROUGH, true);
    response = router.route(req);
    assertFalse(response.hasErrors());
    assertArrayEquals(new int[] { 1, 5, 4, 3, 2 }, calcNodes(graph, response.getAll().get(0)));
    for (double angle = 0; angle < 360; angle += 10) {
        // Ignore angles nearly parallel to 1->5. I.e. it should fallback to results with 8-3.. or 3-8..
        if (angle >= 60 && angle <= 120)
            continue;
        req = new GHRequest().setPoints(Arrays.asList(start, end)).setHeadings(Arrays.asList(angle, Double.NaN)).setProfile("profile").setPathDetails(Collections.singletonList("edge_key"));
        req.putHint(Parameters.Routing.PASS_THROUGH, true);
        response = router.route(req);
        assertFalse(response.hasErrors());
        int[] expectedNodes = (angle >= 130 && angle <= 250) ? new int[] { 3, 8, 7, 0, 1, 2, 3 } : new int[] { 8, 3, 2 };
        // System.out.println(Arrays.toString(calcNodes(graph, response.getAll().get(0))) + " angle:" + angle);
        assertArrayEquals(expectedNodes, calcNodes(graph, response.getAll().get(0)), "angle: " + angle);
    }
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) BaseGraph(com.graphhopper.storage.BaseGraph) GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) Test(org.junit.jupiter.api.Test)

Example 27 with BaseGraph

use of com.graphhopper.storage.BaseGraph in project graphhopper by graphhopper.

the class PathTest method testCalcInstructionsEnterMotorway.

@Test
public void testCalcInstructionsEnterMotorway() {
    final BaseGraph graph = new BaseGraph.Builder(carManager).create();
    final NodeAccess na = graph.getNodeAccess();
    // Actual example: point=48.630533%2C9.459416&point=48.630544%2C9.459829
    // 1 -2 -3 is a motorway and tagged as oneway
    // 1 ->- 2 ->- 3
    // /
    // 4
    na.setNode(1, 48.630647, 9.459041);
    na.setNode(2, 48.630586, 9.459604);
    na.setNode(3, 48.630558, 9.459851);
    na.setNode(4, 48.63054, 9.459406);
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 2).setDistance(5)).setName("A 8");
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(2, 3).setDistance(5)).setName("A 8");
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(4, 2).setDistance(5)).setName("A 8");
    ShortestWeighting weighting = new ShortestWeighting(encoder);
    Path p = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(4, 3);
    assertTrue(p.isFound());
    InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, carManager, tr);
    // no turn instruction for entering the highway
    assertEquals(2, wayList.size());
}
Also used : NodeAccess(com.graphhopper.storage.NodeAccess) BaseGraph(com.graphhopper.storage.BaseGraph) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Test(org.junit.jupiter.api.Test)

Example 28 with BaseGraph

use of com.graphhopper.storage.BaseGraph in project graphhopper by graphhopper.

the class PathTest method testCalcInstructionIssue1047.

@Test
public void testCalcInstructionIssue1047() {
    final BaseGraph g = new BaseGraph.Builder(carManager).create();
    final NodeAccess na = g.getNodeAccess();
    // Actual example: point=51.367105%2C14.491246&point=51.369048%2C14.483092
    // 1-2 & 2-3 is a road that is turning right, 2-4 is a that is branching off.
    // When driving 1-2-4, we should create an instruction notifying the user to continue straight instead of turning and following the road
    // When driving 1-2-3, we should create an instruction as well
    // 
    // 1 ---- 2 ---- 4
    // |
    // 3
    na.setNode(1, 51.367544, 14.488209);
    na.setNode(2, 51.368046, 14.486525);
    na.setNode(3, 51.36875, 14.487019);
    na.setNode(4, 51.368428, 14.485173);
    EnumEncodedValue<RoadClass> roadClassEnc = carManager.getEnumEncodedValue(RoadClass.KEY, RoadClass.class);
    BooleanEncodedValue roadClassLinkEnc = carManager.getBooleanEncodedValue(RoadClassLink.KEY);
    GHUtility.setSpeed(60, true, true, encoder, g.edge(1, 2).setDistance(5)).setName("B 156").set(roadClassEnc, RoadClass.PRIMARY).set(roadClassLinkEnc, false);
    GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 4).setDistance(5)).setName("S 108").set(roadClassEnc, RoadClass.SECONDARY).set(roadClassLinkEnc, false);
    GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 3).setDistance(5)).setName("B 156").set(roadClassEnc, RoadClass.PRIMARY).set(roadClassLinkEnc, false);
    ShortestWeighting weighting = new ShortestWeighting(encoder);
    Path p = new Dijkstra(g, weighting, TraversalMode.NODE_BASED).calcPath(1, 4);
    assertTrue(p.isFound());
    InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, carManager, tr);
    assertEquals(3, wayList.size());
    p = new Dijkstra(g, weighting, TraversalMode.NODE_BASED).calcPath(1, 3);
    assertTrue(p.isFound());
    wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, carManager, tr);
    assertEquals(3, wayList.size());
}
Also used : NodeAccess(com.graphhopper.storage.NodeAccess) BaseGraph(com.graphhopper.storage.BaseGraph) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Test(org.junit.jupiter.api.Test)

Example 29 with BaseGraph

use of com.graphhopper.storage.BaseGraph in project graphhopper by graphhopper.

the class PathTest method testUTurnLeft.

@Test
public void testUTurnLeft() {
    final BaseGraph g = new BaseGraph.Builder(carManager).create();
    final NodeAccess na = g.getNodeAccess();
    // Real Situation: point=48.402116%2C9.994367&point=48.402198%2C9.99507
    // 7
    // |
    // 4----5----6
    // |
    // 1----2----3
    na.setNode(1, 48.402116, 9.994367);
    na.setNode(2, 48.402198, 9.99507);
    na.setNode(3, 48.402344, 9.996266);
    na.setNode(4, 48.402191, 9.994351);
    na.setNode(5, 48.402298, 9.995053);
    na.setNode(6, 48.402422, 9.996067);
    na.setNode(7, 48.402604, 9.994962);
    GHUtility.setSpeed(60, 0, encoder, g.edge(1, 2).setDistance(5).setName("Olgastraße"), g.edge(2, 3).setDistance(5).setName("Olgastraße"), g.edge(6, 5).setDistance(5).setName("Olgastraße"), g.edge(5, 4).setDistance(5).setName("Olgastraße"));
    GHUtility.setSpeed(60, 60, encoder, g.edge(2, 5).setDistance(5).setName("Neithardtstraße"), g.edge(5, 7).setDistance(5).setName("Neithardtstraße"));
    ShortestWeighting weighting = new ShortestWeighting(encoder);
    Path p = new Dijkstra(g, weighting, TraversalMode.NODE_BASED).calcPath(1, 4);
    assertTrue(p.isFound());
    InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, carManager, tr);
    assertEquals(3, wayList.size());
    assertEquals(Instruction.U_TURN_LEFT, wayList.get(1).getSign());
}
Also used : NodeAccess(com.graphhopper.storage.NodeAccess) BaseGraph(com.graphhopper.storage.BaseGraph) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Test(org.junit.jupiter.api.Test)

Example 30 with BaseGraph

use of com.graphhopper.storage.BaseGraph in project graphhopper by graphhopper.

the class DijkstraOneToManyTest method testUseCacheZeroPath_issue707.

@Test
public void testUseCacheZeroPath_issue707() {
    BaseGraph graph = createGHStorage();
    initTestStorage(graph, encoder);
    RoutingAlgorithm algo = createAlgo(graph);
    Path p = algo.calcPath(0, 0);
    assertEquals(0, p.getDistance(), 0.00000);
    p = algo.calcPath(0, 4);
    assertEquals(IntArrayList.from(0, 4), p.calcNodes());
    // expand SPT
    p = algo.calcPath(0, 7);
    assertEquals(IntArrayList.from(0, 4, 5, 7), p.calcNodes());
    // use SPT
    p = algo.calcPath(0, 2);
    assertEquals(IntArrayList.from(0, 1, 2), p.calcNodes());
}
Also used : BaseGraph(com.graphhopper.storage.BaseGraph) Test(org.junit.jupiter.api.Test)

Aggregations

BaseGraph (com.graphhopper.storage.BaseGraph)100 Test (org.junit.jupiter.api.Test)75 NodeAccess (com.graphhopper.storage.NodeAccess)37 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)28 EncodingManager (com.graphhopper.routing.util.EncodingManager)26 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)14 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)14 Dijkstra (com.graphhopper.routing.Dijkstra)13 Path (com.graphhopper.routing.Path)13 GHPoint (com.graphhopper.util.shapes.GHPoint)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)11 GHRequest (com.graphhopper.GHRequest)8 GHResponse (com.graphhopper.GHResponse)8 VirtualEdgeIteratorState (com.graphhopper.routing.querygraph.VirtualEdgeIteratorState)6 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)6 GHUtility.createMockedEdgeIteratorState (com.graphhopper.util.GHUtility.createMockedEdgeIteratorState)6 PMap (com.graphhopper.util.PMap)6 IntsRef (com.graphhopper.storage.IntsRef)4 ReaderWay (com.graphhopper.reader.ReaderWay)3