Question

I know there are other questions similar to this one, but most of them deal with only converting one to the other. But I am searching for a algorithms that convert to and from each other. Simply using one of each has not produced the desired results.

For my purposes a unit sphere is more then acceptable. Any radius value would be 1.

Here are my current methods for performing this, in simple psudocode.

From latitude and longitude to a point on a unit sphere.

x = cos( longitude ) * sin( latitude )
y = sin( longitude ) * sin( latitude )
z = cos( latitude )

From 3D coordinates on a unit sphere to latitude and longitude.

latitude = acos( z )
longitude = atan2( x, y )

However these are not reversible and my trigonometry is not what it should be.

Was it helpful?

Solution

Converting from lat/long to xyz is always possible, but going from xyz to lat/long fails when sin(lat) == 0. There is no solution to this in lat/long space so just stay away from it.
Other than that your formula just has a small error where atan2 takes y then x instead of x then y.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top