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.

**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

Thanks, this was very helpful.

i am really satisfied…thank you.

Thank you for posting this.

It’s a very nice explaination!!!

thanks for alot help…

Thanks. It is very easy to understand. Thank u very much.

thks this is interesting

thanks so much for help. it is now easy to understand.

v.nice explanation!!!!!

nice and thnx

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.

awesome dude