Search in sources :

Example 1 with LocationIndexTree

use of com.graphhopper.storage.index.LocationIndexTree in project graphhopper by graphhopper.

the class ChangeGraphHelperTest method testApplyChanges.

@Test
public void testApplyChanges() {
    // 0-1-2
    // | |
    // 3-4
    graph.edge(0, 1, 1, true);
    graph.edge(1, 2, 1, true);
    graph.edge(3, 4, 1, true);
    graph.edge(0, 3, 1, true);
    graph.edge(1, 4, 1, true);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 0, 0.01, 0.00);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 1, 0.01, 0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 2, 0.01, 0.02);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 3, 0.00, 0.00);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 4, 0.00, 0.01);
    LocationIndex locationIndex = new LocationIndexTree(graph, new RAMDirectory()).prepareIndex();
    FlagEncoder encoder = encodingManager.getEncoder("car");
    double defaultSpeed = encoder.getSpeed(GHUtility.getEdge(graph, 0, 1).getFlags());
    AllEdgesIterator iter = graph.getAllEdges();
    while (iter.next()) {
        long flags = GHUtility.getEdge(graph, 0, 1).getFlags();
        assertEquals(defaultSpeed, encoder.getSpeed(flags), .1);
        assertTrue(encoder.isForward(flags));
    }
    Reader reader = new InputStreamReader(getClass().getResourceAsStream("overlaydata1.json"), Helper.UTF_CS);
    ChangeGraphHelper instance = new ChangeGraphHelper(graph, locationIndex);
    JsonFeatureConverter converter = new JsonFeatureConverter(ghson, instance, encodingManager);
    long updates = converter.applyChanges(reader);
    assertEquals(2, updates);
    // assert changed speed and access
    double newSpeed = encoder.getSpeed(GHUtility.getEdge(graph, 0, 1).getFlags());
    assertEquals(10, newSpeed, .1);
    assertTrue(newSpeed < defaultSpeed);
    assertFalse(encoder.isForward(GHUtility.getEdge(graph, 3, 4).getFlags()));
}
Also used : AllEdgesIterator(com.graphhopper.routing.util.AllEdgesIterator) InputStreamReader(java.io.InputStreamReader) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) JsonFeatureConverter(com.graphhopper.json.JsonFeatureConverter) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) LocationIndex(com.graphhopper.storage.index.LocationIndex) RAMDirectory(com.graphhopper.storage.RAMDirectory) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Test(org.junit.Test)

Example 2 with LocationIndexTree

use of com.graphhopper.storage.index.LocationIndexTree in project graphhopper by graphhopper.

the class PrepareLandmarksTest method testLandmarkStorageAndRouting.

