Search in sources :

Example 46 with GHRequest

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

the class GraphHopperServletIT method testPathDetailsSamePoint.

@Test
public void testPathDetailsSamePoint() throws Exception {
    GraphHopperAPI hopper = new com.graphhopper.api.GraphHopperWeb();
    assertTrue(hopper.load(getTestRouteAPIUrl()));
    GHRequest request = new GHRequest(42.554851, 1.536198, 42.554851, 1.536198);
    request.setPathDetails(Arrays.asList("average_speed", "edge_id", "time"));
    GHResponse rsp = hopper.route(request);
    assertFalse(rsp.getErrors().toString(), rsp.hasErrors());
    assertTrue(rsp.getErrors().toString(), rsp.getErrors().isEmpty());
}
Also used : GraphHopperAPI(com.graphhopper.GraphHopperAPI) GHRequest(com.graphhopper.GHRequest) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Example 47 with GHRequest

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

the class GraphHopperServletIT method testGraphHopperWebRealExceptions.

@Test
public void testGraphHopperWebRealExceptions() {
    GraphHopperAPI hopper = new com.graphhopper.api.GraphHopperWeb();
    assertTrue(hopper.load(getTestRouteAPIUrl()));
    // IllegalArgumentException (Wrong Request)
    GHResponse rsp = hopper.route(new GHRequest());
    assertFalse("Errors expected but not found.", rsp.getErrors().isEmpty());
    Throwable ex = rsp.getErrors().get(0);
    assertTrue("Wrong exception found: " + ex.getClass().getName() + ", IllegalArgumentException expected.", ex instanceof IllegalArgumentException);
    // IllegalArgumentException (Wrong Points)
    rsp = hopper.route(new GHRequest(0.0, 0.0, 0.0, 0.0));
    assertFalse("Errors expected but not found.", rsp.getErrors().isEmpty());
    List<Throwable> errs = rsp.getErrors();
    for (int i = 0; i < errs.size(); i++) {
        assertEquals(((PointOutOfBoundsException) errs.get(i)).getPointIndex(), i);
    }
    // IllegalArgumentException (Vehicle not supported)
    rsp = hopper.route(new GHRequest(42.554851, 1.536198, 42.510071, 1.548128).setVehicle("SPACE-SHUTTLE"));
    assertFalse("Errors expected but not found.", rsp.getErrors().isEmpty());
    ex = rsp.getErrors().get(0);
    assertTrue("Wrong exception found: " + ex.getClass().getName() + ", IllegalArgumentException expected.", ex instanceof IllegalArgumentException);
}
Also used : GraphHopperAPI(com.graphhopper.GraphHopperAPI) GHRequest(com.graphhopper.GHRequest) GHResponse(com.graphhopper.GHResponse) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Example 48 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 49 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 50 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)

Aggregations

GHRequest (com.graphhopper.GHRequest)50 Test (org.junit.Test)43 GHResponse (com.graphhopper.GHResponse)42 GHPoint (com.graphhopper.util.shapes.GHPoint)26 PathWrapper (com.graphhopper.PathWrapper)13 GraphHopperAPI (com.graphhopper.GraphHopperAPI)6 GraphHopper (com.graphhopper.GraphHopper)4 CHGraph (com.graphhopper.storage.CHGraph)3 Graph (com.graphhopper.storage.Graph)3 NodeAccess (com.graphhopper.storage.NodeAccess)3 CmdArgs (com.graphhopper.util.CmdArgs)3 IOException (java.io.IOException)3 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)2 Weighting (com.graphhopper.routing.weighting.Weighting)2 LocationIndex (com.graphhopper.storage.index.LocationIndex)2 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)2 QueryResult (com.graphhopper.storage.index.QueryResult)2 Downloader (com.graphhopper.util.Downloader)2 InstructionList (com.graphhopper.util.InstructionList)2 PathDetail (com.graphhopper.util.details.PathDetail)2