Search in sources :

Example 21 with GHRequest

use of com.graphhopper.GHRequest in project graphhopper by graphhopper.

the class GraphHopperOSMTest method testGetPathsDirectionEnforcement6.

@Test
public void testGetPathsDirectionEnforcement6() {
    // Test if query results at tower nodes are ignored
    instance = createSquareGraphInstance(false);
    // QueryPoints directly on TowerNodes 
    GHPoint start = new GHPoint(0, 0);
    GHPoint via = new GHPoint(0.002, 0.000);
    GHPoint end = new GHPoint(0.002, 0.002);
    GHRequest req = new GHRequest().addPoint(start, 90.).addPoint(via, 270.).addPoint(end, 270.);
    GHResponse response = new GHResponse();
    List<Path> paths = instance.calcPaths(req, response);
    assertFalse(response.hasErrors());
    assertArrayEquals(new int[] { 0, 1, 2 }, paths.get(0).calcNodes().toArray());
    assertArrayEquals(new int[] { 2, 3, 4 }, paths.get(1).calcNodes().toArray());
}
Also used : GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Example 22 with GHRequest

use of com.graphhopper.GHRequest in project graphhopper by graphhopper.

the class GraphHopperOSMTest method testLoadOSM.

@Test
public void testLoadOSM() {
    GraphHopper closableInstance = new GraphHopperOSM().setStoreOnFlush(true).setEncodingManager(new EncodingManager("car")).setGraphHopperLocation(ghLoc).setDataReaderFile(testOsm);
    closableInstance.importOrLoad();
    GHResponse rsp = closableInstance.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
    assertFalse(rsp.hasErrors());
    assertEquals(3, rsp.getBest().getPoints().getSize());
    closableInstance.close();
    // no encoding manager necessary
    closableInstance = new GraphHopperOSM().setStoreOnFlush(true);
    assertTrue(closableInstance.load(ghLoc));
    rsp = closableInstance.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
    assertFalse(rsp.hasErrors());
    assertEquals(3, rsp.getBest().getPoints().getSize());
    closableInstance.close();
    try {
        rsp = closableInstance.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
        assertTrue(false);
    } catch (Exception ex) {
        assertEquals("You need to create a new GraphHopper instance as it is already closed", ex.getMessage());
    }
    try {
        closableInstance.getLocationIndex().findClosest(51.2492152, 9.4317166, EdgeFilter.ALL_EDGES);
        assertTrue(false);
    } catch (Exception ex) {
        assertEquals("You need to create a new LocationIndex instance as it is already closed", ex.getMessage());
    }
}
Also used : GHRequest(com.graphhopper.GHRequest) GraphHopper(com.graphhopper.GraphHopper) GHResponse(com.graphhopper.GHResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 23 with GHRequest

use of com.graphhopper.GHRequest in project graphhopper by graphhopper.

the class GraphHopperOSMTest method testCustomFactoryForNoneCH.

@Test
public void testCustomFactoryForNoneCH() {
    CarFlagEncoder carEncoder = new CarFlagEncoder();
    EncodingManager em = new EncodingManager(carEncoder);
    // Weighting weighting = new FastestWeighting(carEncoder);
    instance = new GraphHopperOSM().setStoreOnFlush(false).setCHEnabled(false).setEncodingManager(em).setGraphHopperLocation(ghLoc).setDataReaderFile(testOsm);
    final RoutingAlgorithmFactory af = new RoutingAlgorithmFactorySimple();
    instance.addAlgorithmFactoryDecorator(new RoutingAlgorithmFactoryDecorator() {

        @Override
        public void init(CmdArgs args) {
        }

        @Override
        public RoutingAlgorithmFactory getDecoratedAlgorithmFactory(RoutingAlgorithmFactory algoFactory, HintsMap map) {
            return af;
        }

        @Override
        public boolean isEnabled() {
            return true;
        }
    });
    instance.importOrLoad();
    assertTrue(af == instance.getAlgorithmFactory(null));
    // test that hints are passed to algorithm opts
    final AtomicInteger cnt = new AtomicInteger(0);
    instance.addAlgorithmFactoryDecorator(new RoutingAlgorithmFactoryDecorator() {

        @Override
        public void init(CmdArgs args) {
        }

        public RoutingAlgorithmFactory getDecoratedAlgorithmFactory(RoutingAlgorithmFactory algoFactory, HintsMap map) {
            return new RoutingAlgorithmFactorySimple() {

                @Override
                public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) {
                    cnt.addAndGet(1);
                    assertFalse(opts.getHints().getBool("test", true));
                    return super.createAlgo(g, opts);
                }
            };
        }

        @Override
        public boolean isEnabled() {
            return true;
        }
    });
    GHRequest req = new GHRequest(51.2492152, 9.4317166, 51.2, 9.4);
    req.getHints().put("test", false);
    instance.route(req);
    assertEquals(1, cnt.get());
}
Also used : CmdArgs(com.graphhopper.util.CmdArgs) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GHRequest(com.graphhopper.GHRequest) Test(org.junit.Test)

