0

We are thinking to make some special edges not to use in the routing temporarily.

I found a question very similar to our question: Does GraphHopper support dynamic edge weights? So I not use the CH algorithm, change their distance to huge value after I filter out the edges I need.

Underlying are the whole two method I add in class GraphHopper. I added a hopper.flush but the result still not right.

I write the method processChange() trying to represent the feedback of traffic data. If you give the location(lat&lon) where having a traffic jam or construction and call the processChange(). It will choose the edges which are one meter away from this location point and change the distance of these edges to 10000000 so that these edges will not to use in the routing temporarily. The method pointToLine() is just to calculate the distance between the location point to a edge.

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;
    }

}

I output the previous distance and changed distance to see dose it work:

preDistance is: 339.245     changed distance is: 1000000.0

But when I route, I found the distance still not change. Why will this happen? Does route.getDistance will read diffierent value from edge.getDistance()? Do edges weight values be stored in the gh-file or the gh-file just store the edge's id and the nodes' id constituted of it?

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

0

You'll need to enable the flexibility mode via prepare.chWeighting=no

E.g. see this blog post where I describe how to integrate traffic data in real time

Karussell
  • 17,085
  • 16
  • 97
  • 197
  • Ah, okay. As it seems you already have this disabled. Hmmh, maybe you had a reload in-between and you need to do a graph.flush before that? – Karussell Mar 23 '15 at 17:13
  • I added a hopper.flush but the result still not right. I am new in working with Graphhopper. Is the place where I add flush() not right? I post the whole code here. @Karussell – cindy chan Mar 24 '15 at 07:52
  • no need to call flush if there is no reload in-between. Are you sure the `if (distance <= 1)` is called? – Karussell Mar 24 '15 at 08:58
  • Yes, I sure the `if (distance <= 1)` is called. I outputted the edges' ID and nodes' ID to check is there any chosen edges, the result was: `changeEdgeID is: 2980 startNodeId[i]: 1725 endNodeId[i]: 1727` `changeEdgeID is: 2981 startNodeId[i]: 1723 endNodeId[i]: 1725`. I choose a route which will go through these two edges, output the distance and time, then call `processChange()` and route again. The output distance and time should be different after calling the `processChange` but they do not change. – cindy chan Mar 25 '15 at 03:36
  • Do edges weight values be stored in the gh-file? Do the 'route.getdistance()' read the edge's distance from the gh-file or find the start node and end node of the edge then calculate the distance between two nodes? @karussell – cindy chan Mar 26 '15 at 07:47
  • The distance and speed is stored in every edge and getDistance sums all the involved distances. There is no expensive node-to-node distance calculation involved, this is done on import. – Karussell Mar 26 '15 at 14:45