Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D coordinates on a sphere to Latitude and Longitude

I've got the following information:

There exists a sphere with origin (0,0,0) and radius R. After doing a ray-sphere intersection I know a point (XYZ) in 3D space that is on the sphere (the exact position in 3D space where the line pierces the sphere hull).

For my program I'd like to calculate the Latitude and Longitude of the XYZ point on the sphere, but I can't think (or Google) up a way to do this easily.

So in short, the function that I'm trying to write is this:

public static LatLon FromVector3(Vector3 position, float sphereRadius) {     return Latitude and Longitude } 

Does anybody know how to do this? As a reference this Wiki SVG file might be helpful:

Geographic coordinates

Update:

Thanks for all the helpful answers, so in the end I went with this code:

 public static LatLon FromVector3(Vector3 position, float sphereRadius)     {         float lat = (float)Math.Acos(position.Y / sphereRadius); //theta         float lon = (float)Math.Atan(position.X / position.Z); //phi         return new LatLon(lat, lon);     } 

Now I've got to think of which answer helped me the most to accept :P.

like image 414
Roy T. Avatar asked Apr 15 '11 08:04

Roy T.


People also ask

How do you convert latitude and longitude to spherical coordinates?

To convert a point from Cartesian coordinates to spherical coordinates, use equations ρ2=x2+y2+z2,tanθ=yx, and φ=arccos(z√x2+y2+z2).


2 Answers

I guess it should not be difficult to find the spherical polar coordinates from x,y,z (3d-coordinate system).

  1. r is always constant if it's on surface.

    enter image description here

  2. (90 - θ) your latitude (negative means it's on the bottom) as it's measured from top.

    enter image description here

  3. φ is your longitude. (but not quite sure about longitude system)

    enter image description here

Also check this diagram from wikipedia.

enter image description here

like image 125
Santosh Linkha Avatar answered Sep 22 '22 12:09

Santosh Linkha


lat=atan2(z,sqrt(x*x+y*y)) lng=atan2(y,x) 

Using formulas with atan2() is more convenient. You don't have to add/subtract pi/2 or care about sign issues in different quadrants or division by zero.

lat will be >0 in the northern hemisphere
lat will be <0 in the southern hemisphere
lng will be >0 in the eastern hemisphere
lng will be <0 in the western hemisphere

like image 44
Curd Avatar answered Sep 25 '22 12:09

Curd