Skip to content
Peter edited this page Nov 29, 2013 · 12 revisions

This wiki is outdated - see unit tests for up to date usage pattern of LocationIndexTree!

Use this raw API with care and only if you know what you're doing. This API will likely break more often than the preferred 'graphHopper.route' API.

Usage with Location2Index

        // Creating and saving the graph
        EncodingManager em = new EncodingManager("CAR");
        GraphBuilder gb = new GraphBuilder(em).setLocation("graphhopper-folder").setStore(true);
        GraphStorage graph = gb.create();
        // Make a weighted edge between two nodes. False means the edge is directed.
        graph.edge(fromId, toId, cost, false);
        // Store to disc
        graph.flush();

        // Load graph
        GraphStorage graph = gb.load();
        // Load index
        LocationIndex index = new LocationIndexTree(graph, new RAMDirectory("graphhopper-folder", true));
        if (!index.loadExisting())
        {
            throw new IllegalStateException("location2id index cannot be loaded!");
        }
        // calculate path
        int fromId = index.findID(latitudeFrom, longituteFrom);
        int toId = index.findID(latitudeTo, longituteTo);
        Path path = new Dijkstra(graph, em.getEncoder("CAR")).calcPath(fromId, toId);

Usage without Location2Index

         // Creating and saving the graph
        GraphBuilder gb = new GraphBuilder(em).setLocation("graphhopper-folder").setStore(true);
        GraphStorage graph = gb.create();
        // Make a weighted, directed edge between two nodes. False means the edge is directed.
        graph.edge(fromId, toId, distanceOrWeight, false);
        // Store to disc
        graph.flush();

        // Loading and using the graph
        GraphStorage graph = gb.load();
        Path path = new Dijkstra(graph, em.getEncoder("CAR")).calcPath(fromId, toId);

LevelGraph - speed - up queries

        // Creating and saving the graph
        GraphBuilder gb = new GraphBuilder(em).setLocation("graphhopper-folder").
                        setStore(true).setLevelGraph(true);
        GraphStorage graph = gb.create();
        // Make a weighted edge between two nodes. False means the edge is directed.
        graph.edge(fromId, toId, distanceOrWeight, false);
        // Prepare the graph for fast querying ...
        new PrepareContractionHierarchies().setGraph(graph).doWork();
        graph.flush();

        // Loading and using the graph
        GraphStorage graph = gb.load();
        RoutingAlgorithm algorithm = new PrepareContractionHierarchies().setGraph(graph).createAlgo();
         // Load index
        Location2IDIndex index = new Location2NodesNtreeLG(graph, new RAMDirectory("graphhopper-folder", true));
        if (!index.loadExisting())
        {
            throw new IllegalStateException("location2id index cannot be loaded!");
        }
        // calculate path
        int fromId = index.findID(latitudeFrom, longituteFrom);
        int toId = index.findID(latitudeTo, longituteTo);
        Path path = algorithm.calcPath(fromId, toId);