Example 24 with GHRequest

use of com.graphhopper.GHRequest in project graphhopper by graphhopper.

the class GraphHopperOSMTest method testNoNPE_ifLoadNotSuccessful.

@Test
public void testNoNPE_ifLoadNotSuccessful() {
    instance = new GraphHopperOSM().setStoreOnFlush(true).setEncodingManager(new EncodingManager("car"));
    try {
        // loading from empty directory
        new File(ghLoc).mkdirs();
        assertFalse(instance.load(ghLoc));
        instance.route(new GHRequest(10, 40, 12, 32));
        assertTrue(false);
    } catch (IllegalStateException ex) {
        assertEquals("Do a successful call to load or importOrLoad before routing", ex.getMessage());
    }
}
Also used : GHRequest(com.graphhopper.GHRequest) File(java.io.File) Test(org.junit.Test)

Example 25 with GHRequest

use of com.graphhopper.GHRequest in project graphhopper by graphhopper.

the class GraphHopperOSMTest method testVia.

@Test
public void testVia() {
    instance = new GraphHopperOSM().setStoreOnFlush(true).init(new CmdArgs().put("datareader.file", testOsm3).put("prepare.min_network_size", "1").put("graph.flag_encoders", "car")).setGraphHopperLocation(ghLoc);
    instance.importOrLoad();
    // A -> B -> C
    GHPoint first = new GHPoint(11.1, 50);
    GHPoint second = new GHPoint(12, 51);
    GHPoint third = new GHPoint(11.2, 51.9);
    GHResponse rsp12 = instance.route(new GHRequest().addPoint(first).addPoint(second));
    assertFalse("should find 1->2", rsp12.hasErrors());
    assertEquals(147930.5, rsp12.getBest().getDistance(), .1);
    GHResponse rsp23 = instance.route(new GHRequest().addPoint(second).addPoint(third));
    assertFalse("should find 2->3", rsp23.hasErrors());
    assertEquals(176608.9, rsp23.getBest().getDistance(), .1);
    GHResponse grsp = instance.route(new GHRequest().addPoint(first).addPoint(second).addPoint(third));
    assertFalse("should find 1->2->3", grsp.hasErrors());
    PathWrapper rsp = grsp.getBest();
    assertEquals(rsp12.getBest().getDistance() + rsp23.getBest().getDistance(), rsp.getDistance(), 1e-6);
    assertEquals(5, rsp.getPoints().getSize());
    assertEquals(5, rsp.getInstructions().size());
    assertEquals(Instruction.REACHED_VIA, rsp.getInstructions().get(1).getSign());
}
Also used : PathWrapper(com.graphhopper.PathWrapper) CmdArgs(com.graphhopper.util.CmdArgs) GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Aggregations

GHRequest (com.graphhopper.GHRequest)26 Test (org.junit.Test)23 GHResponse (com.graphhopper.GHResponse)22 GHPoint (com.graphhopper.util.shapes.GHPoint)11 PathWrapper (com.graphhopper.PathWrapper)8 GraphHopper (com.graphhopper.GraphHopper)4 GraphHopperAPI (com.graphhopper.GraphHopperAPI)3 Graph (com.graphhopper.storage.Graph)3 CmdArgs (com.graphhopper.util.CmdArgs)3 IOException (java.io.IOException)3 CHGraph (com.graphhopper.storage.CHGraph)2 NodeAccess (com.graphhopper.storage.NodeAccess)2 Downloader (com.graphhopper.util.Downloader)2 File (java.io.File)2 Random (java.util.Random)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 PrepareContractionHierarchies (com.graphhopper.routing.ch.PrepareContractionHierarchies)1 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)1 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)1 Weighting (com.graphhopper.routing.weighting.Weighting)1