5

I have two GPS positions (lon, lat, alt) and want to get the position of one relative to the other one. (in meters, as x, y, z)

How do I do that?

Edit: The locations have a distance of up to three kilometers And: I do NOT just want the distance.

nmtoken
  • 13,355
  • 5
  • 38
  • 87
Lara
  • 51
  • 1
  • 1
  • 4

1 Answers1

3

You can get the [x y z] vector of each as follows:

r = 6371000 + alt
x = r*cos(lat)*cos(lon)
y = r*cos(lat)*sin(lon)
z = r*sin(lat)

Then subtract the vectors from each other. Make sure your lat and lon are expressed in radians.

Note that I have assumed a spherical model of the earth, which will yield very good answers in proportional terms — less than 0.3% worst case, based on a scratch-of-the-head "calculation", and much, much less for points that are within a few degrees of each other. If you need an answer that's good down to the meter regardless of the distance, you will need to use an ellipsoidal model, such as WGS 84 (thanks to @whuber for pointing this out). In that case, you can replace 6371000 with the equatorial radius, 6378137, and multiply z by (1 – 1/298.257223563).

  • This does not give you your result in meters. Consider using a library to deal with this, for Java, JTS/GeoTolls offers the orthodromicDistance function (http://docs.geotools.org/latest/javadocs/org/geotools/geometry/jts/JTS.html#orthodromicDistance(com.vividsolutions.jts.geom.Coordinate, com.vividsolutions.jts.geom.Coordinate, org.opengis.referencing.crs.CoordinateReferenceSystem)) – atlefren Apr 19 '12 at 12:27
  • @atlefren: The OP was asking for the relative positions of two geo-coordinates on an orthonormal basis. Did I not do that? Is there a mistake in my math? I don't understand how the question could be interpreted as referring to orthodromic distance; it clearly indicates relative positions, not distance. Given the steady trickle of down-votes, I'm guessing I must have gotten it wrong; I just don't understand how. – Marcelo Cantos Apr 19 '12 at 13:44
  • he wrote, in the parentheses: "in meters, as x, y, z". Although your mats are correct, the fact that he asked for meters and that "most people" want to know distance, not relative distance between two points, which I don't see any errors in as such – atlefren Apr 19 '12 at 14:02
  • 1
    @atlefren: Then it appears that I'm being down-voted for no good reason. Whatever it is that most people want, the OP clearly and unambiguously asked for a 3-vector expressing the relative positions. The presence of the term "meters" makes absolutely no difference, since the 3-vector derived in my answer is expressed in meters. – Marcelo Cantos Apr 19 '12 at 14:23
  • It may be that I misunderstand the question, so you may be right. Maybe sycoso could clarify? – atlefren Apr 19 '12 at 14:27
  • One possible reason for the downvotes is that this answer is incorrect: it assumes a spherical model of the earth, whereas GPS coordinates always use an ellipsoidal model (WGS84 or an extremely close approximation). The error will usually be huge (compared to typical GPS error). – whuber Apr 19 '12 at 14:56
  • @whuber: Thank you for pointing out the issue, but I must take issue with your interpretation. The answer is not incorrect. True, it relies on a couple of approximate absolute 3-space coordinates. But the question wan't asking for absolute coordinates, it was asking for relative coordinates, for which the answer doesn't produce huge errors, proportionally speaking. I will concede that the answer could be improved by clarifying the subtleties involved, so I have added a qualifying paragraph. – Marcelo Cantos Apr 19 '12 at 23:51
  • Sorry for not answering...

    To make it clear: I don't just need the distance, I want a coordinate system with real-life locations in it. The maximum size is about three kilometers, rather less.

    – Lara Apr 27 '12 at 10:55