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.


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:


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:


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:


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:


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.


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



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





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
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:!E7DBA9A4BFD458C5!257.entry


16 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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s