@Test
public void testLandmarkStorageAndRouting() {
    // create graph with lat,lon 
    // 0  1  2  ...
    // 15 16 17 ...
    Random rand = new Random(0);
    int width = 15, height = 15;
    for (int hIndex = 0; hIndex < height; hIndex++) {
        for (int wIndex = 0; wIndex < width; wIndex++) {
            int node = wIndex + hIndex * width;
            long flags = encoder.setProperties(20 + rand.nextDouble() * 30, true, true);
            // do not connect first with last column!
            if (wIndex + 1 < width)
                graph.edge(node, node + 1).setFlags(flags);
            // avoid dead ends
            if (hIndex + 1 < height)
                graph.edge(node, node + width).setFlags(flags);
            AbstractRoutingAlgorithmTester.updateDistancesFor(graph, node, -hIndex / 50.0, wIndex / 50.0);
        }
    }
    Directory dir = new RAMDirectory();
    LocationIndex index = new LocationIndexTree(graph, dir);
    index.prepareIndex();
    int lm = 5, activeLM = 2;
    Weighting weighting = new FastestWeighting(encoder);
    LandmarkStorage store = new LandmarkStorage(graph, dir, lm, weighting, tm);
    store.setMinimumNodes(2);
    store.createLandmarks();
    // landmarks should be the 4 corners of the grid:
    int[] intList = store.getLandmarks(1);
    Arrays.sort(intList);
    assertEquals("[0, 14, 70, 182, 224]", Arrays.toString(intList));
    // two landmarks: one for subnetwork 0 (all empty) and one for subnetwork 1
    assertEquals(2, store.getSubnetworksWithLandmarks());
    assertEquals(0, store.getFromWeight(0, 224));
    double factor = store.getFactor();
    assertEquals(4671, Math.round(store.getFromWeight(0, 47) * factor));
    assertEquals(3640, Math.round(store.getFromWeight(0, 52) * factor));
    long weight1_224 = store.getFromWeight(1, 224);
    assertEquals(5525, Math.round(weight1_224 * factor));
    long weight1_47 = store.getFromWeight(1, 47);
    assertEquals(921, Math.round(weight1_47 * factor));
    // grid is symmetric
    assertEquals(weight1_224, store.getToWeight(1, 224));
    assertEquals(weight1_47, store.getToWeight(1, 47));
    // prefer the landmarks before and behind the goal
    int[] activeLandmarkIndices = new int[activeLM];
    int[] activeFroms = new int[activeLM];
    int[] activeTos = new int[activeLM];
    Arrays.fill(activeLandmarkIndices, -1);
    store.initActiveLandmarks(27, 47, activeLandmarkIndices, activeFroms, activeTos, false);
    List<Integer> list = new ArrayList<>();
    for (int idx : activeLandmarkIndices) {
        list.add(store.getLandmarks(1)[idx]);
    }
    // TODO should better select 0 and 224?
    assertEquals(Arrays.asList(224, 70), list);
    AlgorithmOptions opts = AlgorithmOptions.start().weighting(weighting).traversalMode(tm).build();
    PrepareLandmarks prepare = new PrepareLandmarks(new RAMDirectory(), graph, weighting, tm, 4, 2);
    prepare.setMinimumNodes(2);
    prepare.doWork();
    AStar expectedAlgo = new AStar(graph, weighting, tm);
    Path expectedPath = expectedAlgo.calcPath(41, 183);
    // landmarks with A*
    RoutingAlgorithm oneDirAlgoWithLandmarks = prepare.getDecoratedAlgorithm(graph, new AStar(graph, weighting, tm), opts);
    Path path = oneDirAlgoWithLandmarks.calcPath(41, 183);
    assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
    assertEquals(expectedPath.calcNodes(), path.calcNodes());
    assertEquals(expectedAlgo.getVisitedNodes(), oneDirAlgoWithLandmarks.getVisitedNodes() + 142);
    // landmarks with bidir A*
    opts.getHints().put("lm.recalc_count", 50);
    RoutingAlgorithm biDirAlgoWithLandmarks = prepare.getDecoratedAlgorithm(graph, new AStarBidirection(graph, weighting, tm), opts);
    path = biDirAlgoWithLandmarks.calcPath(41, 183);
    assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
    assertEquals(expectedPath.calcNodes(), path.calcNodes());
    assertEquals(expectedAlgo.getVisitedNodes(), biDirAlgoWithLandmarks.getVisitedNodes() + 164);
    // landmarks with A* and a QueryGraph. We expect slightly less optimal as two more cycles needs to be traversed
    // due to the two more virtual nodes but this should not harm in practise
    QueryGraph qGraph = new QueryGraph(graph);
    QueryResult fromQR = index.findClosest(-0.0401, 0.2201, EdgeFilter.ALL_EDGES);
    QueryResult toQR = index.findClosest(-0.2401, 0.0601, EdgeFilter.ALL_EDGES);
    qGraph.lookup(fromQR, toQR);
    RoutingAlgorithm qGraphOneDirAlgo = prepare.getDecoratedAlgorithm(qGraph, new AStar(qGraph, weighting, tm), opts);
    path = qGraphOneDirAlgo.calcPath(fromQR.getClosestNode(), toQR.getClosestNode());
    expectedAlgo = new AStar(qGraph, weighting, tm);
    expectedPath = expectedAlgo.calcPath(fromQR.getClosestNode(), toQR.getClosestNode());
    assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
    assertEquals(expectedPath.calcNodes(), path.calcNodes());
    assertEquals(expectedAlgo.getVisitedNodes(), qGraphOneDirAlgo.getVisitedNodes() + 133);
}
Also used : LocationIndex(com.graphhopper.storage.index.LocationIndex) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) QueryResult(com.graphhopper.storage.index.QueryResult) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 3 with LocationIndexTree

use of com.graphhopper.storage.index.LocationIndexTree in project graphhopper by graphhopper.

the class RoundTripRoutingTemplateTest method testCalcRoundTrip.

