1
我想阻塞某些道路,並在生成路線時避開它。在graphhopper中封鎖某條道路
我使用Graphhopper basic map sample
我發現這個代碼 Weighting。我相信這是我正在尋找的功能,但我沒有把它整合在一起。
我真的很感謝在展示如何將兩個代碼放在一起的幫助。示例代碼非常感謝。
在此先感謝。
我想阻塞某些道路,並在生成路線時避開它。在graphhopper中封鎖某條道路
我使用Graphhopper basic map sample
我發現這個代碼 Weighting。我相信這是我正在尋找的功能,但我沒有把它整合在一起。
我真的很感謝在展示如何將兩個代碼放在一起的幫助。示例代碼非常感謝。
在此先感謝。
所以,我會分享我的代碼示例,它顯示了我如何實現自定義加權,但它們與您提到的example類似。
首先,您必須擴展類GraphHopper
並覆蓋方法createWeighting(WeightingMap weightingMap, FlagEncoder encoder)
。
public class MyGraphHopper extends GraphHopper {
@Override
public Weighting createWeighting(WeightingMap weightingMap, FlagEncoder encoder) {
String weighting = weightingMap.getWeighting();
if (Consts.CURRENT_TRAFFIC.equalsIgnoreCase(weighting)) {
return new CurrentTrafficWeighting(encoder);
} else {
return super.createWeighting(weightingMap, encoder);
}
}
}
然後,你實現自定義加權,在你的情況下,你實現阻止一些道路的邏輯。
public class CurrentTrafficWeighting extends AbstractWeighting {
protected final static double SPEED_CONV = 3.6;
public CurrentTrafficWeighting(FlagEncoder encoder) {
super(encoder);
System.out.println("Current traffic weighting");
}
@Override
public double getMinWeight(double distance) {
return distance;
}
@Override
public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
double speed = reverse ? flagEncoder.getReverseSpeed(edgeState.getFlags()) : flagEncoder.getSpeed(edgeState.getFlags());
if (speed == 0)
return Double.POSITIVE_INFINITY;
double time = edgeState.getDistance()/speed * SPEED_CONV;
return time;
}
@Override
public String getName() {
return Consts.CURRENT_TRAFFIC;
}
}
現在,也許對你來說最重要的部分,它展示瞭如何加入這兩部分代碼。當您創建新請求時,您必須將權重設置爲自定義權重(您實施的權重權重)。通過這種方式,您將使用您的自定義加權與一些道路阻塞,同時計算最佳路線。
public static void main(String[] args) {
MyGraphHopper hopper = new MyGraphHopper();
hopper.setOSMFile(OSM_FILE_PATH);
hopper.setGraphHopperLocation(GRAPH_FOLDER);
hopper.clean();
hopper.setEncodingManager(new EncodingManager("car"));
hopper.setCHEnable(false);
hopper.importOrLoad();
GHRequest req = new GHRequest(startPoint.getX(), startPoint.getY(), finishPoint.getX(), finishPoint.getY())
.setWeighting(Consts.CURRENT_TRAFFIC)
.setVehicle("car")
.setLocale(Locale.US)
.setAlgorithm(AlgorithmOptions.DIJKSTRA_BI);
GHResponse rsp = hopper.route(req);
}
看一看這個類:https://github.com/graphhopper/graphhopper/blob/master/core/src/main/java/com/graphhopper/routing/weighting/AvoidEdgesWeighting.java或本存儲庫:https://github.com/karussell/graphhopper-traffic-data-integration – Karussell
謝謝你sir @karussell,我檢查了樣本,但不幸的是我無法弄清楚結合這兩個代碼。對不起,新手。我可以看到更簡單的樣本嗎?有點,顯示一個基本的grapphopper地圖代碼,在基本的grapphoer示例代碼中調用一個權重類或避免邊緣。預先感謝您 – Mellorine
查看最近的拉請求https://github.com/graphhopper/graphhopper/pull/890 – Karussell