# How do I calculate a xyz-position of one GPS position relative to another?

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.

• is the an ongoing calculation ( in code) or a one time analysis? Apr 19, 2012 at 13:12
• Do you want an algorithm or just pointers to software that will do the calculations? Apr 19, 2012 at 14:58
• I need ongoing calculation written with python, both algorithm and library are OK
– Lara
Apr 27, 2012 at 9:46
• Nov 3, 2014 at 5:28
• May 26, 2020 at 15:50

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 (docs.geotools.org/latest/javadocs/org/geotools/geometry/jts/…, com.vividsolutions.jts.geom.Coordinate, org.opengis.referencing.crs.CoordinateReferenceSystem)) Apr 19, 2012 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. Apr 19, 2012 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 Apr 19, 2012 at 14:02
• @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. Apr 19, 2012 at 14:23
• It may be that I misunderstand the question, so you may be right. Maybe sycoso could clarify? Apr 19, 2012 at 14:27