Search in sources :

Example 11 with Dijkstra

use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.

the class LowLevelAPIExample method createAndSaveGraph.

public static void createAndSaveGraph() {
    FlagEncoder encoder = new CarFlagEncoder();
    EncodingManager em = EncodingManager.create(encoder);
    GraphHopperStorage graph = new GraphBuilder(em).setRAM(graphLocation, true).create();
    // Make a weighted edge between two nodes and set average speed to 50km/h
    EdgeIteratorState edge = graph.edge(0, 1).setDistance(1234).set(encoder.getAverageSpeedEnc(), 50);
    // Set node coordinates and build location index
    NodeAccess na = graph.getNodeAccess();
    graph.edge(0, 1).set(encoder.getAccessEnc(), true).set(encoder.getAverageSpeedEnc(), 10).setDistance(1530);
    na.setNode(0, 15.15, 20.20);
    na.setNode(1, 15.25, 20.21);
    LocationIndexTree index = new LocationIndexTree(graph, graph.getDirectory());
    index.prepareIndex();
    // Flush the graph and location index to disk
    graph.flush();
    index.flush();
    graph.close();
    index.close();
    // Load the graph ... can be also in a different code location
    graph = new GraphBuilder(em).setRAM(graphLocation, true).build();
    graph.loadExisting();
    // Load the location index
    index = new LocationIndexTree(graph.getBaseGraph(), graph.getDirectory());
    if (!index.loadExisting())
        throw new IllegalStateException("location index cannot be loaded!");
    // calculate with location index
    Snap fromSnap = index.findClosest(15.15, 20.20, EdgeFilter.ALL_EDGES);
    Snap toSnap = index.findClosest(15.25, 20.21, EdgeFilter.ALL_EDGES);
    QueryGraph queryGraph = QueryGraph.create(graph, fromSnap, toSnap);
    Weighting weighting = new FastestWeighting(encoder);
    Path path = new Dijkstra(queryGraph, weighting, TraversalMode.NODE_BASED).calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
    assert Helper.round(path.getDistance(), -2) == 1500;
    // calculate without location index (get the fromId and toId nodes from other code parts)
    path = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 1);
    assert Helper.round(path.getDistance(), -2) == 1500;
}
Also used : Path(com.graphhopper.routing.Path) Snap(com.graphhopper.storage.index.Snap) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Dijkstra(com.graphhopper.routing.Dijkstra) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Example 12 with Dijkstra

use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.

the class InstructionListTest method testFind.

@Test
public void testFind() {
    Graph g = new GraphBuilder(carManager).create();
    // n-4-5   (n: pillar node)
    // |
    // 7-3-2-6
    // |
    // 1
    NodeAccess na = g.getNodeAccess();
    na.setNode(1, 15.0, 10);
    na.setNode(2, 15.1, 10);
    na.setNode(3, 15.1, 9.9);
    PointList waypoint = new PointList();
    waypoint.add(15.2, 9.9);
    na.setNode(4, 15.2, 10);
    na.setNode(5, 15.2, 10.1);
    na.setNode(6, 15.1, 10.1);
    na.setNode(7, 15.1, 9.8);
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(1, 2).setDistance(10000)).setName("1-2");
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 3).setDistance(10000)).setName("2-3");
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 6).setDistance(10000)).setName("2-6");
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(3, 4).setDistance(10000)).setName("3-4").setWayGeometry(waypoint);
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(3, 7).setDistance(10000)).setName("3-7");
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(4, 5).setDistance(10000)).setName("4-5");
    ShortestWeighting weighting = new ShortestWeighting(carEncoder);
    Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 5);
    InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
    // query on first edge, get instruction for second edge
    assertEquals("2-3", Instructions.find(wayList, 15.05, 10, 1000).getName());
    // query east of first edge, get instruction for second edge
    assertEquals("2-3", Instructions.find(wayList, 15.05, 10.001, 1000).getName());
    // query south-west of node 3, get instruction for third edge
    assertEquals("3-4", Instructions.find(wayList, 15.099, 9.9, 1000).getName());
    // too far away
    assertNull(Instructions.find(wayList, 50.8, 50.25, 1000));
}
Also used : Path(com.graphhopper.routing.Path) NodeAccess(com.graphhopper.storage.NodeAccess) Graph(com.graphhopper.storage.Graph) GraphBuilder(com.graphhopper.storage.GraphBuilder) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Dijkstra(com.graphhopper.routing.Dijkstra) Test(org.junit.jupiter.api.Test)

Example 13 with Dijkstra

use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.

the class InstructionListTest method testWayList2.

@Test
public void testWayList2() {
    Graph g = new GraphBuilder(carManager).create();
    // 2
    // \.  5
    // \/
    // 4
    // /
    // 3
    NodeAccess na = g.getNodeAccess();
    na.setNode(2, 10.3, 10.15);
    na.setNode(3, 10.0, 10.08);
    na.setNode(4, 10.1, 10.10);
    na.setNode(5, 10.2, 10.13);
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(3, 4).setDistance(100)).setName("3-4");
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(4, 5).setDistance(100)).setName("4-5");
    EdgeIteratorState iter = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 4).setDistance(100));
    iter.setName("2-4");
    PointList list = new PointList();
    list.add(10.20, 10.05);
    iter.setWayGeometry(list);
    ShortestWeighting weighting = new ShortestWeighting(carEncoder);
    Path p = new Dijkstra(g, weighting, tMode).calcPath(2, 3);
    InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
    List<String> tmpList = getTurnDescriptions(wayList);
    assertEquals(Arrays.asList("continue onto 2-4", "turn slight right onto 3-4", "arrive at destination"), tmpList);
    p = new Dijkstra(g, weighting, tMode).calcPath(3, 5);
    wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
    tmpList = getTurnDescriptions(wayList);
    assertEquals(Arrays.asList("continue onto 3-4", "keep right onto 4-5", "arrive at destination"), tmpList);
}
Also used : Path(com.graphhopper.routing.Path) NodeAccess(com.graphhopper.storage.NodeAccess) Graph(com.graphhopper.storage.Graph) GraphBuilder(com.graphhopper.storage.GraphBuilder) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Dijkstra(com.graphhopper.routing.Dijkstra) Test(org.junit.jupiter.api.Test)

