Search in sources :

Example 81 with GraphBuilder

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

the class DirectionResolverTest method setup.

@BeforeEach
public void setup() {
    encoder = new CarFlagEncoder();
    graph = new GraphBuilder(EncodingManager.create(encoder)).create();
    na = graph.getNodeAccess();
}
Also used : GraphBuilder(com.graphhopper.storage.GraphBuilder) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 82 with GraphBuilder

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

the class HeadingResolverTest method withQueryGraph.

@Test
public void withQueryGraph() {
    // 2
    // 0 -x- 1
    FlagEncoder encoder = new CarFlagEncoder();
    EncodingManager em = EncodingManager.create(encoder);
    GraphHopperStorage graph = new GraphBuilder(em).create();
    NodeAccess na = graph.getNodeAccess();
    na.setNode(0, 48.8611, 1.2194);
    na.setNode(1, 48.8538, 2.3950);
    EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 1).setDistance(10));
    Snap snap = createSnap(edge, 48.859, 2.00, 0);
    QueryGraph queryGraph = QueryGraph.create(graph, snap);
    HeadingResolver resolver = new HeadingResolver(queryGraph);
    // if the heading points East we get the Western edge 0->2
    assertEquals("0->2", queryGraph.getEdgeIteratorState(1, Integer.MIN_VALUE).toString());
    assertEquals(IntArrayList.from(1), resolver.getEdgesWithDifferentHeading(2, 90));
    // if the heading points West we get the Eastern edge 2->1
    assertEquals("2->1", queryGraph.getEdgeIteratorState(2, Integer.MIN_VALUE).toString());
    assertEquals(IntArrayList.from(2), resolver.getEdgesWithDifferentHeading(2, 270));
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) NodeAccess(com.graphhopper.storage.NodeAccess) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) GraphBuilder(com.graphhopper.storage.GraphBuilder) Snap(com.graphhopper.storage.index.Snap) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.jupiter.api.Test)

Example 83 with GraphBuilder

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

the class HeadingResolverTest method curvyEdge.

@Test
public void curvyEdge() {
    // 1 -|
    // |- 0 -|
    // |- 2
    FlagEncoder encoder = new CarFlagEncoder();
    EncodingManager em = EncodingManager.create(encoder);
    GraphHopperStorage graph = new GraphBuilder(em).create();
    NodeAccess na = graph.getNodeAccess();
    na.setNode(1, 0.01, 0.00);
    na.setNode(0, 0.00, 0.00);
    na.setNode(2, -0.01, 0.00);
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 1).setDistance(10)).setWayGeometry(Helper.createPointList(0.00, 0.01, 0.01, 0.01));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 2).setDistance(10)).setWayGeometry(Helper.createPointList(0.00, -0.01, -0.01, -0.01));
    HeadingResolver resolver = new HeadingResolver(graph);
    resolver.setTolerance(120);
    // asking for the edges not going east returns 0-2
    assertEquals(IntArrayList.from(1), resolver.getEdgesWithDifferentHeading(0, 90));
    // asking for the edges not going west returns 0-1
    assertEquals(IntArrayList.from(0), resolver.getEdgesWithDifferentHeading(0, 270));
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) NodeAccess(com.graphhopper.storage.NodeAccess) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GraphBuilder(com.graphhopper.storage.GraphBuilder) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.jupiter.api.Test)

Example 84 with GraphBuilder

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

the class PriorityRoutingTest method testMaxPriority.

