# Convert latitude/longitude to three dimensional space

I am working to convert longitude to latitude to three-dimensional space in the form of x,y,z.

``````def transform(lat, lon):
return x,y,z
``````

Also, I need to make a function that reverses the operation. ie. convert back x,y,z into lon/lat.

``````lat = np.degrees(np.arcsin(z))
``````

This gives the value of latitude by inversing equation `z = np.sin(np.radians(lat))`. but I couldn't recover the longitude values. I tried solving the equations but it's returning a different value from what I expected.

I know taking the inverse of sine returns multiple values, I tried multiple values but could not get anyone close to the expected value.

I came up with a solution:

``````def inverse_transform(x,y,z):
lat = np.degrees(np.arcsin(z))

lon = 0

if(lon_1 > 0 and lon_2 > 0): # for 0 to +90
lon = lon_1

elif(lon_1 < 0 and lon_2 > 0): # for -90 to 0
lon = lon_1

elif(lon_1 < 0 and lon_2 < 0): # for -180 to -90
lon = -lon_1 - 180

elif(lon_1 > 0 and lon_2 < 0): # for -180 to -90
lon = 180 - lon_1

return lat,lon
``````