经过两天的艰苦努力,在GitHub和JavaDocs上完成了graphhopper的单元测试和片段,认为我可以将所有内容放在一个非常基本的Java应用程序中,我必须得出结论,我是不能:(
我想做的就是:
到目前为止我的代码:
package javaapplication1;
import com.graphhopper.*;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.storage.GraphBuilder;
import com.graphhopper.storage.GraphStorage;
public class JavaApplication1 {
protected static String location = "./tmp/graphstorage";
protected static String defaultGraph = "./tmp/graphstorage/default";
private static final EncodingManager encodingManager = new EncodingManager("CAR");
public static GraphStorage createGraph() {
GraphStorage graph = new GraphBuilder(encodingManager).setLocation(location).create();
graph.setNode(0, 42, 10);
graph.setNode(1, 42.1, 10.1);
graph.setNode(2, 42.1, 10.2);
graph.setNode(3, 42, 10.4);
graph.setNode(4, 41.9, 10.2);
graph.edge(0, 1, 10, true);
graph.edge(1, 2, 10, false);
graph.edge(2, 3, 10, true);
graph.edge(0, 4, 40, true);
graph.edge(4, 3, 40, true);
return graph;
}
public static void main(String[] args) {
double fromLat = 42;
double fromLon = 10.4;
double toLat = 42;
double toLon = 10;
GraphStorage gs = createGraph();
GraphHopperAPI instance = new GraphHopper()
.setEncodingManager(encodingManager)
.setGraphHopperLocation(location)
.disableCHShortcuts();
GraphHopper hopper = (GraphHopper) instance;
//hopper.setGraph(createGraph()); // protected because only for testing?
hopper.load(location);
GHResponse ph = hopper.route(new GHRequest(fromLat, fromLon, toLat, toLon));
if(ph.isFound()) {
System.out.println(ph.getDistance());
System.out.println(ph.getPoints().getSize());
} else {
System.out.println("No Route found!");
}
}
}
Java说:“线程中的异常”main“java.lang.IllegalStateException:在路由之前调用load或importOrLoad ”。但我在漏斗上调用.load(),不幸的是返回'false',但我无法弄清楚原因。
我的目标是提供GH组件的一个非常基本的工作代码示例,以及如何连接它们来解决用例“在图上路由,而不是从OSM加载”。
答案 0 :(得分:3)
如果您不想将OSM用作数据源,则必须选择:
MyGraphHopper extends GraphHopper
并重载必要的方法。确保在调用'postProcessing'方法之前设置图表。例如,创建快捷方式并构建locationIndex。BTW:在主GraphHopper.setGraph中是公共的吗?