@Test
public void testCalcRoundTrip() throws Exception {
    Weighting weighting = new FastestWeighting(carFE);
    Graph g = createTestGraph(true);
    RoundTripRoutingTemplate rTripRouting = new RoundTripRoutingTemplate(new GHRequest(), new GHResponse(), null, 1);
    LocationIndex locationIndex = new LocationIndexTree(g, new RAMDirectory()).prepareIndex();
    QueryResult qr4 = locationIndex.findClosest(0.05, 0.25, EdgeFilter.ALL_EDGES);
    assertEquals(4, qr4.getClosestNode());
    QueryResult qr5 = locationIndex.findClosest(0.00, 0.05, EdgeFilter.ALL_EDGES);
    assertEquals(5, qr5.getClosestNode());
    QueryResult qr6 = locationIndex.findClosest(0.00, 0.10, EdgeFilter.ALL_EDGES);
    assertEquals(6, qr6.getClosestNode());
    QueryGraph qGraph = new QueryGraph(g);
    qGraph.lookup(qr4, qr5);
    rTripRouting.setQueryResults(Arrays.asList(qr5, qr4, qr5));
    List<Path> paths = rTripRouting.calcPaths(qGraph, new RoutingAlgorithmFactorySimple(), new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
    assertEquals(2, paths.size());
    assertEquals(Helper.createTList(5, 6, 3, 4), paths.get(0).calcNodes());
    assertEquals(Helper.createTList(4, 8, 7, 6, 5), paths.get(1).calcNodes());
    qGraph = new QueryGraph(g);
    qGraph.lookup(qr4, qr6);
    rTripRouting.setQueryResults(Arrays.asList(qr6, qr4, qr6));
    paths = rTripRouting.calcPaths(qGraph, new RoutingAlgorithmFactorySimple(), new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
    assertEquals(2, paths.size());
    assertEquals(Helper.createTList(6, 3, 4), paths.get(0).calcNodes());
    assertEquals(Helper.createTList(4, 8, 7, 6), paths.get(1).calcNodes());
}
Also used : GHResponse(com.graphhopper.GHResponse) LocationIndex(com.graphhopper.storage.index.LocationIndex) RAMDirectory(com.graphhopper.storage.RAMDirectory) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) QueryResult(com.graphhopper.storage.index.QueryResult) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) Graph(com.graphhopper.storage.Graph) GHRequest(com.graphhopper.GHRequest) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 4 with LocationIndexTree

use of com.graphhopper.storage.index.LocationIndexTree in project graphhopper by graphhopper.

the class GraphHopper method createLocationIndex.

protected LocationIndex createLocationIndex(Directory dir) {
    LocationIndexTree tmpIndex = new LocationIndexTree(ghStorage, dir);
    tmpIndex.setResolution(preciseIndexResolution);
    tmpIndex.setMaxRegionSearch(maxRegionSearch);
    if (!tmpIndex.loadExisting()) {
        ensureWriteAccess();
        tmpIndex.prepareIndex();
    }
    return tmpIndex;
}
Also used : LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree)

Example 5 with LocationIndexTree

use of com.graphhopper.storage.index.LocationIndexTree in project graphhopper by graphhopper.

the class AbstractRoutingAlgorithmTester method calcPathViaQuery.

Path calcPathViaQuery(Weighting weighting, GraphHopperStorage ghStorage, double fromLat, double fromLon, double toLat, double toLon) {
    LocationIndex index = new LocationIndexTree(ghStorage, new RAMDirectory());
    index.prepareIndex();
    QueryResult from = index.findClosest(fromLat, fromLon, EdgeFilter.ALL_EDGES);
    QueryResult to = index.findClosest(toLat, toLon, EdgeFilter.ALL_EDGES);
    // correct order for CH: in factory do prepare and afterwards wrap in query graph
    AlgorithmOptions opts = AlgorithmOptions.start().weighting(weighting).build();
    RoutingAlgorithmFactory factory = createFactory(ghStorage, opts);
    QueryGraph qGraph = new QueryGraph(getGraph(ghStorage, weighting)).lookup(from, to);
    return factory.createAlgo(qGraph, opts).calcPath(from.getClosestNode(), to.getClosestNode());
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) LocationIndex(com.graphhopper.storage.index.LocationIndex) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree)

Aggregations

LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)7 LocationIndex (com.graphhopper.storage.index.LocationIndex)5 Test (org.junit.Test)5 QueryResult (com.graphhopper.storage.index.QueryResult)3 EncodingManager (com.graphhopper.routing.util.EncodingManager)2 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)2 HintsMap (com.graphhopper.routing.util.HintsMap)2 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)2 Weighting (com.graphhopper.routing.weighting.Weighting)2 RAMDirectory (com.graphhopper.storage.RAMDirectory)2 GHRequest (com.graphhopper.GHRequest)1 GHResponse (com.graphhopper.GHResponse)1 GraphHopper (com.graphhopper.GraphHopper)1 GHIntHashSet (com.graphhopper.coll.GHIntHashSet)1 JsonFeatureConverter (com.graphhopper.json.JsonFeatureConverter)1 PrinctonReader (com.graphhopper.reader.PrinctonReader)1 AllEdgesIterator (com.graphhopper.routing.util.AllEdgesIterator)1 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)1 DefaultEdgeFilter (com.graphhopper.routing.util.DefaultEdgeFilter)1 AlgoHelperEntry (com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry)1