Search in sources :

Example 6 with PathWrapper

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

the class ShapeFileReaderTest method testTravelTimesBetweenRandomLocations.

@Test
public void testTravelTimesBetweenRandomLocations() {
    int nTests = 200;
    final Random random = new Random(123);
    final GHPoint min = new GHPoint(35.882931, 14.403076);
    final GHPoint max = new GHPoint(35.913523, 14.448566);
    class RandPointGenerator {

        double rand(double min, double max) {
            return min + random.nextDouble() * (max - min);
        }

        GHPoint randPoint() {
            return new GHPoint(rand(min.lat, max.lat), rand(min.lon, max.lon));
        }
    }
    RandPointGenerator pointGenerator = new RandPointGenerator();
    int nbFails = 0;
    DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
    for (int i = 0; i < nTests; i++) {
        FromToPair pair = new FromToPair(pointGenerator.randPoint(), pointGenerator.randPoint());
        // paths from random points can fail to don't assert on failure
        PathWrapper shpPath = pair.getPath(hopperShp, false);
        PathWrapper pbfPath = pair.getPath(hopperPbf, false);
        // the road network)
        if (shpPath == null || pbfPath == null) {
            nbFails++;
            continue;
        }
        double shpSecs = getSecondsTravel(shpPath);
        double pbfSecs = getSecondsTravel(pbfPath);
        double frac = shpSecs / pbfSecs;
        double percentageDeviation = Math.abs(1.0 - frac) * 100;
        stats.accept(percentageDeviation);
    }
    assertTrue("Number of fails should be small for the chosen box", nbFails < nTests / 3);
    // Test mean fraction. There will be some deviation as not all tags are
    // considered etc,
    // but we expect it to be small for a large number of tests
    double mean = stats.getAverage();
    assertTrue("Should have a mean deviation in travel times of less than 1%", mean < 1.0);
}
Also used : Random(java.util.Random) PathWrapper(com.graphhopper.PathWrapper) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Example 7 with PathWrapper

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

the class GraphHopperOSMTest method testSortedGraph_noCH.

@Test
public void testSortedGraph_noCH() {
    instance = new GraphHopperOSM().setStoreOnFlush(false).setSortGraph(true).setEncodingManager(new EncodingManager("car")).setCHEnabled(false).setGraphHopperLocation(ghLoc).setDataReaderFile(testOsm);
    instance.importOrLoad();
    PathWrapper rsp = instance.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4).setAlgorithm(DIJKSTRA_BI)).getBest();
    assertFalse(rsp.hasErrors());
    assertEquals(3, rsp.getPoints().getSize());
    assertEquals(new GHPoint(51.24921503475044, 9.431716451757769), rsp.getPoints().toGHPoint(0));
    assertEquals(new GHPoint(52.0, 9.0), rsp.getPoints().toGHPoint(1));
    assertEquals(new GHPoint(51.199999850988384, 9.39999970197677), rsp.getPoints().toGHPoint(2));
    GHRequest req = new GHRequest(51.2492152, 9.4317166, 51.2, 9.4);
    boolean old = instance.isEnableInstructions();
    req.getHints().put("instructions", true);
    instance.route(req);
    assertEquals(old, instance.isEnableInstructions());
    req.getHints().put("instructions", false);
    instance.route(req);
    assertEquals("route method should not change instance field", old, instance.isEnableInstructions());
}
Also used : PathWrapper(com.graphhopper.PathWrapper) GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Example 8 with PathWrapper

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

the class GraphHopperOSMTest method testFootAndCar.

