I need to calculate the shortest distance from a lat/lng GPS point P to a line segment described by 2 other lat/lng GPS points A and B.
'Cross-track distance' helps me to calculate the shortest distance between P and the great circle described by A and B.
However, this is not what I want. I need need the distance between P and the line segment of A-B, not the entire great circle.
I have used the following implementation from http://www.movable-type.co.uk/scripts/latlong.html
Formula: dxt = asin( sin(δ13) ⋅ sin(θ13−θ12) ) ⋅ R
where:
δ13 is (angular) distance from start point to third point
θ13 is (initial) bearing from start point to third point
θ12 is (initial) bearing from start point to end point
R is the earth’s radius
The following images hopefully demonstrate the problem I am trying to solve:
In the first image the Cross-Track distance, indicated by the green line is correct and indeed the shortest distance to the line segment AB.
In the second image the problem with cross-track distance is shown, In this case I would want the shortest distance to be the simple distance AP, but Cross-Track distance gives me the distance indicated by the red line.
How do I change my algoritm to take this into account, or check whether or not point X is within AB. Is it possible to do this computationally? Or is iterative the only possible (expensive) solution? (take N points along AB and calculate the min distance from P to all these points)
For simplicity purposes all lines in the images are straight. In reality, these are minor arcs on a great circle
First, some nomenclature:
Our arc is drawn from p1 to p2.
Our third point is p3.
The imaginary point that intersects the great circle is p4.
p1 is defined by lat1,lon1; p2 by lat2,lon2; etc.
dis12 is the distance from p1 to p2; etc.
bear12 is the bearing from p1 to p2; etc.
dxt is cross-track distance.
dxa is cross-arc distance, our goal!
Notice that the cross-track formula relies on the relative bearing,
bear13-bear12
We have 3 cases to deal with.
Case 1: The relative bearing is obtuse. So, dxa=dis13.
Case 2.1: The relative bearing is acute, AND p4 falls on our arc. So, dxa=dxt.
Case 2.2: The relative bearing is acute,AND p4 falls beyond our arc. So, dxa=dis23
The algorithm:
Step 1: If relative bearing is obtuse, dxa=dis13
Done!
Step 2: If relative bearing is acute:
2.1: Find dxt.
2.3: Find dis12.
2.4: Find dis14.
2.4: If dis14>dis12, dxa=dis23.
Done!
2.5: If we reach here, dxa=abs(dxt)
MATLAB code:
Sample outputs: Demonstrate all cases. See maps below.
Those same outputs on the map!:
Demonstrates case 1:
Demonstrates case 2.1:
Demonstrates case 2.2:
Credit to: http://www.movable-type.co.uk/scripts/latlong.html
for the formulas
and: http://www.darrinward.com/lat-long/?id=1788764
for generating the map images.
the above method requires cartesian 3D space arguments and you asked to use lat/lon arguments. To do the conversion use
For the rest of the 3D/R3 code or how to calculate distance to a path/route/track check https://sourceforge.net/projects/geokarambola/
Adding a Java version to
wdickerson
answer:For 100 - 1000m spherical problems, it is easy to just convert to cartesian space, using a equirectangular projection.
Then it continues with school mathematics:
Use the function "distance from line segment" which is easy to find ready implemented. This fucntion uses (and sometimes returns) a relative forward/backward position for the projected point X on the line A,B. The value is
If the relative position is between 0,1 the normal distance is taken, if outside the shorter distance of the both start and line-end points, A,B.
An example of such / or very similar an cartesian implementaion is Shortest distance between a point and a line segment