I have an imported version of a GPX file plotting the route of a trail. I also have a list of coordinates for waypoints along the route. I am trying to calculate how far along the route each waypoint is.
The cleaned up route data can be here: https://pastebin.com/8KDcvMex and can be imported directly:
ClearAll[data];
ToExpression@Import["https://pastebin.com/raw/8KDcvMex", "String"];
Using that data I then used the following code to plot the trail:
trail = GeoPosition /@ data[[All, 1]]
Then I bring in the waypoint I want to find the location of:
waypoint = GeoPosition[{34.56544401, -77.90251801}]
I can find the first point on the route with this:
start = First[trail]
And find the straight line distance with this:
GeoDistance[waypoint, start]
But how do I have Mathematica measure that distance along the path instead of straight distance?
Additionally, I have found I can find the closest route point with this:
GeoNearest[trail, waypoint]
And I can find out far the waypoint is from one of the route points with this:
Min[GeoDistance[waypoint, trail]]
In looking for possible solutions, I found this example on the Wolfram Community and he does some cool stuff using Accumulate
as a way of calculating overall distance. I could use that function combined with finding the nearest route point and have the answer, but that won't always work. You see, there are gaps in some of the GPX route coordinates where at times there is up to half a mile between coordinates. I have cases where waypoints are returning the same route point as closest because they really are the closest in the dataset, even though the waypoints themselves are separated by some distance. In cases like that, my distance calculations could be off by a pretty substantial bit.
Is there another way to do what I am trying to do, or am I stuck with either the drawbacks of finding the distances to the nearest route point or manually entering the waypoint coordinates into the appropriate spot within the route points?
EDIT:
Using techniques described in the Wolfram Community post above, I was able to approximate the result, but it still doesn't account for the distance between the waypoint and the nearest route point.
Here is what I did:
First I calculated the cumulative distance of each route point from the starting point (I stripped units for simplicity).
distances =
Accumulate[Prepend[QuantityMagnitude[GeoDistanceList[trail]], 0]];
Then I combined the two datasets:
combined = Thread[{trail, distances}]
To extract the relevent data, I used:
nearest =
GeoNearest[combined[[All, 1]],
waypoint][[1]]; Select[combined, #[[1]] == nearest &]
Again, this gets close since the output is as expected with the geoposition of the nearest route point and the distance to that route point, but isn't accurate enough.
A couple notes on the code above. Remember that Mathematica returns GeoNearest[combined[[All, 1]], waypoint]
as a single item list so you will either need to call the first item with [[1]]
as I did below or use First@GeoNearest[combined[[All, 1]], waypoint]
. Also, if I don't precalculate nearest
and use the following code, Mathematica gets the right answer, but takes several minutes compared to near instant results:
Select[combined, #[[1]] ==
GeoNearest[combined[[All, 1]], randomblaze][[1]] &]
Definitely something in code optimization that I am missing.
GeoPath
? I’ve been playing with it a bit, seems promising, but not really there yet. Your edited approach is interesting too. $\endgroup$