1

I'm implementing simple navigation, and I need to compute approximate location (GPS) on my route. Route is a list of points, so basically it looks like this:

                    (latX, longX)
(lat1, long1) o------o---------------o (lat2, long2)
                     |
                     |
                     o
                     (lat3, long3) - GPS My location

I have a two points (lat1, long1) and (lat2, long2) and my location (lat3, long3). How can I compute (latX, longX)?

Michał Jurczuk
  • 3,728
  • 4
  • 32
  • 56
  • Is [orthogonal](https://en.wikibooks.org/wiki/Linear_Algebra/Orthogonal_Projection_Onto_a_Line) [projection](http://math.stackexchange.com/questions/62633/orthogonal-projection-of-a-point-onto-a-line) what you need? Or you want to account for spherical geometry? – Petr Oct 28 '15 at 09:36
  • 1
    http://stackoverflow.com/questions/16429562/find-a-point-in-a-polyline-which-is-closest-to-a-latlng provides a starting point (or indeed most of the work, I think) – AakashM Oct 28 '15 at 10:36
  • Possible duplicate of [Projecting a point onto a path](http://stackoverflow.com/questions/30925042/projecting-a-point-onto-a-path) – Spektre Oct 29 '15 at 15:46

1 Answers1

0

Let me try to give you a sketch of how I would divide the problem into smaller steps.

  1. Consider the Earth as a perfect 3D sphere of radius r.

  2. Transform positions given by (lat, long) pairs into 3D points (x, y, z) on the surface of the sphere, i.e., with x^2 + y^2 + z^2 = r^2. You will also need the inverse transformation to get back to (lat, long) coordinates.

  3. Lets put (using 2 above)

    P1 := (x1, y1, z1) <-> (lat1, long1)

    P2 := (x2, y2, z2) <-> (lat2, long2)

    P3 := (x3, y3, z3) <-> (lat3, long3)

    P := (x, y, z) <-> (latX, longX)

  4. Let H be the plane containing the origin (at the center of the Earth) and the points P1 and P2. Note that H intersects the surface of the sphere in the path that joins P1 with P2 as in your illustration.

  5. Let (a, b, c) perpendicular to H. You can find it by solving the matrix equation Mv = 0, where M is the matrix whose rows are (x1, y1, z1) and (x2, y2, z2) and v is the unknown column with coordinates (a, b, c).

  6. Let Q = (x3, y3, z3) + u*(a, b, c), where u is the coefficient for which Q = (q1, q2, q3) lines in H. You can calculate u by solving the matrix equation Mv = P3, where M is the 3x3 matrix with columns (x1, y1, z1), (x2, y2, z2) and (-a, -b, -c), v is the unknown column (s, t, u) and P3 stands for the column (x3, y3, z3).

  7. Let P = w*Q, where w is the coefficient for which P = (x, y, z) with x^2 + y^2 + z^2 = r^2.

  8. Use the inverse of the transformation mentioned in step 2 above to compute (latX, longX) <-> (x, y, z).

Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51