Search in sources :

Example 56 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class QueryGraphTest method testEnforceHeading.

@Test
public void testEnforceHeading() {
    // setup graph
    // ____
    // |    |
    // x    |
    // |    |
    // 0    1
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 0, 0);
    na.setNode(1, 0, 2);
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(10)).setWayGeometry(Helper.createPointList(2, 0, 2, 2));
    EdgeIteratorState edge = GHUtility.getEdge(g, 0, 1);
    // snap on first vertical part of way (upward, base is in south)
    Snap snap = fakeEdgeSnap(edge, 1.5, 0, 0);
    QueryGraph queryGraph = lookup(snap);
    // enforce going out north
    HeadingResolver headingResolver = new HeadingResolver(queryGraph);
    IntArrayList unfavoredEdges = headingResolver.getEdgesWithDifferentHeading(snap.getClosestNode(), 0);
    queryGraph.unfavorVirtualEdges(unfavoredEdges);
    // test penalized south
    boolean expect = true;
    assertEquals(expect, isAvoidEdge(queryGraph.getEdgeIteratorState(1, 2)));
    assertEquals(expect, isAvoidEdge(queryGraph.getEdgeIteratorState(1, 0)));
    queryGraph.clearUnfavoredStatus();
    // test cleared edges south
    expect = false;
    assertEquals(expect, isAvoidEdge(queryGraph.getEdgeIteratorState(1, 2)));
    assertEquals(expect, isAvoidEdge(queryGraph.getEdgeIteratorState(1, 0)));
    // enforce going south (same as coming in from north)
    unfavoredEdges = headingResolver.getEdgesWithDifferentHeading(snap.getClosestNode(), 180);
    queryGraph.unfavorVirtualEdges(unfavoredEdges);
    // test penalized north
    expect = true;
    assertEquals(expect, isAvoidEdge(queryGraph.getEdgeIteratorState(2, 1)));
    assertEquals(expect, isAvoidEdge(queryGraph.getEdgeIteratorState(2, 2)));
    // snap on second vertical part of way (downward, base is in north)
    // ____
    // |    |
    // |    x
    // |    |
    // 0    1
    snap = fakeEdgeSnap(edge, 1.5, 2, 2);
    queryGraph = lookup(Arrays.asList(snap));
    // enforce north
    unfavoredEdges = headingResolver.getEdgesWithDifferentHeading(snap.getClosestNode(), 180);
    queryGraph.unfavorVirtualEdges(unfavoredEdges);
    // test penalized south
    expect = true;
    assertEquals(expect, isAvoidEdge(queryGraph.getEdgeIteratorState(2, 1)));
    assertEquals(expect, isAvoidEdge(queryGraph.getEdgeIteratorState(2, 2)));
    queryGraph.clearUnfavoredStatus();
    // enforce south
    unfavoredEdges = headingResolver.getEdgesWithDifferentHeading(snap.getClosestNode(), 0);
    queryGraph.unfavorVirtualEdges(unfavoredEdges);
    // test penalized north
    expect = true;
    assertEquals(expect, isAvoidEdge(queryGraph.getEdgeIteratorState(1, 0)));
    assertEquals(expect, isAvoidEdge(queryGraph.getEdgeIteratorState(1, 2)));
}
Also used : IntArrayList(com.carrotsearch.hppc.IntArrayList) Snap(com.graphhopper.storage.index.Snap) HeadingResolver(com.graphhopper.routing.HeadingResolver) Test(org.junit.jupiter.api.Test)

Example 57 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class QueryGraphTest method testVirtualEdgeIds_reverse.

