Search in sources :

Example 51 with GraphBuilder

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

the class DijkstraOneToManyTest method testDifferentEdgeFilter.

@Test
public void testDifferentEdgeFilter() {
    GraphHopperStorage g = new GraphBuilder(encodingManager).setCHGraph(new FastestWeighting(carEncoder)).create();
    g.edge(4, 3, 10, true);
    g.edge(3, 6, 10, true);
    g.edge(4, 5, 10, true);
    g.edge(5, 6, 10, true);
    DijkstraOneToMany algo = (DijkstraOneToMany) createAlgo(g);
    algo.setEdgeFilter(new EdgeFilter() {

        @Override
        public boolean accept(EdgeIteratorState iter) {
            return iter.getAdjNode() != 5;
        }
    });
    Path p = algo.calcPath(4, 6);
    assertEquals(Helper.createTList(4, 3, 6), p.calcNodes());
    // important call!
    algo.clear();
    algo.setEdgeFilter(new EdgeFilter() {

        @Override
        public boolean accept(EdgeIteratorState iter) {
            return iter.getAdjNode() != 3;
        }
    });
    p = algo.calcPath(4, 6);
    assertEquals(Helper.createTList(4, 5, 6), p.calcNodes());
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) EdgeFilter(com.graphhopper.routing.util.EdgeFilter) GraphBuilder(com.graphhopper.storage.GraphBuilder) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.Test)

Example 52 with GraphBuilder

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

the class GraphHopperAPITest method testDisconnected179.

@Test
public void testDisconnected179() {
    GraphHopperStorage graph = new GraphBuilder(encodingManager).create();
    initGraph(graph);
    GraphHopper instance = new GraphHopper().setStoreOnFlush(false).setEncodingManager(encodingManager).setCHEnabled(false).loadGraph(graph);
    GHResponse rsp = instance.route(new GHRequest(42, 10, 42, 10.4));
    assertTrue(rsp.hasErrors());
    try {
        rsp.getBest().getPoints();
        assertTrue(false);
    } catch (Exception ex) {
    }
    instance.close();
}
Also used : GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.Test)

Example 53 with GraphBuilder

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

the class GraphHopperAPITest method testLoad.

@Test
public void testLoad() {
    GraphHopperStorage graph = new GraphBuilder(encodingManager).create();
    initGraph(graph);
    // do further changes:
    NodeAccess na = graph.getNodeAccess();
    na.setNode(4, 41.9, 10.2);
    graph.edge(1, 2, 10, false);
    graph.edge(0, 4, 40, true);
    graph.edge(4, 3, 40, true);
    GraphHopper instance = new GraphHopper().setStoreOnFlush(false).setEncodingManager(encodingManager).setCHEnabled(false).loadGraph(graph);
    // 3 -> 0
    GHResponse rsp = instance.route(new GHRequest(42, 10.4, 42, 10));
    assertFalse(rsp.hasErrors());
    PathWrapper arsp = rsp.getBest();
    assertEquals(80, arsp.getDistance(), 1e-6);
    PointList points = arsp.getPoints();
    assertEquals(42, points.getLatitude(0), 1e-5);
    assertEquals(10.4, points.getLongitude(0), 1e-5);
    assertEquals(41.9, points.getLatitude(1), 1e-5);
    assertEquals(10.2, points.getLongitude(1), 1e-5);
    assertEquals(3, points.getSize());
    instance.close();
}
Also used : PointList(com.graphhopper.util.PointList) NodeAccess(com.graphhopper.storage.NodeAccess) GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.Test)

Example 54 with GraphBuilder

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

the class GraphHopperAPITest method testConcurrentGraphChange.