Example 14 with Dijkstra

use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.

the class InstructionListTest method testNoInstructionIfSameStreet.

// TODO is this problem fixed with the new instructions?
// problem: we normally don't want instructions if streetname stays but here it is suboptimal:
@Test
public void testNoInstructionIfSameStreet() {
    Graph g = new GraphBuilder(carManager).create();
    // 2
    // \.  5
    // \/
    // 4
    // /
    // 3
    NodeAccess na = g.getNodeAccess();
    na.setNode(2, 10.3, 10.15);
    na.setNode(3, 10.0, 10.05);
    na.setNode(4, 10.1, 10.10);
    na.setNode(5, 10.2, 10.15);
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(3, 4).setDistance(100)).setName("street");
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(4, 5).setDistance(100)).setName("4-5");
    EdgeIteratorState iter = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 4).setDistance(100));
    iter.setName("street");
    PointList list = new PointList();
    list.add(10.20, 10.05);
    iter.setWayGeometry(list);
    ShortestWeighting weighting = new ShortestWeighting(carEncoder);
    Path p = new Dijkstra(g, weighting, tMode).calcPath(2, 3);
    InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
    List<String> tmpList = getTurnDescriptions(wayList);
    assertEquals(Arrays.asList("continue onto street", "turn right onto street", "arrive at destination"), tmpList);
}
Also used : Path(com.graphhopper.routing.Path) NodeAccess(com.graphhopper.storage.NodeAccess) Graph(com.graphhopper.storage.Graph) GraphBuilder(com.graphhopper.storage.GraphBuilder) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Dijkstra(com.graphhopper.routing.Dijkstra) Test(org.junit.jupiter.api.Test)

Example 15 with Dijkstra

use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.

the class InstructionListTest method testWayList.

@Test
public void testWayList() {
    Graph g = createTestGraph();
    ShortestWeighting weighting = new ShortestWeighting(carEncoder);
    Path p = new Dijkstra(g, weighting, TraversalMode.NODE_BASED).calcPath(0, 10);
    InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
    List<String> tmpList = getTurnDescriptions(wayList);
    assertEquals(Arrays.asList("continue onto 0-1", "turn right onto 1-4", "turn left onto 7-8", "arrive at destination"), tmpList);
    wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, trMap.getWithFallBack(Locale.GERMAN));
    tmpList = getTurnDescriptions(wayList, trMap.getWithFallBack(Locale.GERMAN));
    assertEquals(Arrays.asList("dem Straßenverlauf von 0-1 folgen", "rechts abbiegen auf 1-4", "links abbiegen auf 7-8", "Ziel erreicht"), tmpList);
    assertEquals(70000.0, sumDistances(wayList), 1e-1);
    PointList points = p.calcPoints();
    assertEquals(10, points.size());
    // check order of tower nodes
    assertEquals(1, points.getLon(0), 1e-6);
    assertEquals(1.4, points.getLon(points.size() - 1), 1e-6);
    // check order of pillar nodes
    assertEquals(1.15, points.getLon(4), 1e-6);
    assertEquals(1.16, points.getLon(5), 1e-6);
    compare(Arrays.asList(asL(1.2d, 1.0d), asL(1.2d, 1.1), asL(1.0, 1.1), asL(1.1, 1.4)), createStartPoints(wayList));
    p = new Dijkstra(g, weighting, TraversalMode.NODE_BASED).calcPath(6, 2);
    assertEquals(42000, p.getDistance(), 1e-2);
    assertEquals(IntArrayList.from(6, 7, 8, 5, 2), p.calcNodes());
    wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
    tmpList = getTurnDescriptions(wayList);
    assertEquals(Arrays.asList("continue onto 6-7", "turn left onto 5-8", "arrive at destination"), tmpList);
    compare(Arrays.asList(asL(1d, 1d), asL(1d, 1.2), asL(1.2, 1.2)), createStartPoints(wayList));
    // special case of identical start and end
    p = new Dijkstra(g, weighting, TraversalMode.NODE_BASED).calcPath(0, 0);
    wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
    assertEquals(1, wayList.size());
    assertEquals("arrive at destination", wayList.get(0).getTurnDescription(usTR));
}
Also used : Path(com.graphhopper.routing.Path) Graph(com.graphhopper.storage.Graph) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Dijkstra(com.graphhopper.routing.Dijkstra) Test(org.junit.jupiter.api.Test)

Aggregations

Dijkstra (com.graphhopper.routing.Dijkstra)21 Path (com.graphhopper.routing.Path)16 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)14 Test (org.junit.jupiter.api.Test)12 Graph (com.graphhopper.storage.Graph)10 GraphBuilder (com.graphhopper.storage.GraphBuilder)10 NodeAccess (com.graphhopper.storage.NodeAccess)8 Weighting (com.graphhopper.routing.weighting.Weighting)4 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)3 Test (org.junit.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 DijkstraBidirectionCH (com.graphhopper.routing.DijkstraBidirectionCH)2 DijkstraOneToMany (com.graphhopper.routing.DijkstraOneToMany)2 RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)2 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)2 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)2 EncodingManager (com.graphhopper.routing.util.EncodingManager)2 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)2 Snap (com.graphhopper.storage.index.Snap)2 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)2