Find closest point on road irrespective of distance

I am trying to do road snapping to the closest point on a road irrespective of the distance to the next road. To do the road snapping I am using

QueryResult queryResult = graphhopper.getLocationIndex().findClosest(latitude, longitude, EdgeFilter.ALL_EDGES);

When I try this for a location that is very far from my road network I get an IllegalStateException:

java.lang.IllegalStateException: Calculate snapped point before!

How can I ensure that findClosest(..) always returns a point? I have looked around and got stuck somewhere in findClosest(..) in the LocationIndexTree.java

This is kind of contradicting itself: closest point vs. irrespective of the distance. What did you mean here?

If you want to increase the “acceptable radius” you could increase the setting index.max_region_search from 4 to 8.

How can I ensure that findClosest(..) always returns a point?

You should better not, because it means that in worst case one request would traverse the whole graph.

I was thinking of the case where my map is some city but the snap request could be anywhere in the world. I would like GH to snap to the closest road even if it is very far away.

It seems that if the closest point is a certain distance away from the request then GH will not return it and instead raise an exception.

Ok, so this index.setMaxRegionSearch(100); works really well. Controls how far snapped location may be from requested location…

Is it possible to configure via config file?

Yes: https://github.com/graphhopper/graphhopper/blob/1c7a0ed5837802f952c710ad19875dbfd59bafa7/core/src/main/java/com/graphhopper/GraphHopper.java#L500-L501

1 Like

@easbar thanks!
Does increasing the radius affect the performance of the importing and queries?

Not the importing, but it can affect the query time. More specifically there can be single requests that will be very slow (when no point is found quickly). Generally it is very hard to tune these parameters for large maps that include very dense cities as well as sparse country areas, so use with care.

1 Like

In case of increasing the index.setMaxRegionSearch, can the algorithm consume more CPU or RAM ?

Yes. It is recommended to avoid changing this without experience.