Conversion between Spherical and Cartesian Coordinates Systems

When representing the location of objects in three dimensions there are several different types of coordinate systems that can be used to represent the location with respect to some point of origin. Locations on a Virtual Earth map are represented by the spherical coordinate system. The spherical coordinate system uses three parameters to represent a point in space, a radial distance (r) from a point of origin to the point in space, a zenith angle (θ) from the positive z-axis, and azimuth angle (Φ) from the positive x-axis. In Virtual Earth the radial distance is the radius of the Earth, the zenith angle is the latitude, and the azimuth angle is the longitude. The origin is considered to be the center of the Earth. The Cartesian coordinate system is the standard x, y, and z coordinate system that is commonly used to represent a point in space. The following figure gives a graphical representation of the spherical coordinate system. It also points out the Cartesian coordinate system equivalent dimensions.

y1pdRfi8V7SDHo8yh_BSs3FkwdrJE3pvP7jPRfLAKN5ytXCsf3b18lczB_E1I744YLxDX3joMEQDms

Figure 1 Spherical Coordinate System

By looking at the Spherical coordinate figure we can calculate the x, y, and z components of the Cartesian coordinate system. We will start with the z component because it is the easiest to calculate. Since the z component is in the same plane as r and the θ angle, and this plane is perpendicular to the xy plane which the Φ angle is in, we can see that the z component is equivalent to the adjacent side of the triangle, and r is equivalent to the hypotenuse. Taking this information into consideration we can calculate the z component as follows:

clip_image002

To calculate the x and y components we need to first calculate the projection of the radius line in the xy plane. This projection would be equivalent to the hypotenuse of the two triangles in the xy plane. The following formula will give use the length of the projection of r:

clip_image002[7]

The x component is equal to the projection of the r projection in the xz plane. We can calculate the x component using the following formula:

clip_image002[11]

The y component is equal to the projection of the r projection in the yz plane. We can calculate the y component using the following formula:

clip_image002[13]

Now that we can convert from Spherical to Cartesian coordinates, it would be useful to be able to do the reverse. Using Pythagorean Theorem we can calculate the length of r.

clip_image012

Using the formula for the z component we can derive the formula to calculate the angle θ.

clip_image002[15]

clip_image002[17]

By rearranging the formulas for the x and y components we can solve the value for the Φ angle.

clip_image002[19]

clip_image002[21]

clip_image002[23]

Application

Before we convert the coordinates we need to create a definition for a Cartesian coordinate object. The following function is how we will define a Cartesian coordinate:

function Cartesian(x,y,z)
{
this.X = x;
this.Y = y;
this.Z = z;
}

Listing 1 Cartesian Coordinate Object

For the conversion between Spherical and Cartesian coordinates we will take in a VELatLong object and use a constant value for the radius of the earth. If we want to perform more advance calculations we could extract the altitude value of the VELatLong object, if it’s relative to the WGS 84 ellipsoid, and add the radius of the earth to get the total distance from the center of the Earth to a point in space. We will return a Cartesian coordinate object that contains the x, y and z values. This will make it much easier to follow trace the code.

var earthRadius = 6367; //radius in km
function
convertSphericalToCartesian(latlong)
{
var lat = DegtoRad (latlong.Latitude);
var lon = DegtoRad (latlong.Longitude);
var x = earthRadius * Math.cos(lat)*Math.cos(lon);
var y = earthRadius * Math.cos(lat)*Math.sin(lon);
var z = earthRadius * Math.sin(lat);
return new Cartesian(x,y,z);
}

Listing 2 Spherical to Cartesian coordinate conversion

For the conversion from Cartesian coordinates to Spherical coordinates we will take in Cartesian coordinate object. This function will return a VELatLong that represents our spherical coordinates.

function convertCartesianToSpherical(cartesian)
{
var r = Math.sqrt(cartesian.X* cartesian.X + cartesian.Y* cartesian.Y+ cartesian.Z* cartesian.Z);
var lat = RadtoDeg(Math.asin(cartesian.Z/r));
var lon = RadtoDeg(Math.atan2(cartesian.Y, cartesian.X));
return new VELatLong(lat,lon);
}

Listing 3 Cartesian to Spherical coordinate conversion

The following post has additional information on the RadToDeg and DegToRad methods: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!257.entry

23 thoughts on “Conversion between Spherical and Cartesian Coordinates Systems

  1. I went through websites looking for a simple explanation of the transformation between Cartesian and Spherical coordinates. Your derivation of the formula really stands out. Thank you for the effort.

    • Actually, these calculations are correct. If you look closely you will notice that the labels in my example are different from the example you linked to. This is why the calculations look slightly different. I’ve had a countless number of people (math majors/teachers) verify the calculations in this blog post over the years. I originally wrote the blog post on one of my older blogs 8 years ago not long after finishing my degree in physics.

      • Thanks.

        It would be perfect guide if you explain why the radius projection on xy plane is r * sin(theta). angle theta at P is driven by the parallel lines, one z axis goes through the origin (0, 0, 0) and the other z axis goes through the point (x, y) on xy plane. The origin, point P (x, y, z), and p(x, y) on xy plane forms a right angle triangle at p. It validates two Z axis in parallel.

        I checked the link page and the angle variables are in reverse order of yours. Most math books don’t provide how formula is derived, which is intellectual laziness or a sign of repeater. Many professionals are only good at repeating stuff.

  2. This is an excellent and timely presentation of reference material and example code for its implementation, which is more than I was hoping for. The post was also concise, which I highly value. Thank you for your efforts.

  3. Pingback: Realigning Sports Leagues with a Clustering Algorithm – Data Science Austria

  4. Pingback: Realigning Sports Leagues with a Clustering Algorithm - OSLucidity

  5. Pingback: How to get camera position with mouse move event and transform into screen coordinates in three.js? [closed] – w3toppers.com

  6. Pingback: [Solved] How to get camera position with mouse move event and transform into screen coordinates in three.js? [closed] - Jass Web

  7. Pingback: [Solved] How to get camera position with mouse move event and transform into screen coordinates in three.js? [closed] – Jassweb Solved

  8. I tried to plot a 3D point using your formula, but the z-component didn’t give the desired outcome. I had a series of coordinates that originally went from high to low.
    With your formula:
    z = earthRadius * math.sin(lat)
    what was plotted actually went from low to high.
    However, when I switched to :
    z = earthRadius * math.cos(lat)
    the trajectory was correctly depicted from top to bottom.
    Have you try to plot some coordinate before? Do you have an explanation for this ?
    Alex

    • Take a closer look at the image and you will see that theta is not latitude, but the opposite angle (90 – latitude). So if using latitude, you would want z = earthRadius * math.cos(lat) or z = earthRadius * math.sin(90 – lat)

      This is a really old post that has had hundreds of feedback both inside and outside this blog. It’s had over a million viewers, most of whom are using this for physics, not mapping (like 99% of the viewers). As such, theta being as it is in the diagram is more aligned with the physics users.

Leave a comment