forked from graphhopper/graphhopper
-
Notifications
You must be signed in to change notification settings - Fork 0
Low Level API
Peter edited this page Nov 29, 2013
·
12 revisions
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.
// 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);
// 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);
// 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);