0

我们正在考虑制作一些特殊的边缘暂时不在路由中使用。

我发现了一个与我们的问题非常相似的问题: GraphHopper 是否支持动态边缘权重? 所以我不使用 CH 算法,在过滤掉我需要的边缘后将它们的距离更改为巨大的值。

基础是我添加的整个两种方法class GraphHopper。我添加了一个hopper.flush,但结果仍然不对。

我写的方法processChange()试图表示交通数据的反馈。如果您提供交通拥堵或施工的位置(纬度和经度)并致电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 和由它构成的节点的 id?

4

1 回答 1

0

您需要通过以下方式启用灵活性模式prepare.chWeighting=no

例如,请参阅这篇博客文章,其中我描述了如何实时集成交通数据

于 2015-03-23T17:11:32.327 回答