Search in sources :

Example 1 with TurnWeighting

use of com.graphhopper.routing.weighting.TurnWeighting in project graphhopper by graphhopper.

the class QueryGraphTest method testTurnCostsProperlyPropagated_Issue282.

@Test
public void testTurnCostsProperlyPropagated_Issue282() {
    TurnCostExtension turnExt = new TurnCostExtension();
    FlagEncoder encoder = new CarFlagEncoder(5, 5, 15);
    GraphHopperStorage graphWithTurnCosts = new GraphHopperStorage(new RAMDirectory(), new EncodingManager(encoder), false, turnExt).create(100);
    NodeAccess na = graphWithTurnCosts.getNodeAccess();
    na.setNode(0, .00, .00);
    na.setNode(1, .00, .01);
    na.setNode(2, .01, .01);
    EdgeIteratorState edge0 = graphWithTurnCosts.edge(0, 1, 10, true);
    EdgeIteratorState edge1 = graphWithTurnCosts.edge(2, 1, 10, true);
    QueryGraph qGraph = new QueryGraph(graphWithTurnCosts);
    FastestWeighting weighting = new FastestWeighting(encoder);
    TurnWeighting turnWeighting = new TurnWeighting(weighting, (TurnCostExtension) qGraph.getExtension());
    assertEquals(0, turnWeighting.calcTurnWeight(edge0.getEdge(), 1, edge1.getEdge()), .1);
    // now use turn costs and QueryGraph
    turnExt.addTurnInfo(edge0.getEdge(), 1, edge1.getEdge(), encoder.getTurnFlags(false, 10));
    assertEquals(10, turnWeighting.calcTurnWeight(edge0.getEdge(), 1, edge1.getEdge()), .1);
    QueryResult res1 = createLocationResult(0.000, 0.005, edge0, 0, QueryResult.Position.EDGE);
    QueryResult res2 = createLocationResult(0.005, 0.010, edge1, 0, QueryResult.Position.EDGE);
    qGraph.lookup(Arrays.asList(res1, res2));
    int fromQueryEdge = GHUtility.getEdge(qGraph, res1.getClosestNode(), 1).getEdge();
    int toQueryEdge = GHUtility.getEdge(qGraph, res2.getClosestNode(), 1).getEdge();
    assertEquals(10, turnWeighting.calcTurnWeight(fromQueryEdge, 1, toQueryEdge), .1);
    graphWithTurnCosts.close();
}
Also used : TurnWeighting(com.graphhopper.routing.weighting.TurnWeighting) GHPoint(com.graphhopper.util.shapes.GHPoint) QueryResult(com.graphhopper.storage.index.QueryResult) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 2 with TurnWeighting

use of com.graphhopper.routing.weighting.TurnWeighting in project graphhopper by graphhopper.

the class TestAlgoCollector method assertDistance.

public TestAlgoCollector assertDistance(AlgoHelperEntry algoEntry, List<QueryResult> queryList, OneRun oneRun) {
    List<Path> altPaths = new ArrayList<>();
    QueryGraph queryGraph = new QueryGraph(algoEntry.getForQueryGraph());
    queryGraph.lookup(queryList);
    AlgorithmOptions opts = algoEntry.getAlgorithmOptions();
    FlagEncoder encoder = opts.getWeighting().getFlagEncoder();
    if (encoder.supports(TurnWeighting.class)) {
        if (!opts.getTraversalMode().isEdgeBased()) {
            errors.add("Cannot use TurnWeighting with a node based traversal");
            return this;
        }
        algoEntry.setAlgorithmOptions(AlgorithmOptions.start(opts).weighting(new TurnWeighting(opts.getWeighting(), (TurnCostExtension) queryGraph.getExtension())).build());
    }
    RoutingAlgorithmFactory factory = algoEntry.createRoutingFactory();
    for (int i = 0; i < queryList.size() - 1; i++) {
        RoutingAlgorithm algo = factory.createAlgo(queryGraph, algoEntry.getAlgorithmOptions());
        //            if (!algoEntry.getExpectedAlgo().equals(algo.toString())) {
        //                errors.add("Algorithm expected " + algoEntry.getExpectedAlgo() + " but was " + algo.toString());
        //                return this;
        //            }
        Path path = algo.calcPath(queryList.get(i).getClosestNode(), queryList.get(i + 1).getClosestNode());
        altPaths.add(path);
    }
    PathMerger pathMerger = new PathMerger().setCalcPoints(true).setSimplifyResponse(false).setEnableInstructions(true);
    PathWrapper rsp = new PathWrapper();
    pathMerger.doWork(rsp, altPaths, trMap.getWithFallBack(Locale.US));
    if (rsp.hasErrors()) {
        errors.add("response for " + algoEntry + " contains errors. Expected distance: " + oneRun.getDistance() + ", expected points: " + oneRun + ". " + queryList + ", errors:" + rsp.getErrors());
        return this;
    }
    PointList pointList = rsp.getPoints();
    double tmpDist = pointList.calcDistance(distCalc);
    if (Math.abs(rsp.getDistance() - tmpDist) > 2) {
        errors.add(algoEntry + " path.getDistance was  " + rsp.getDistance() + "\t pointList.calcDistance was " + tmpDist + "\t (expected points " + oneRun.getLocs() + ", expected distance " + oneRun.getDistance() + ") " + queryList);
    }
    if (Math.abs(rsp.getDistance() - oneRun.getDistance()) > 2) {
        errors.add(algoEntry + " returns path not matching the expected distance of " + oneRun.getDistance() + "\t Returned was " + rsp.getDistance() + "\t (expected points " + oneRun.getLocs() + ", was " + pointList.getSize() + ") " + queryList);
    }
    // There are real world instances where A-B-C is identical to A-C (in meter precision).
    if (Math.abs(pointList.getSize() - oneRun.getLocs()) > 1) {
        errors.add(algoEntry + " returns path not matching the expected points of " + oneRun.getLocs() + "\t Returned was " + pointList.getSize() + "\t (expected distance " + oneRun.getDistance() + ", was " + rsp.getDistance() + ") " + queryList);
    }
    return this;
}
Also used : TurnWeighting(com.graphhopper.routing.weighting.TurnWeighting) ArrayList(java.util.ArrayList) GHPoint(com.graphhopper.util.shapes.GHPoint) PathWrapper(com.graphhopper.PathWrapper) TurnCostExtension(com.graphhopper.storage.TurnCostExtension)

Aggregations

TurnWeighting (com.graphhopper.routing.weighting.TurnWeighting)2 GHPoint (com.graphhopper.util.shapes.GHPoint)2 PathWrapper (com.graphhopper.PathWrapper)1 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)1 TurnCostExtension (com.graphhopper.storage.TurnCostExtension)1 QueryResult (com.graphhopper.storage.index.QueryResult)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1