@Test
public void testConcurrentGraphChange() throws InterruptedException {
    final GraphHopperStorage graph = new GraphBuilder(encodingManager).create();
    initGraph(graph);
    graph.edge(1, 2, 10, true);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger checkPointCounter = new AtomicInteger(0);
    final GraphHopper graphHopper = new GraphHopper() {

        @Override
        protected ChangeGraphHelper createChangeGraphHelper(Graph graph, LocationIndex locationIndex) {
            return new ChangeGraphHelper(graph, locationIndex) {

                @Override
                public long applyChanges(EncodingManager em, Collection<JsonFeature> features) {
                    // force sleep inside the lock and let the main thread run until the lock barrier
                    latch.countDown();
                    try {
                        Thread.sleep(400);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    checkPointCounter.incrementAndGet();
                    return super.applyChanges(em, features);
                }
            };
        }
    }.setStoreOnFlush(false).setEncodingManager(encodingManager).setCHEnabled(false).loadGraph(graph);
    GHResponse rsp = graphHopper.route(new GHRequest(42, 10.4, 42, 10));
    assertFalse(rsp.toString(), rsp.hasErrors());
    assertEquals(1800, rsp.getBest().getTime());
    final List<JsonFeature> list = new ArrayList<>();
    Map<String, Object> properties = new HashMap<>();
    properties.put("speed", 5);
    list.add(new JsonFeature("1", "bbox", new BBox(10.399, 10.4, 42.0, 42.001), null, properties));
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    executorService.submit(new Runnable() {

        @Override
        public void run() {
            graphHopper.changeGraph(list);
            checkPointCounter.incrementAndGet();
        }
    });
    latch.await();
    assertEquals(0, checkPointCounter.get());
    rsp = graphHopper.route(new GHRequest(42, 10.4, 42, 10));
    assertFalse(rsp.toString(), rsp.hasErrors());
    assertEquals(8400, rsp.getBest().getTime());
    executorService.shutdown();
    executorService.awaitTermination(3, TimeUnit.SECONDS);
    assertEquals(2, checkPointCounter.get());
}
Also used : ChangeGraphHelper(com.graphhopper.storage.change.ChangeGraphHelper) GraphBuilder(com.graphhopper.storage.GraphBuilder) EncodingManager(com.graphhopper.routing.util.EncodingManager) CountDownLatch(java.util.concurrent.CountDownLatch) LocationIndex(com.graphhopper.storage.index.LocationIndex) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) JsonFeature(com.graphhopper.json.geo.JsonFeature) Graph(com.graphhopper.storage.Graph) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BBox(com.graphhopper.util.shapes.BBox) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 55 with GraphBuilder

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

the class InstructionListTest method testInstructionsWithTimeAndPlace.

@Test
public void testInstructionsWithTimeAndPlace() {
    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);
    na.setNode(4, 15.2, 9.9);
    na.setNode(5, 15.2, 10);
    na.setNode(6, 15.1, 10.1);
    na.setNode(7, 15.1, 9.8);
    g.edge(1, 2, 7000, true).setName("1-2").setFlags(flagsForSpeed(carManager, 70));
    g.edge(2, 3, 8000, true).setName("2-3").setFlags(flagsForSpeed(carManager, 80));
    g.edge(2, 6, 10000, true).setName("2-6").setFlags(flagsForSpeed(carManager, 10));
    g.edge(3, 4, 9000, true).setName("3-4").setFlags(flagsForSpeed(carManager, 90));
    g.edge(3, 7, 10000, true).setName("3-7").setFlags(flagsForSpeed(carManager, 10));
    g.edge(4, 5, 10000, true).setName("4-5").setFlags(flagsForSpeed(carManager, 100));
    Path p = new Dijkstra(g, new ShortestWeighting(carEncoder), tMode).calcPath(1, 5);
    InstructionList wayList = p.calcInstructions(usTR);
    assertEquals(4, wayList.size());
    List<GPXEntry> gpxList = wayList.createGPXList();
    assertEquals(34000, p.getDistance(), 1e-1);
    assertEquals(34000, sumDistances(wayList), 1e-1);
    assertEquals(5, gpxList.size());
    assertEquals(1604120, p.getTime());
    assertEquals(1604120, gpxList.get(gpxList.size() - 1).getTime());
    assertEquals(Instruction.CONTINUE_ON_STREET, wayList.get(0).getSign());
    assertEquals(15, wayList.get(0).getFirstLat(), 1e-3);
    assertEquals(10, wayList.get(0).getFirstLon(), 1e-3);
    assertEquals(Instruction.TURN_LEFT, wayList.get(1).getSign());
    assertEquals(15.1, wayList.get(1).getFirstLat(), 1e-3);
    assertEquals(10, wayList.get(1).getFirstLon(), 1e-3);
    assertEquals(Instruction.TURN_RIGHT, wayList.get(2).getSign());
    assertEquals(15.1, wayList.get(2).getFirstLat(), 1e-3);
    assertEquals(9.9, wayList.get(2).getFirstLon(), 1e-3);
    String gpxStr = wayList.createGPX("test", 0);
    verifyGPX(gpxStr);
    assertTrue(gpxStr, gpxStr.contains("<trkpt lat=\"15.0\" lon=\"10.0\"><time>1970-01-01T00:00:00Z</time>"));
    assertTrue(gpxStr, gpxStr.contains("<extensions>") && gpxStr.contains("</extensions>"));
    assertTrue(gpxStr, gpxStr.contains("<rtept lat=\"15.1\" lon=\"10.0\">"));
    assertTrue(gpxStr, gpxStr.contains("<gh:distance>8000.0</gh:distance>"));
    assertTrue(gpxStr, gpxStr.contains("<desc>turn left onto 2-3</desc>"));
    assertTrue(gpxStr, gpxStr.contains("<gh:sign>-2</gh:sign>"));
    assertTrue(gpxStr, gpxStr.contains("<gh:direction>N</gh:direction>"));
    assertTrue(gpxStr, gpxStr.contains("<gh:azimuth>0.0</gh:azimuth>"));
    assertFalse(gpxStr, gpxStr.contains("NaN"));
}
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.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