Add encoded value to graphHopper

I, i want to extend the functionality of graphhopper.
I want to create a custom profile that calculate a route between point A to point B in a city of Bologna based on pollution.
For achieving that, i want to add a new encoded value to all edges with a random value from 0 to 1.
After that i want the routing algorithm to take into consideration also that encoded value to calculate the best path.
For now I uploaded my pbf file of Bologna but i have difficulty to add new encoded values and adding a random value to all edges.
Can you please help me in achieving it?

1 Like

There should be several posts in this forum where adding encoded values and using them in custom models is explained. You can also use some of the pull requests that added encoded values as a bluebrint, like this one: Add footway encoded value by easbar · Pull Request #2707 · graphhopper/graphhopper · GitHub

@easbar: Thank you for providing such a clear example. Correct me if I’m mistaken, but it seems like your change involved exposing a feature that already existed in OSM to GraphHopper.

I would like to add a new feature to OSM data and then use that feature in GH. I imagine the second part of such an effort would follow your example, but do you have any suggestions on how to do the first part?

I believe @Andrea_Zavatta is working on air pollution; I am working on road risk (=collision risk) but it seems like we are both looking to add an attribute to the OSM data.

Here’s how I currently see OSM data:

|            geometry| osm_id|code|        fclass|name|ref|oneway|maxspeed|layer|bridge|tunnel|
|LINESTRING (-122....|4304424|5134|secondary_link|    |   |     F|       0|    1|     T|     F|

I imagine something like:

|            geometry| osm_id|code|        fclass|name|ref|oneway|maxspeed|layer|bridge|tunnel|road_risk|
|LINESTRING (-122....|4304424|5134|secondary_link|    |   |     F|       0|    1|     T|     F|     high|

Should I simply create my own shapefiles in this manner, or is there a better way to do it? I imagine that this could be a separate key-value pair (a HashMap) keyed off the osm_id. Would that be considered a hack?



@easbar: I posted a related question on Reddit and someone from GraphHopper responded. I guess this might be the way to go for features that don’t already exist in OSM:

Obviously you should not put your risk data into public OSM (unless of course it is of common interest according to OSM guidelines). But adding the data to your local OSM file (if that is easy for you) would be a solid solution to your problem, I wouldn’t consider this a ‘hack’ at all. Using a hash map keyed off the osm_id would also work, since the osm_id will be available to your custom tag parser.

1 Like

@easbar: I forked the repository to try to make my change. The current master branch has some changes to the FOOTWAY example you provided, so I had to improvise a bit. The good news is that my change compiled successfully. The bad news is that the UI won’t accept road_risk in a custom model.

Even if I botched the insertion of the data, it should render the the default ‘medium’ value, I think. Is there any way I can ask you to take a look at my change? If so, I would be very grateful for any hints about what I’m doing wrong. Thanks!…larsskaug:graphhopper:road-risk?expand=1

This is what I’m trying for a custom model:

“priority”: [{
“if”: “road_risk == LOW”,
“multiply_by”: “1.5”
}, {
“if”: “road_risk == HIGH”,
“multiply_by”: “0.5”

@easbar: I didn’t see any changes in your PR to expose “footway” to the front-end JavaScript. Is that because “road_class” already was? My change is a new tag at the level of road_class. Do you know if I need to do something to expose it to JavaScript?

You can still send the custom model via pressing Ctrl+Enter. But usually all encoded values should be accepted in the UI as they are all listed in /info.


I got my change to work. It was simply a matter of changing the config file and deleting the cache to have it rebuilt. Thank you so much for your help, @easbar!

1 Like

I want to do the exact same thing but with air pollution, i would like to have an encoded value that can be either HIGH, MEDIUM, LOW as yours.
after that i would like to create a custom profile that use that encoded value to prioritize some streets.
I want to to know how you added these encoded values to the edge of the graph also.
Can you explain me how you achieved the result?
And share me the repository?

I, i’m trying to replicate it, but i receive this error: java.lang.IllegalArgumentException: DefaultEncodedValueFactory cannot find EncodedValue pollution

can you please help me? this is my repository:

java.lang.IllegalArgumentException: DefaultEncodedValueFactory cannot find EncodedValue pollution

You probably forgot adding it to the DefaultEncodedValueFactory then?

i added it in this commit add pollution encoded value · graphhopper/graphhopper@4e9aa96 · GitHub
I don’t know why it doesn’t compile

It doesn’t compile? You said you got an exception which would mean it compiles but yields an error during runtime.

Sorry, it compiles but i receive this error. Can you please help me resolve this? I added it to DefaultEncodedValueFactory but it doesn’t work

It is probably best to use a debugger to see where and why the exception is thrown.