Search in sources :

Example 36 with GraphBuilder

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

the class TarjanSCCTest method doImplicitVsExplicit.

private void doImplicitVsExplicit(boolean excludeSingle) {
    GraphHopperStorage g = new GraphBuilder(em).create();
    long seed = System.nanoTime();
    Random rnd = new Random(seed);
    GHUtility.buildRandomGraph(g, rnd, 1_000, 2, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), 60d, 0.8, 0.7, 0);
    TarjanSCC.ConnectedComponents implicit = TarjanSCC.findComponentsRecursive(g, edgeFilter, excludeSingle);
    TarjanSCC.ConnectedComponents explicit = TarjanSCC.findComponents(g, edgeFilter, excludeSingle);
    assertEquals(g.getNodes(), implicit.getNodes(), "total number of nodes in connected components should equal number of nodes in graph");
    assertEquals(g.getNodes(), explicit.getNodes(), "total number of nodes in connected components should equal number of nodes in graph");
    // Unfortunately the results are not expected to be identical because the edges are traversed in reversed order
    // for the explicit stack version. To make sure the components are the same we need to check for every node that
    // the component it is contained in is the same for both algorithms.
    Set<IntWithArray> componentsImplicit = buildComponentSet(implicit.getComponents());
    Set<IntWithArray> componentsExplicit = buildComponentSet(explicit.getComponents());
    if (!componentsExplicit.equals(componentsImplicit)) {
        System.out.println("seed: " + seed);
        GHUtility.printGraphForUnitTest(g, encoder);
        assertEquals(componentsExplicit, componentsImplicit, "The components found for this graph are different between the implicit and explicit implementation");
    }
    if (!implicit.getSingleNodeComponents().equals(explicit.getSingleNodeComponents())) {
        System.out.println("seed: " + seed);
        GHUtility.printGraphForUnitTest(g, encoder);
        assertEquals(implicit.getSingleNodeComponents(), explicit.getSingleNodeComponents());
    }
    assertEquals(implicit.getBiggestComponent(), explicit.getBiggestComponent(), "seed: " + seed);
    assertEquals(implicit.getNodes(), explicit.getNodes(), "seed: " + seed);
    assertEquals(implicit.getTotalComponents(), explicit.getTotalComponents(), "seed: " + seed);
}
Also used : GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage)

Example 37 with GraphBuilder

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

the class NameSimilarityEdgeFilterTest method curvedWayGeometry_issue2319.

@Test
public void curvedWayGeometry_issue2319() {
    // 0 - 1
    // |   |
    // |   |
    // -----
    // 
    // 2 -- 3
    CarFlagEncoder encoder = new CarFlagEncoder(new PMap().putObject("speed_two_directions", true));
    EncodingManager em = EncodingManager.create(encoder);
    GraphHopperStorage graph = new GraphBuilder(em).create();
    PointList pointList = new PointList(20, false);
    pointList.add(43.844377, -79.264005);
    pointList.add(43.843771, -79.263824);
    pointList.add(43.843743, -79.2638);
    pointList.add(43.843725, -79.26375);
    pointList.add(43.843724, -79.263676);
    pointList.add(43.843801, -79.263412);
    pointList.add(43.843866, -79.263);
    pointList.add(43.843873, -79.262838);
    pointList.add(43.843863, -79.262801);
    pointList.add(43.843781, -79.262729);
    pointList.add(43.842408, -79.262395);
    pointList.add(43.842363, -79.262397);
    pointList.add(43.842336, -79.262422);
    pointList.add(43.842168, -79.263186);
    pointList.add(43.842152, -79.263348);
    pointList.add(43.842225, -79.263421);
    pointList.add(43.842379, -79.263441);
    pointList.add(43.842668, -79.26352);
    pointList.add(43.842777, -79.263566);
    pointList.add(43.842832, -79.263627);
    pointList.add(43.842833, -79.263739);
    pointList.add(43.842807, -79.263802);
    pointList.add(43.842691, -79.264477);
    pointList.add(43.842711, -79.264588);
    graph.getNodeAccess().setNode(0, 43.844521, -79.263976);
    graph.getNodeAccess().setNode(1, 43.842775, -79.264649);
    EdgeIteratorState doubtfire = graph.edge(0, 1).setWayGeometry(pointList).set(encoder.getAccessEnc(), true, true).set(encoder.getAverageSpeedEnc(), 60, 60).setName("Doubtfire Crescent");
    EdgeIteratorState golden = graph.edge(0, 1).set(encoder.getAccessEnc(), true, true).set(encoder.getAverageSpeedEnc(), 60, 60).setName("Golden Avenue");
    graph.getNodeAccess().setNode(2, 43.841501560244744, -79.26366394602502);
    graph.getNodeAccess().setNode(3, 43.842247922172724, -79.2605663670726);
    PointList pointList2 = new PointList(1, false);
    pointList2.add(43.84191413615452, -79.261912128223);
    EdgeIteratorState denison = graph.edge(2, 3).setWayGeometry(pointList2).set(encoder.getAccessEnc(), true, true).set(encoder.getAverageSpeedEnc(), 60, 60).setName("Denison Street");
    double qlat = 43.842122;
    double qLon = -79.262162;
    // if we use a very large radius we find doubtfire
    NameSimilarityEdgeFilter filter = new NameSimilarityEdgeFilter(EdgeFilter.ALL_EDGES, "doubtfire", new GHPoint(qlat, qLon), 1_000);
    assertFalse(filter.accept(golden));
    assertFalse(filter.accept(denison));
    assertTrue(filter.accept(doubtfire));
    // but also using a smaller radius should work, because the inner way geomerty of Doubtfire Crescent comes very
    // close to the marker even though the tower nodes are rather far away
    filter = new NameSimilarityEdgeFilter(EdgeFilter.ALL_EDGES, "doubtfire", new GHPoint(qlat, qLon), 100);
    assertFalse(filter.accept(golden));
    assertFalse(filter.accept(denison));
    assertTrue(filter.accept(doubtfire));
}
Also used : GraphBuilder(com.graphhopper.storage.GraphBuilder) GHPoint(com.graphhopper.util.shapes.GHPoint) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.jupiter.api.Test)

