我们正在考虑暂时不要在路由中使用一些特殊边缘。
我发现了一个与我们的问题非常相似的问题: Does GraphHopper support dynamic edge weights? 所以我不使用CH算法,在我过滤掉我需要的边缘后,将它们的距离改为巨大的值。
底层是我在class GraphHopper
中添加的全部两种方法。我添加了hopper.flush
但结果仍然不对。
我编写方法processChange()
试图表示流量数据的反馈。如果你给位置(lat& lon)有交通拥堵或
建设并致电processChange()
。它将选择距离此位置点一米的边缘,并将这些边缘的距离更改为10000000,以便这些边缘不会临时用于布线。
方法pointToLine()
只是计算位置点到边缘之间的距离。
public static GraphHopper processChange(double[] dirtyCoor){
double[] dirtyPoint;
dirtyPoint = dirtyCoor;
GraphHopper hopper = new GraphHopper();
hopper.setGraphHopperLocation("gh-problem")
.setEncodingManager(new EncodingManager("car"))
.setOSMFile("foshan.osm")
.forServer()
.setCHWeighting("no")
.setCHEnable(false);
hopper.importOrLoad();
GraphStorage g =hopper.getGraph();
AllEdgesIterator edges = g.getAllEdges();
int n =edges.getCount();
EdgeIterator iter = g.getAllEdges();
int[] edgeIds;
edgeIds = new int[n];
int[] startNodeId;
startNodeId = new int[n];
int[] endNodeId;
endNodeId = new int[n];
double[] SNlat;
double[] SNlon;
double[] ENlat;
double[] ENlon;
SNlat = new double[n];
SNlon = new double[n];
ENlat = new double[n];
ENlon = new double[n];
int i=0;
while (iter.next()) {
int edgeId = iter.getEdge();
edgeIds[i] = edgeId;
int nodeA = iter.getBaseNode();
int nodeB = iter.getAdjNode();
startNodeId[i] = nodeA;
endNodeId[i] = nodeB;
NodeAccess nodeAccess = g.getNodeAccess();
double lat = nodeAccess.getLatitude(nodeA);
double lon = nodeAccess.getLongitude(nodeA);
SNlat[i] = lat;
SNlon[i] = lon;
double adjLat = nodeAccess.getLatitude(nodeB);
double adjLon = nodeAccess.getLongitude(nodeB);
ENlat[i] = adjLat;
ENlon[i] = adjLon;
double distance = pointToLine(SNlat[i],SNlon[i],ENlat[i],ENlon[i],dirtyPoint[0],dirtyPoint[1]);
if (distance <= 1){
double preDist = iter.getDistance();
iter.setDistance(1000000);
double cDist = iter.getDistance();
}
i=i+1;
}
hopper.flush();
hopper.setGraph(g);
//routeing test
double[] orig = new double[]{23.0389909, 113.096614};
double[] dest = new double[]{23.0389031, 113.1028902};
GHRequest request = new GHRequest(orig[0], orig[1], dest[0], dest[1]);
request.setWeighting("fastest");
request.setVehicle("car");
GHResponse route = hopper.route(request);
double time=route.getMillis();
double dis=route.getDistance();
System.out.println("distance=" + dis);
System.out.println("time=" + time);
return hopper;
}
public static double pointToLine(double SNlat, double SNlon, double ENlat, double ENlon, double DPlat, double DPlon) {
double space = 0;
double edgeLength = new DistanceCalcEarth().calcDist(SNlat, SNlon, ENlat, ENlon);
double SN2DP = new DistanceCalcEarth().calcDist(SNlat, SNlon, DPlat, DPlon);
double EN2DP = new DistanceCalcEarth().calcDist(ENlat, ENlon, DPlat, DPlon);
if (Math.abs((SN2DP + EN2DP) - edgeLength)<=0.000001){
space = 0;
return space;
}
else{
double p = (edgeLength + EN2DP + SN2DP) / 2;
double s = Math.sqrt(p * (p - edgeLength) * (p - SN2DP) * (p - EN2DP));
space = 2 * s / edgeLength;
return space;
}
}
我输出之前的距离并改变距离以查看其工作的剂量:
preDistance is: 339.245 changed distance is: 1000000.0
但是当我路线时,我发现距离仍然没有改变。为什么会这样? route.getDistance会从edge.getDistance()读取不同的值吗?边权重值是否存储在gh文件中或gh文件中只存储边的id和节点&#39; id构成了吗?