Calculate Distance between two Coordinates

There are several different ways to calculate the distance between two coordinates, P1(r, θ, Φ) and P2(r, θ, Φ). The first method consists of converting the points to Cartesian coordinates, and then calculating the cord length (d) between the points (straight line distance through the Earth). From there we can calculate the central angle between the two points (δ). Once we have the central angle between the two points we can make a simple calculation to figure out the arc length (L) between the two points.

clip_image002

Figure 1 Arc Length calculation

To calculate the cord length between P1 and P2 we can use Pythagorean Theorem as follows:

clip_image004

Now that we have the cord length we can calculate the central angle between the two points. Using simple trigonometry we can calculate clip_image006 from r and clip_image008 as follows:

clip_image010

clip_image012

We want clip_image014 to be in radians. If we calculate clip_image014[1] using javascript the angle will be in radians. If we calculate clip_image014[2] using a calculator we will have to convert the angle from degrees to radians. We can then calculate the arc length using the following formula:

clip_image016

The second method for calculating the distance between two points is the Haversine method. The basic principal is similar to the last method of calculating the distance between two points except that it does not solve for x, y, and z, but uses the formulas of the components. The cord length between the two points can be calculated by using Pythagorean Theorem against the displacement in the x, y, and z axis.

clip_image018

clip_image020

By using the following trigonometric identities we can derive a simpler formula for the cord length between two points.

Half-angle formula: clip_image022

Double-Angle formula: clip_image024

Subtraction formula: clip_image026

clip_image028

We can then use the atan2 function to derive the central angle between the two points:

clip_image030

We can then calculate the distance between the two points using the following formula:

clip_image016[1]

Both of these methods produce fairly accurate results. If more precision is required the Vincenty formula can be used which calculates the distance between two points on an ellipsoid. The basic principals are similar to the other two formulas except that the radius varies between the two points.

Application

For the function that calculates the arc length between two points it will take in to VELatLong objects and return a number. This function has to convert the VELatLongs into Cartesian coordinates. When we use the convertSphericalToCartesian function the returned value is a Cartesian coordinate object with X, Y, and Z values.

var earthRadius = 6367; //radius in km

function arcLength(latlong1, latlong2){   
 var point1 = convertSphericalToCartesian(latlong1);
 var point2 = convertSphericalToCartesian(latlong2);
 var cordLength = Math.sqrt(Math.pow(point1.X-point2.X,2)+Math.pow(point1.Y-point2.Y,2)+Math.pow(point1.Z-point2.Z,2));
 var centralAngle = 2*Math.asin(cordLength/2/earthRadius);
 return earthRadius*centralAngle;
}

Listing 1 Arc Length function

The Haversine function will take in to VELatLong objects and return a number. In this function we will need to convert the latitude and longitude values from degrees to radians using the DegtoRad function.

function haversineDistance(latlong1,latlong2){
 var lat1 = DegtoRad(latlong1.Latitude);
 var lon1 = DegtoRad(latlong1.Longitude);
 var lat2 = DegtoRad(latlong2.Latitude);
 var lon2 = DegtoRad(latlong2.Longitude);
 var dLat = lat2-lat1;
 var dLon = lon2-lon1;
 var cordLength = Math.pow(Math.sin(dLat/2),2)+Math.cos(lat1)*Math.cos(lat2)*Math.pow(Math.sin(dLon/2),2);
 var centralAngle = 2 * Math.atan2(Math.sqrt(cordLength), Math.sqrt(1-cordLength));
 return earthRadius * centralAngle;
}

Listing 2 Haversine function

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

The following post has additional information on the convertSphericalToCartesian method: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!280.entry

 

 

 

 

 

 

Advertisements

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