3D coordinates on a sphere to Latitude and Longitude

45,495

Solution 1

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

Solution 2

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

Solution 3

This helped using Javascript/THREE.js:

var lat = 90 - (Math.acos(y / RADIUS_SPHERE)) * 180 / Math.PI;
var lon = ((270 + (Math.atan2(x , z)) * 180 / Math.PI) % 360) -180;
Share:
45,495

Related videos on Youtube

Roy T.
Author by

Roy T.

At the age of 14 I started programming in PowerPoint’s VBScript environment, quickly after that I switched to Visual Basic 6 learning basic programming skills. A few years later I applied these skill to write my first ‘database’ application for the Salida youth crisis relief center. The application used VB6 and flat files on an FTP server to mimic a real database. Around the same time I wrote my first game, a two player top-down shooter. A year later I rewrote the database application in C#, now using a real database (MySQL) which allowed users to really work together. This application is still used daily by approximately 20 people at Stichting Cardan. Since 2008 I started focusing on games. In 2008 I discovered Microsoft’s XNA framework (a managed DirectX wrapper for PC, Xbox and Windows Phone). Since then I’ve been an active member of the XNA community. While I was learning the framework and general game development techniques I started keeping a blog. At this blog I periodically post tutorials and code snippets. I also wrote a few tutorials for popular XNA sites like www.ziggyware.com (now defunct) and www.sgtconker.com (renamed to MadGameDev). With these tutorials I won a couple of prizes. In 2010 I met a few game designers and artists working on the game Hollandia. They had just won a Dutch Game Award but their programmer was unable to continue working on the game engine, which caused the project to stall. I rewrote most of their engine and coupled it to an existing physics framework. Around the same time I worked with a 15 man strong team on an extensive web shop project for Q-Free. We developed a process that automatically pulled new releases from source control and integrated them into our web shop. While finishing up my Bsc. in Computing Science I worked at Science LinX (2009-2012) where I grew from a system administrator to serious game developer with a diverse set of responsibilities, from building exhibits to managing projects. January 2015 I graduated Cum Laude and I now poses a master's degree from Utrecht University. During my master I also I interned at Abbey Games (creators of Reus). After that I worked SilverFit where I used the Kinect camera to help the elderly recover or stay fit. Currently I work at the Dutch e-tailer Bol.com as an expert engineer and cloud liaison.

Updated on July 19, 2020

Comments

  • Roy T.
    Roy T. almost 4 years

    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.

    • PKCLsoft
      PKCLsoft over 7 years
      I found that to get the lat/long to have the correct sign using SceneKit, I needed: float latitude = -((float)acosf(result.localCoordinates.y / sphereRadius) - M_PI_2); //theta float longitude = M_PI - ((float)atan2f(result.localCoordinates.z, result.localCoordinates.x));/ //phi if (longitude > M_PI) { longitude = longitude - (2.0 * M_PI); }
  • Roy T.
    Roy T. about 13 years
    Wow, thanks for the neatly layouted answer, I'll test this right now. And if everything works out I'll accept it in a bit :).
  • Roy T.
    Roy T. about 13 years
    I'm getting very strange results with these formula, so I'm afraid this doesn't seem to be the solution.
  • Roy T.
    Roy T. about 13 years
    I'm using this a spherical coordinates for a perfect sphere (in a game) so I don't have to use real-world complexities :)
  • Jaydee
    Jaydee about 13 years
    I realised that after posting, I really should read the question properly sounding off. I've done a fair bit of this in the past so I see lat/long and start pontificating.
  • Roy T.
    Roy T. about 13 years
    Your latitude seems to work, but your longitude is very erratic
  • Roy T.
    Roy T. about 13 years
    nvm, you are correct. I didnt look at your picture, you use Z = up, I used Y = up. Also you swapped lat and lon.
  • Curd
    Curd about 13 years
    @Roy T.: probably depends on the definition which one of the two axis is x- and -y. Try it with x- and y-axis exchanged (You didn't specify the coordinate system exactly).
  • Motionharvest
    Motionharvest over 8 years
    Would you be willing to help me figure out the reverse of this. I have the lat, lon, and radius, and I'm trying to figure out the {x,y,z} of a THREE.Vector3.
  • ppareja
    ppareja about 7 years
    In my case I had to change the calculation of lon to the following: var lon = ((270 + (Math.atan2(x , z)) * 180 / Math.PI) % 360) -360;
  • Donny V.
    Donny V. about 3 years
    What is the RADIUS_SPHERE = ?
  • Donny V.
    Donny V. about 3 years
    Is it the earths radius? 6378140

Related