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

About these ads

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s