How can one get samples between the start/end of a route

Are there any Java examples for using graphhopper to get the route between two points and sample it every x meters/seconds/w.e ? By sample I mean get a point with lat&long&elevation… etc

Edit: I do know there are the “snapped_waypoints” along the path but how are those picked ? I’d like to sample using my own metric (e.g. sample every 10m of road, or something around those lines).

Second edit: Since none’s answered until now I’ll try to make the question even more clear.
Say I get to waypoints lan=x1&x2 lat=y1&y2, would there be a formula to generate an x and y, between the latitude and longitude of those nodes, in such a way that x and y are “on a road” ?

If this is possible, the follow up question would be if its also possible to find the elevation for said x and y.

would there be a formula to generate an x and y, between the latitude and longitude of those nodes, in such a way that x and y are “on a road” ?

Yes, though you need to do a little work. Your first step would be iterating through the edges of a route (path.calcEdges) and then, for each edge, get the geometry (edge.fetchWayGeometry). You can then use one of the distance calcs (probably com.graphhopper.util.DistanceCalcEarth), or the edge distance, to measure how far you are along.

For your use case, it may be sufficient to sample from these waypoints. However, if you need the same sampling interval along the route (e.g. 10m) then you’ll need to interpolate between two waypoints. There are a couple of options there:

  • get the direction and use DistanceCalcEarth.projectCoordinate
  • have a nosey around here
  • if you’re happy with a very small amount of inaccuracy (which is probably below the level of the OSM data anyway, except maybe for very, very long straight edges) - then you could probably just use linear interpolation between the latitude/longitude of the two waypoints.

Alternatively, you may be able to export the route as a shape (using e.g. path.calcPoints) and finding some other Java package which can interpolate along a path. I haven’t looked into this, but if you’re using it for visualization it’s easy to do with SVG.

If this is possible, the follow up question would be if its also possible to find the elevation for said x and y.

Should be, in a similar way. I’m not sure if e.g. DistanceCalcEarth fully support elevation in some of the calculations (and those formulae linked above probably don’t) - but the error will be small. If you need exactly exact (which is probably beyond the accuracy of the data) then you can probably derive your own formulae.

1 Like

Routing in GraphHopper returns a points list that describes the route on road network (see here).
The result PointList has methods for getting latitude, longitude and elevation per point.

What I want is, indeed to sample every x meters, where “x” is unknown to me but will probably be in the range of 5-40 meters.

Currently I’m transforming lat/long into an x/y system using:

x = (longitude-longituide0)*cos(latitude1)
latitude - latitude1

and:

private val longituide0 : Double= 10
private val latitude1 : Double = 50

Since all these routes are in Germany.
The next step I envisioned is simply assuming the route between two waypoints is “straight” between any two neighbors and getting the straight line equation from the pairs of x,y (thus being able to find any point between two waypoints). Do you believe this is flawed in any way ?

Also, does DistanceCalcEarth.projectCoordinate simply take a waypoint (in lat/long) and then project a point “X” meters away in the given direction ? Or is there something fancier that DistanceCalcEarth.projectCoordinate does and my approach doesn’t ?

All in all, thank you for your help up until this point :slight_smile:

What I want is, indeed to sample every x meters

That’s what I tried to describe.

Do you believe this is flawed in any way ?

Nope - that’s what I described, if you’re happy with a straight line equation (which does not apply in polar coordinates but may be an acceptable approximation for your use case).

Or is there something fancier that DistanceCalcEarth.projectCoordinate does and my approach doesn’t ?

It does it correctly in polar coordinates (as far as I know). Likewise those other equations I linked.

@kodonnell

Do you have any idea what the magnitude of the error for using a straight line equation would be ? (Note, I think I’m changing my equation to Pseudo-Mercator… which should yield a better accuracy).

… In the end I will probably just go with DistanceCalcEarth.projectCoordinate … though I would have prefered the previous approach since it would have allowed me not to get into the whole Java lib, but currently I’m using Scala for my program, so integrating that shouldn’t be “too hard”.

Do you have any idea what the magnitude of the error for using a straight line equation would be ?

I suspect it’ll be negligible - e.g. over 10m it might be a millimeter or some such. Of course, if you’re using this for something serious, you will test it yourself and not take my word for it = )

Well, I’m not, that’s the thing. I’m using it for a POC, that’s why I don’t care about elevation either. The real thing will likely take a long time, for this one I barely have a few hours. Still, its quite decoupled in terms of elements, so presumably for the final product I will use OSM directly and a better formula that takes elevation and the eliptical shape of the earth into account.

Cool - good luck. Only piece of advice is to choose the appropriate tool for the job first - e.g. elliptical equations might be more exact, but if they only change the result by a trillionth of a meter (which is not only less than OSM accuracy, but even local topological variations like small pebbles!), then you they might not be worth the hassle.