ZIP*Data II:Distance Calculation

From Melissa Data Wiki
Jump to navigation Jump to search

← ZIP*Data II

ZIP*Data II Navigation
Introduction
Database Layouts
Distance Calculation
Abbreviations and Codes



Any point on the earth’s surface can be located by its latitude and longitude coordinates. Latitude is the angle above or below the equator in degrees. The equator is zero degrees, the north pole is north 90 degrees latitude, and the south pole is south 90 degrees latitude. The continental United States falls between 25 and 50 degrees north.

Longitude is the angle east or west of the Greenwich meridian. The continental United States is between 70 and 125 degrees west.

Approximate Solutions

One degree of latitude is equal to 69.1 miles. One degree of longitude is equal to 69.1 miles at the equator. North or south of the equator, 1 degree of longitude is a smaller distance. It’s reduced by the cosine of the latitude. Dividing the latitude number by 57.3 converts it to radians.

DistLat = 69.1 * (Lat2-Lat1)
DistLong = 69.1 * (Lg2-Lg1) * cos(Lat1 / 57.3)
Dist = (DistLat2 + DistLong2) 0.5

If you don’t want to use the COS function, then a good approximate solution is:

DistLat = 69.1 * (Lat2-Lat1)
DistLg = 53 * (Lg2 - Lg1)
Dist = (DistLat2 + DistLong2) 0.5

Exact Solution

To calculate the exact distance between points requires spherical geometry.

Basic Forumla

D = 3959arccos[sin(Lat1)sin(Lat2)+cos(Lat1)cos(Lat2)cos(Lg2-Lg1)]

The above formula has one major problem: most computer languages do not support the arc cosine function. Therefore a different form is necessary, one that uses the arc tangent function that most languages embrace.

Modified Formula

D = 3959atn [ (1-A2 ) 0.5 / A]

where A is equal to:

A = sin(Lat1)sin(Lat2)+cos(Lat1)cos(Lat2)cos(Lg2-Lg1)

Most computer languages compute the sine and cosine function with the angle given in radians. To convert degrees to radians, divide degrees by the constant 57.3 (180/pi).

BASIC Example

In BASIC this would be programmed as follows:

C = 57.3
A = sin(Lat1/C)*sin(Lat2/C) + cos(Lat1/C)* cos(Lat2/C)*cos(Lg2/C - Lg1/C)
D = 3959 * atn( SQR(1-A^2)/A)

Where:

D = distance in statue miles from the first to the second point.
C = degrees to radians constant 57.3.
Lat1 = latitude of the first point in degrees.
Lg1 = longitude of the first point in degrees.
Lat2 = latitude of the second point in degrees.
Lg2 = longitude of the second point in degrees.
NOTE
Dividing the latitude number by 57.3 converts it to radians.

Since all ZIP code points in the United States are north latitude and west longitude, there is no need to check the sines (positive and negative) of the latitudes and longitudes.

Some programming languages, such as dBASE II/III, do not have the functions of the sine, cosine and arc tangent. Also, the formulas given are time-consuming to calculate. A simpler but less accurate method is given here:

Less-Accurate Formula

D = 69.1 * SQR [(LAT2-LAT1)2 +0.6* (LG2-LG1)2]

Although this formula is not as accurate as the first great circle method, it will give good results for most applications. In Basic, it is written as follows:

BASIC Example

D = 69.1 * SQR [(LAT2-LAT1)^2 + .6*(LONG2-LONG1)^2]

In dBASE III it is written as follows:

dBASE Example

D = 69.1 * SQRT [(LAT2-LAT1)^2 + 0.6 *(LONG2-LONG1)^2]

To make using the database easier, the latitudes and longitudes are given in the decimal format instead of the degree, minute and second format. The latter would require the additional steps of converting seconds to fractions of a minute and then minutes to a fraction of a degree.


Bearing Formula

The bearing is the direction from the first point to the second point. It is expressed as an angle from north in degrees. Due north is a bearing of zero degrees, east is a bearing of 90 degrees, south is 180, and west is 270.

The bearing from the first point to the second is calculated with:

sin(Lat1) * sin(Lg2-Lg1)
B = Arc Tan ——————————————
sin(Lat2) * cos(Lat1) - cos(Lat2) *
sin(Lat1) * cos(Lg2-Lg1)

Since the ARCTAN function gives the angle in radians, it is necessary to convert B to degrees by multiplying by 57.3 (180/Pi).

To use this equation requires some special considerations. This example assumes that Lat1, Lat2, Lng1, Lng2 have been converted to radians.

Use the following steps:

N = sin(Lat1)*sin(Lng1-Lng2)
D = sin(Lat2)*cos(Lat1) – cos(Lat2)*sin(Lat1)*cos(Lng1-Lng2)
B = 57.3 * ARCTAN(N/D)

If (D > 0) then (B = 360+B)
If (D < 0) then (B = 180+B)
If (B < 0) then (B = 360 + B)

Where:

N is the Numerator.
D is the Denominator.
B is the Bearing.


Examples

The following examples should give you a feel for the numbers. For these examples, we will use the points of Schenectady, NY and Los Angeles, CA.

Point #1

Schenectady, NY, 12345
Lat1 = 42.8145
Lng1 = 73.9380

Point #2

Los Angeles, CA, 90001
Lat2 = 34.0515
Lng2 = 118.2420

Example 1: Approximate Formula (1)

Lat Dist = 69.1 (Lat2 - Lat1) = 605.5
Lng Dist = 69.1 * ( Lng2 - Lng1 ) *
COS ( 42.8145/57.3 ) = 2245.8
Dist = 2326.0 miles

Example 2: Approximate Formula (2)

Lat Dist = 69.1 (Lat2 - Lat1) = 605.5
Lng Dist = 53 * ( Lng2 - Lng1 ) = 2348.1
Dist = 2424.9 miles

Example 3: Exact Solution Formula (3)

A1 = sin(Lat1/57.3) * sin( Lat2/57.3) = 0.3805
A2 = cos(Lat1/57.3) * cos(Lat2/57.3) * cos(Lng2/57.3-Lng1/57.3)
A2 = 0.4350
A = A1 + A2 = 0.8155
Dist = 3959 * ATN[(1 - A2)0.5/A]
Dist = 2443.4 miles

Example 4: Bearing Formula (4)

N = sin(Lat1/57.3) * sin( Lng2/57.3 - Lng1/57.3) = 0.4747
D1 = sin(Lat2/57.3) * cos(Lat1/57.3) = 0.4107
D2 = cos(Lat2/57.3) * sin(Lat1/57.3) * cos(Lng2/57.3 - Lng1/57.3)
D2 = 0.4029
B = 57.3 * ATN(N/(D1 - D2)) = -89.1 Degrees
Bearing = 360 + B = 360 - 89.1
Bearing = 270.9 degrees from North