Search code examples
mathanglegeo

How to find relative angle from one gps coordinate vector to other?


I have extracted a set of three coordinates and I want to find the relative angles between their two vectors.

  • P1: 57.3112, 25.24871
  • C1: 57.31105, 25.24875
  • T1: 57.31086, 25.24803

Like: V1 = C1 - P1, V2 = T1 - C1 and then get angle from V1 to V2.

But I am afraid that cartesian functions won't cut it here, because in actual application, the coordinates could span different latitudes and longitudes.

I have searched google but the functions I have seen, do not convince me. I also tried some in my app, but the returned angles didn't return the expected results.

For these three points, deducing visually, I expect the angle to be somewhere around 120-140 degrees.

I have limited knowledge in geographic calculations and I'm out of clues here.

How do I calculate the angle?


Solution

  • Using formulas from this page, calculate bearings (azimuths) from middle point to the first one and to the third one.

    θ = atan2( sin Δλ ⋅ cos φ2 , cos φ1 ⋅ sin φ2 − sin φ1 ⋅ cos φ2 ⋅ cos Δλ )
    where   φ1,λ1 is the start point, φ2,λ2 the end point (Δλ is the difference in longitude)
    

    I emphasize that for "angle to turn" bearings should be calculated from the turn point - because for arc A-B bearings will differ in the starting and ending points (except for specific cases like meridian or parallel arcs). At the next picture direction v differs from direction u (shortest path at the sphere is so-called "big circle arc")

    enter image description here

    Having two bearings, you can get their difference to know needed angle (don't forget to treat transition over 360 degrees)


    There is also method from spherical trigonometry - spherical cosine theorem (rearrangement part here), but it requires to find arc lengths for all triangle sides (might be found using the first link - but more calculations)