How to use encoded values to assign new values to all edges?

Hi everyone!
I am currently working on an air pollution routing project which uses Graphhopper library.
I had created an additional decimal encoded value “smokeEnc” to hold expected AQI value of each edge in graphhopper. My objective is to use this smokeEnc value for greener routings. For this, I must assign this AQI value to all edges. This is how I had done it:
Create smokeEnc Decimal Encoded value(1):


Assign AQI value to all edges(2):

Create custom Greenest Weighting(3):

However on querying, something strange happens. While doing (2) all the edges(301586 edges) in the graph are filled with their expected (and always NON-ZERO) AQI values but on debugging the calcEdgeWeight function of Greenest Weighting at routing query time, I get to know only 983 edges have non-zero values of “smokeEnc” and all others have 0 instead :slightly_frowning_face:. Moreover, the route I get from Greenest routing actually has a very poor AQI score, poor even in comparison to FasterWeighting or ShortestWeighting etc. I find myself really lost. What mistake am I doing and what things have I misconsidered while working on this?

1 Like

What happens if you do:

@Override
    public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {


        double smoke = smokeEnc.getDecimal(false, edgeState.getFlags());
        double calcEdgeWeight = smoke;

        System.out.println("******************** Smoke values: " + smoke)
        
        return calcEdgeWeight;

Please note that you should not use:

smokeEnc.getDecimal(false, edgeState.getFlags());

because

edgeState.get(smokeEnc);

takes into account the direction of the edgeState. (and it also does not use the very low level .getFlags() which is likely to get deprecated at some point)

Yes this seems to give more accurate results i.e. the edges all have non-zero values now and the greenest route is also having lowest AQI score. I wonder why this is the case? Thanks for the help, btw :grinning:

Oh this is unfortunate. It is strangely seemingly giving the correct answers now. I wonder why that is the case?

I am glad it worked. Perhaps try what @karussell suggested and see if you get the same results or better.

I wish I could tell you why it worked, but I am not entirely sure. I saw this being done in the CurvatureWeighting and it solved a similar problem I had.

Could it be that you set storeTwoDirections=true for your SmokeEncodedValue and did not fill both directions? Then edgeState.get(encVal) might return 0 for those cases.

To fix this either set storeTwoDirections=false if you do not need this feature or fill both directions:

edge.set(encVal, 10)
edge.setReverse(encVal, 10)

Also defaulting to 0 might not be what you want as a weight.

And instead of your own weighting you should prefer the more powerful CustomWeighting where you can tweak it easier and per-request.

Oh thanks I will try these out :grinning: