0

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

def transform(lat, lon):
    x = np.cos(np.radians(lat)) * np.cos(np.radians(lon))
    y = np.cos(np.radians(lat)) * np.sin(np.radians(lon))
    z = np.sin(np.radians(lat))
    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.

1 Answer 1

1

I came up with a solution:

def inverse_transform(x,y,z):
  lat = np.degrees(np.arcsin(z))
  lon_1 = np.degrees(np.arcsin(y / np.cos(np.radians(lat))))
  lon_2 = np.degrees(np.arcsin(x / np.cos(np.radians(lat))))

  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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.