@Test
void testMaxPriority() {
    BikeFlagEncoder encoder = new BikeFlagEncoder();
    EncodingManager em = EncodingManager.create(encoder);
    GraphHopperStorage graph = new GraphBuilder(em).create();
    NodeAccess na = graph.getNodeAccess();
    na.setNode(0, 48.0, 11.0);
    na.setNode(1, 48.1, 11.1);
    na.setNode(2, 48.2, 11.2);
    na.setNode(3, 48.3, 11.3);
    na.setNode(4, 48.1, 11.0);
    na.setNode(5, 48.2, 11.1);
    // 0 - 1 - 2 - 3
    // \- 4 - 5 -/
    double dist1 = 0;
    dist1 += maxSpeedEdge(em, graph, 0, 1, encoder, 1.0).getDistance();
    dist1 += maxSpeedEdge(em, graph, 1, 2, encoder, 1.0).getDistance();
    dist1 += maxSpeedEdge(em, graph, 2, 3, encoder, 1.0).getDistance();
    final double maxPrio = PriorityCode.getFactor(PriorityCode.BEST.getValue());
    double dist2 = 0;
    dist2 += maxSpeedEdge(em, graph, 0, 4, encoder, maxPrio).getDistance();
    dist2 += maxSpeedEdge(em, graph, 4, 5, encoder, maxPrio).getDistance();
    dist2 += maxSpeedEdge(em, graph, 5, 3, encoder, maxPrio).getDistance();
    // the routes 0-1-2-3 and 0-4-5-3 have similar distances (and use max speed everywhere)
    // ... but the shorter route 0-1-2-3 has smaller priority
    assertEquals(40101, dist1, 1);
    assertEquals(43005, dist2, 1);
    // A* and Dijkstra should yield the same path (the max priority must be taken into account by weighting.getMinWeight)
    {
        PriorityWeighting weighting = new PriorityWeighting(encoder, new PMap(), TurnCostProvider.NO_TURN_COST_PROVIDER);
        Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes());
        assertEquals(IntArrayList.from(0, 4, 5, 3), pathAStar.calcNodes());
    }
    {
        CustomModel customModel = new CustomModel();
        CustomWeighting weighting = CustomModelParser.createWeighting(encoder, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel);
        Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes());
        assertEquals(IntArrayList.from(0, 4, 5, 3), pathAStar.calcNodes());
    }
    {
        CustomModel customModel = new CustomModel();
        // now we even increase the priority in the custom model, which also needs to be accounted for in weighting.getMinWeight
        customModel.addToPriority(Statement.If("road_class == MOTORWAY", Statement.Op.MULTIPLY, 3));
        CustomWeighting weighting = CustomModelParser.createWeighting(encoder, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel);
        Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes());
        assertEquals(IntArrayList.from(0, 4, 5, 3), pathAStar.calcNodes());
    }
}
Also used : NodeAccess(com.graphhopper.storage.NodeAccess) PMap(com.graphhopper.util.PMap) CustomModel(com.graphhopper.util.CustomModel) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) CustomWeighting(com.graphhopper.routing.weighting.custom.CustomWeighting) GraphBuilder(com.graphhopper.storage.GraphBuilder) PriorityWeighting(com.graphhopper.routing.weighting.PriorityWeighting) Test(org.junit.jupiter.api.Test)

Example 85 with GraphBuilder

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

the class PathTest method testCalcInstructionForForkWithSameName.

@Test
public void testCalcInstructionForForkWithSameName() {
    final Graph graph = new GraphBuilder(carManager).create();
    final NodeAccess na = graph.getNodeAccess();
    // Actual example: point=48.982618%2C13.122021&point=48.982336%2C13.121002
    // 1-2 & 2-4 have the same Street name, but other from that, it would be hard to see the difference
    // We have to enforce a turn instruction here
    // 3
    // \
    // 2   --  1
    // /
    // 4
    na.setNode(1, 48.982618, 13.122021);
    na.setNode(2, 48.982565, 13.121597);
    na.setNode(3, 48.982611, 13.121012);
    na.setNode(4, 48.982336, 13.121002);
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(5)).setName("Regener Weg");
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(2, 4).setDistance(5)).setName("Regener Weg");
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(2, 3).setDistance(5));
    ShortestWeighting weighting = new ShortestWeighting(encoder);
    Path p = new Dijkstra(graph, 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(-7, wayList.get(1).getSign());
}
Also used : NodeAccess(com.graphhopper.storage.NodeAccess) Graph(com.graphhopper.storage.Graph) GraphBuilder(com.graphhopper.storage.GraphBuilder) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Test(org.junit.jupiter.api.Test)

Aggregations

GraphBuilder (com.graphhopper.storage.GraphBuilder)98 Test (org.junit.jupiter.api.Test)60 Graph (com.graphhopper.storage.Graph)48 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)40 NodeAccess (com.graphhopper.storage.NodeAccess)30 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)22 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)21 EncodingManager (com.graphhopper.routing.util.EncodingManager)19 Test (org.junit.Test)16 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)15 RepeatedTest (org.junit.jupiter.api.RepeatedTest)14 ConnectedComponents (com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents)12 Dijkstra (com.graphhopper.routing.Dijkstra)10 Path (com.graphhopper.routing.Path)10 ReaderWay (com.graphhopper.reader.ReaderWay)9 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)8 BeforeEach (org.junit.jupiter.api.BeforeEach)7 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)6 IntsRef (com.graphhopper.storage.IntsRef)6 Random (java.util.Random)6