# I have equations for getting x,y,z given latitude, longitude, and altitude. How do I reverse them?

I am using equations that look like the following to get x, y, and z given latitude, longitude, and altitude.

x = (6378137 + alt) * cos(lat) * cos(lon);
y = (6378137 + alt) * cos(lat) * sin(lon);
z = (6378137 + alt) * sin(lat);


If I already know x,y,z, I want to solve for lat, lon, and alt.

For the purposes of the solution it's just a problem of the form:

x=A*cos(B)*cos(C)
y=A*cos(B)*sin(C)
z=A*sin(B)


I know x, y, and z. I want to solve for A, B, and C.

Normally this should be easy. I've got 3 unknowns and 3 equations. I'd just solve for one variable and substitute it in the second equation, etc. But I'm having trouble doing this with the trig functions.

For example, if I try to solve them I end up with

C = atan(y/x)
B = acos( x / (A*cos( atan(y/x) )) )
A = z / sin( acos( x / (A*cos( atan(y/x) )) ) )


I don't know how to solve that last one for A.

EDIT: The following works. Thanks!

    double alt = Math.sqrt(x*x + y*y + z*z) - a;
double lon = Math.atan2(y,x) * MathUtils.num180DivPI;
double lat = Math.asin(z / (alt+a)) * MathUtils.num180DivPI;

-
$A=\sqrt{x^2+y^2+z^2}$... more or less, look up the conversion formulae from spherical to Cartesian coordinates. You will want to use the two-argument arctangent for this application instead of the usual arctangent. – J. M. Aug 29 '11 at 1:51
If this is for a real life app, you really need to take into account that the Earth's radius is not a constant 6378137 meters. – Jyrki Lahtonen Aug 29 '11 at 9:25

## 1 Answer

"A" should be the distance of of (x,y,z) from the origin, or $$\sqrt{x^2+y^2+z^2}$$ as J.M. commented.

-