@Test
public void testVirtualEdgeIds_reverse() {
    // virtual nodes:     2
    // 0 - x - 1
    // virtual edges:   1   2
    FlagEncoder encoder = new CarFlagEncoder(new PMap().putObject("speed_two_directions", true));
    EncodingManager encodingManager = EncodingManager.create(encoder);
    DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc();
    Graph g = new GraphBuilder(encodingManager).create();
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 50.00, 10.10);
    na.setNode(1, 50.00, 10.20);
    double dist = DistanceCalcEarth.DIST_EARTH.calcDist(na.getLat(0), na.getLon(0), na.getLat(1), na.getLon(1));
    // this time we store the edge the other way
    EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder, g.edge(1, 0).setDistance(dist));
    edge.set(speedEnc, 100, 50);
    // query graph
    Snap snap = createLocationResult(50.00, 10.15, edge, 0, EDGE);
    QueryGraph queryGraph = QueryGraph.create(g, snap);
    assertEquals(3, queryGraph.getNodes());
    assertEquals(5, queryGraph.getEdges());
    assertEquals(4, queryGraph.getVirtualEdges().size());
    EdgeIteratorState edge_0x = queryGraph.getEdgeIteratorState(1, 2);
    EdgeIteratorState edge_x0 = queryGraph.getEdgeIteratorState(1, 0);
    EdgeIteratorState edge_x1 = queryGraph.getEdgeIteratorState(2, 1);
    EdgeIteratorState edge_1x = queryGraph.getEdgeIteratorState(2, 2);
    assertNodes(edge_0x, 0, 2);
    assertNodes(edge_x0, 2, 0);
    assertNodes(edge_x1, 2, 1);
    assertNodes(edge_1x, 1, 2);
    // virtual edge IDs are 1 and 2
    assertEquals(1, edge_0x.getEdge());
    assertEquals(1, edge_x0.getEdge());
    assertEquals(2, edge_x1.getEdge());
    assertEquals(2, edge_1x.getEdge());
    // edge keys
    assertEquals(2, edge_0x.getEdgeKey());
    assertEquals(3, edge_x0.getEdgeKey());
    assertEquals(4, edge_x1.getEdgeKey());
    assertEquals(5, edge_1x.getEdgeKey());
    assertNodes(queryGraph.getEdgeIteratorStateForKey(2), 0, 2);
    assertNodes(queryGraph.getEdgeIteratorStateForKey(3), 2, 0);
    assertNodes(queryGraph.getEdgeIteratorStateForKey(4), 2, 1);
    assertNodes(queryGraph.getEdgeIteratorStateForKey(5), 1, 2);
    // internally each edge is represented by two edge states for the two directions
    assertSame(queryGraph.getVirtualEdges().get(0), edge_0x);
    assertSame(queryGraph.getVirtualEdges().get(1), edge_x0);
    assertSame(queryGraph.getVirtualEdges().get(2), edge_x1);
    assertSame(queryGraph.getVirtualEdges().get(3), edge_1x);
    for (EdgeIteratorState e : Arrays.asList(edge_0x, edge_x1)) {
        assertEquals(50, e.get(speedEnc), 1.e-6);
        assertEquals(100, e.getReverse(speedEnc), 1.e-6);
    }
    for (EdgeIteratorState e : Arrays.asList(edge_x0, edge_1x)) {
        assertEquals(100, e.get(speedEnc), 1.e-6);
        assertEquals(50, e.getReverse(speedEnc), 1.e-6);
    }
    try {
        queryGraph.getEdgeIteratorState(3, 2);
        fail("there should be an error");
    } catch (IndexOutOfBoundsException e) {
    // ok
    }
}
Also used : Snap(com.graphhopper.storage.index.Snap) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) Test(org.junit.jupiter.api.Test)

Example 58 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class QueryGraphTest method testOneWayLoop_Issue162.

@Test
public void testOneWayLoop_Issue162() {
    // do query at x, where edge is oneway
    // 
    // |\
    // | x
    // 0<-\
    // |
    // 1
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 0, 0);
    na.setNode(1, 0, -0.001);
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(10));
    BooleanEncodedValue accessEnc = encoder.getAccessEnc();
    DecimalEncodedValue avSpeedEnc = encoder.getAverageSpeedEnc();
    // in the case of identical nodes the wayGeometry defines the direction!
    EdgeIteratorState edge = g.edge(0, 0).setDistance(100).set(accessEnc, true, false).set(avSpeedEnc, 20.0).setWayGeometry(Helper.createPointList(0.001, 0, 0, 0.001));
    Snap snap = new Snap(0.0011, 0.0009);
    snap.setClosestEdge(edge);
    snap.setWayIndex(1);
    snap.calcSnappedPoint(new DistanceCalcEuclidean());
    QueryGraph qg = lookup(snap);
    EdgeExplorer ee = qg.createEdgeExplorer();
    assertTrue(snap.getClosestNode() > 1);
    assertEquals(2, GHUtility.count(ee.setBaseNode(snap.getClosestNode())));
    EdgeIterator iter = ee.setBaseNode(snap.getClosestNode());
    iter.next();
    assertTrue(iter.get(accessEnc), iter.toString());
    assertFalse(iter.getReverse(accessEnc), iter.toString());
    iter.next();
    assertFalse(iter.get(accessEnc), iter.toString());
    assertTrue(iter.getReverse(accessEnc), iter.toString());
}
Also used : BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) Snap(com.graphhopper.storage.index.Snap) Test(org.junit.jupiter.api.Test)

