我们正在考虑制作一些特殊的边,暂时不在布线中使用。
我发现一个问题与我们的问题非常相似:GraphHopper是否支持动态边缘权重?所以我不使用CH算法,在过滤掉我需要的边缘后,将它们的距离更改为巨大的值。
下面是我在class GraphHopper
中添加的两种方法。我添加了一个hopper.flush
,但结果仍然不正确。
我编写了方法processChange()
,试图表示交通数据的反馈。如果你给出交通堵塞或构造并调用CCD_ 4。它将选择距离该位置点一米远的边,并将这些边的距离更改为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文件中,还是只存储边的id和由它组成的节点的id?
您需要通过prepare.chWeighting=no
启用灵活性模式
例如,请参阅我描述如何在实时中集成交通数据的博客文章