@Test
public void testFootAndCar() {
    // now all ways are imported
    instance = new GraphHopperOSM().setStoreOnFlush(false).setEncodingManager(new EncodingManager("car,foot")).setCHEnabled(false).setGraphHopperLocation(ghLoc).setDataReaderFile(testOsm3);
    instance.importOrLoad();
    assertEquals(5, instance.getGraphHopperStorage().getNodes());
    assertEquals(8, instance.getGraphHopperStorage().getAllEdges().getMaxId());
    // A to D
    GHResponse grsp = instance.route(new GHRequest(11.1, 50, 11.3, 51).setVehicle("car"));
    assertFalse(grsp.hasErrors());
    PathWrapper rsp = grsp.getBest();
    assertEquals(3, rsp.getPoints().getSize());
    // => found A and D
    assertEquals(50, rsp.getPoints().getLongitude(0), 1e-3);
    assertEquals(11.1, rsp.getPoints().getLatitude(0), 1e-3);
    assertEquals(51, rsp.getPoints().getLongitude(2), 1e-3);
    assertEquals(11.3, rsp.getPoints().getLatitude(2), 1e-3);
    // A to D not allowed for foot. But the location index will choose a node close to D accessible to FOOT        
    grsp = instance.route(new GHRequest(11.1, 50, 11.3, 51).setVehicle("foot"));
    assertFalse(grsp.hasErrors());
    rsp = grsp.getBest();
    assertEquals(2, rsp.getPoints().getSize());
    // => found a point on edge A-B        
    assertEquals(11.680, rsp.getPoints().getLatitude(1), 1e-3);
    assertEquals(50.644, rsp.getPoints().getLongitude(1), 1e-3);
    // A to E only for foot
    grsp = instance.route(new GHRequest(11.1, 50, 10, 51).setVehicle("foot"));
    assertFalse(grsp.hasErrors());
    rsp = grsp.getBest();
    assertEquals(2, rsp.getPoints().size());
    // A D E for car
    grsp = instance.route(new GHRequest(11.1, 50, 10, 51).setVehicle("car"));
    assertFalse(grsp.hasErrors());
    rsp = grsp.getBest();
    assertEquals(3, rsp.getPoints().getSize());
}
Also used : PathWrapper(com.graphhopper.PathWrapper) GHRequest(com.graphhopper.GHRequest) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Example 9 with PathWrapper

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

the class GraphHopperOSMTest method testFootOnly.

@Test
public void testFootOnly() {
    // now only footable ways are imported => no A D C and B D E => the other both ways have pillar nodes!
    instance = new GraphHopperOSM().setStoreOnFlush(false).setEncodingManager(new EncodingManager("foot")).setGraphHopperLocation(ghLoc).setDataReaderFile(testOsm3);
    instance.importOrLoad();
    assertEquals(2, instance.getGraphHopperStorage().getNodes());
    assertEquals(2, instance.getGraphHopperStorage().getAllEdges().getMaxId());
    // A to E only for foot
    GHResponse grsp = instance.route(new GHRequest(11.1, 50, 11.19, 52).setVehicle("foot"));
    assertFalse(grsp.hasErrors());
    PathWrapper rsp = grsp.getBest();
    // the last points snaps to the edge
    assertEquals(Helper.createPointList(11.1, 50, 10, 51, 11.194015, 51.995013), rsp.getPoints());
}
Also used : PathWrapper(com.graphhopper.PathWrapper) GHRequest(com.graphhopper.GHRequest) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Example 10 with PathWrapper

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

the class AlternativeRoutingTemplate method isReady.

@Override
public boolean isReady(PathMerger pathMerger, Translation tr) {
    if (pathList.isEmpty())
        throw new RuntimeException("Empty paths for alternative route calculation not expected");
    // if alternative route calculation was done then create the responses from single paths        
    PointList wpList = getWaypoints();
    altResponse.setWaypoints(wpList);
    ghResponse.add(altResponse);
    pathMerger.doWork(altResponse, Collections.singletonList(pathList.get(0)), tr);
    for (int index = 1; index < pathList.size(); index++) {
        PathWrapper tmpAltRsp = new PathWrapper();
        tmpAltRsp.setWaypoints(wpList);
        ghResponse.add(tmpAltRsp);
        pathMerger.doWork(tmpAltRsp, Collections.singletonList(pathList.get(index)), tr);
    }
    return true;
}
Also used : PointList(com.graphhopper.util.PointList) PathWrapper(com.graphhopper.PathWrapper) GHPoint(com.graphhopper.util.shapes.GHPoint)

Aggregations

PathWrapper (com.graphhopper.PathWrapper)16 GHPoint (com.graphhopper.util.shapes.GHPoint)9 GHRequest (com.graphhopper.GHRequest)8 GHResponse (com.graphhopper.GHResponse)8 Test (org.junit.Test)8 JSONObject (org.json.JSONObject)3 PointList (com.graphhopper.util.PointList)2 Random (java.util.Random)2 JSONArray (org.json.JSONArray)2 GraphHopperAPI (com.graphhopper.GraphHopperAPI)1 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)1 TurnWeighting (com.graphhopper.routing.weighting.TurnWeighting)1 CHGraph (com.graphhopper.storage.CHGraph)1 Graph (com.graphhopper.storage.Graph)1 NodeAccess (com.graphhopper.storage.NodeAccess)1 TurnCostExtension (com.graphhopper.storage.TurnCostExtension)1 CmdArgs (com.graphhopper.util.CmdArgs)1 Downloader (com.graphhopper.util.Downloader)1 InstructionList (com.graphhopper.util.InstructionList)1 StopWatch (com.graphhopper.util.StopWatch)1