Example 59 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class QueryGraphTest method testUnfavorVirtualEdgePair.

@Test
public void testUnfavorVirtualEdgePair() {
    // setup graph
    // ____
    // |    |
    // |    |
    // 0    1
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 0, 0);
    na.setNode(1, 0, 2);
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(10)).setWayGeometry(Helper.createPointList(2, 0, 2, 2));
    EdgeIteratorState edge = GHUtility.getEdge(g, 0, 1);
    // snap on first vertical part of way (upward)
    Snap snap = fakeEdgeSnap(edge, 1.5, 0, 0);
    QueryGraph queryGraph = lookup(snap);
    // enforce coming in north
    queryGraph.unfavorVirtualEdge(1);
    // test penalized south
    VirtualEdgeIteratorState incomingEdge = (VirtualEdgeIteratorState) queryGraph.getEdgeIteratorState(1, 2);
    VirtualEdgeIteratorState incomingEdgeReverse = (VirtualEdgeIteratorState) queryGraph.getEdgeIteratorState(1, incomingEdge.getBaseNode());
    // expect incoming and reverse incoming edge to be avoided
    boolean expect = true;
    assertEquals(expect, isAvoidEdge(incomingEdge));
    assertEquals(expect, isAvoidEdge(incomingEdgeReverse));
    assertEquals(new LinkedHashSet<>(Arrays.asList(incomingEdge, incomingEdgeReverse)), queryGraph.getUnfavoredVirtualEdges());
    queryGraph.clearUnfavoredStatus();
    // expect incoming and reverse incoming edge not to be avoided
    expect = false;
    assertEquals(expect, isAvoidEdge(incomingEdge));
    assertEquals(expect, isAvoidEdge(incomingEdgeReverse));
    assertEquals(new LinkedHashSet<>(), queryGraph.getUnfavoredVirtualEdges());
}
Also used : Snap(com.graphhopper.storage.index.Snap) Test(org.junit.jupiter.api.Test)

Example 60 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class QueryGraphTest method createLocationResult.

public Snap createLocationResult(double lat, double lon, EdgeIteratorState edge, int wayIndex, Snap.Position pos) {
    if (edge == null)
        throw new IllegalStateException("Specify edge != null");
    Snap tmp = new Snap(lat, lon);
    tmp.setClosestEdge(edge);
    tmp.setWayIndex(wayIndex);
    tmp.setSnappedPosition(pos);
    tmp.calcSnappedPoint(new DistanceCalcEarth());
    return tmp;
}
Also used : Snap(com.graphhopper.storage.index.Snap)

Aggregations

Snap (com.graphhopper.storage.index.Snap)77 Test (org.junit.jupiter.api.Test)39 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)31 GHPoint (com.graphhopper.util.shapes.GHPoint)22 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)20 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)13 QueryRoutingCHGraph (com.graphhopper.routing.querygraph.QueryRoutingCHGraph)12 Weighting (com.graphhopper.routing.weighting.Weighting)11 ArrayList (java.util.ArrayList)11 Path (com.graphhopper.routing.Path)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)8 RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)6 DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)6 LocationIndex (com.graphhopper.storage.index.LocationIndex)6 IntArrayList (com.carrotsearch.hppc.IntArrayList)5 GraphHopper (com.graphhopper.GraphHopper)5 Profile (com.graphhopper.config.Profile)5 ValueSource (org.junit.jupiter.params.provider.ValueSource)5 DefaultSnapFilter (com.graphhopper.routing.util.DefaultSnapFilter)4