Example 38 with GraphBuilder

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

the class NameSimilarityEdgeFilterTest method testDistanceFiltering.

@Test
public void testDistanceFiltering() {
    CarFlagEncoder encoder = new CarFlagEncoder();
    Graph g = new GraphBuilder(EncodingManager.create(encoder)).create();
    NodeAccess na = g.getNodeAccess();
    GHPoint pointFarAway = new GHPoint(49.458629, 11.146124);
    GHPoint point25mAway = new GHPoint(49.464871, 11.143575);
    GHPoint point200mAway = new GHPoint(49.464598, 11.149039);
    int farAwayId = 0;
    int nodeId50 = 1;
    int nodeID200 = 2;
    na.setNode(farAwayId, pointFarAway.lat, pointFarAway.lon);
    na.setNode(nodeId50, point25mAway.lat, point25mAway.lon);
    na.setNode(nodeID200, point200mAway.lat, point200mAway.lon);
    // Check that it matches a street 50m away
    EdgeIteratorState edge1 = g.edge(nodeId50, farAwayId).setName("Wentworth Street");
    assertTrue(createNameSimilarityEdgeFilter("Wentworth Street").accept(edge1));
    // Check that it doesn't match streets 200m away
    EdgeIteratorState edge2 = g.edge(nodeID200, farAwayId).setName("Wentworth Street");
    assertFalse(createNameSimilarityEdgeFilter("Wentworth Street").accept(edge2));
}
Also used : NodeAccess(com.graphhopper.storage.NodeAccess) Graph(com.graphhopper.storage.Graph) GraphBuilder(com.graphhopper.storage.GraphBuilder) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.jupiter.api.Test)

Example 39 with GraphBuilder

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

the class FootFlagEncoderTest method testCombined.

@Test
public void testCombined() {
    Graph g = new GraphBuilder(encodingManager).create();
    EdgeIteratorState edge = g.edge(0, 1);
    edge.set(footAvgSpeedEnc, 10.0).set(footAccessEnc, true, true);
    edge.set(carAvSpeedEnc, 100.0).set(carAccessEnc, true, false);
    assertEquals(10, edge.get(footAvgSpeedEnc), 1e-1);
    assertTrue(edge.get(footAccessEnc));
    assertTrue(edge.getReverse(footAccessEnc));
    assertEquals(100, edge.get(carAvSpeedEnc), 1e-1);
    assertTrue(edge.get(carAccessEnc));
    assertFalse(edge.getReverse(carAccessEnc));
    IntsRef raw = encodingManager.createEdgeFlags();
    footAvgSpeedEnc.setDecimal(false, raw, 10);
    footAccessEnc.setBool(false, raw, true);
    footAccessEnc.setBool(true, raw, true);
    assertEquals(0, carAvSpeedEnc.getDecimal(false, raw), 1e-1);
}
Also used : Graph(com.graphhopper.storage.Graph) GraphBuilder(com.graphhopper.storage.GraphBuilder) IntsRef(com.graphhopper.storage.IntsRef) Test(org.junit.jupiter.api.Test)

Example 40 with GraphBuilder

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

the class OSMRoadAccessParserTest method countryRule.

@Test
void countryRule() {
    EncodingManager em = EncodingManager.create("car");
    EnumEncodedValue<RoadAccess> roadAccessEnc = em.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class);
    Graph graph = new GraphBuilder(em).create();
    FlagEncoder encoder = em.getEncoder("car");
    EdgeIteratorState e1 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 1).setDistance(100));
    EdgeIteratorState e2 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(100));
    OSMRoadAccessParser parser = new OSMRoadAccessParser(roadAccessEnc, OSMRoadAccessParser.toOSMRestrictions(TransportationMode.CAR));
    IntsRef relFlags = em.createRelationFlags();
    ReaderWay way = new ReaderWay(27L);
    way.setTag("highway", "track");
    way.setTag("country_rule", new CountryRule() {

        @Override
        public RoadAccess getAccess(ReaderWay readerWay, TransportationMode transportationMode, RoadAccess currentRoadAccess) {
            return RoadAccess.DESTINATION;
        }
    });
    parser.handleWayTags(e1.getFlags(), way, relFlags);
    assertEquals(RoadAccess.DESTINATION, e1.get(roadAccessEnc));
    // if there is no country rule we get the default value
    way.removeTag("country_rule");
    parser.handleWayTags(e2.getFlags(), way, relFlags);
    assertEquals(RoadAccess.YES, e2.get(roadAccessEnc));
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) RoadAccess(com.graphhopper.routing.ev.RoadAccess) TransportationMode(com.graphhopper.routing.util.TransportationMode) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) ReaderWay(com.graphhopper.reader.ReaderWay) Graph(com.graphhopper.storage.Graph) CountryRule(com.graphhopper.routing.util.countryrules.CountryRule) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) GraphBuilder(com.graphhopper.storage.GraphBuilder) IntsRef(com.graphhopper.storage.IntsRef) 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