I have a sample SAS program to calculate the distance and bearing
between to points. There are two completely different distance formulas
(Law of Cosines and Haversine); also, I included several definitions for
the Earth Radius constant.
I derived this after reading a whole lot of web sites, little of which I
truly understand. My "production" code distills all of this into a
single formula with lots of imbedded function calls. It works well for
me, and I hope it does for you.
I'd be happy to share this on the Wiki site, but I haven't figured out
how to do that. If someone wants to post it there, I'm fine with that;
no credit necessary.
There's the code:
/* These examples demonstrate how to calculate the distance */
/* in miles between two geographic coordinate pairs. */
/* Note, the examples expect the data starts as "degrees". */
/* and converts the coordinates to radians. If your data is */
/* already in radians, modify accordingly. */
data SAS_TABLE;
if _n_ = 1 then do;
/* Define Pi as a constant */
Pi = constant('pi'); /* Use the SAS function to improve precision
*/
/* Define Earth radius as a constant in miles */
* 1 kilometer = 0.6213712 mile;
* P=Polar Radius = 6356.9 km (3949.99453 miles);
* E=Equatorial radius = 6378.5137 km (3963.42477 miles);
* M=Mean volumetric radius = 6371.01 km (3958.76208 miles);
* Q=Quadradic mean radius = 6372.795478 km (3959.87152 miles);
* Q=SQRT((3E**2 + P**2)/4);
* GIS FAQ = 6367 km (3956.2704 miles);
REarth = 3956.2704;
end;
retain Pi REarth;
/* First location. */
deg_lon1 = -81.2238;
deg_lat1 = 35.25927;
/* Second location. */
deg_lon2 = -81.1492;
deg_lat2 = 35.22143;
/* First convert degrees to radians, if necessary */
rad_lon1 = deg_lon1 * pi / 180;
rad_lat1 = deg_lat1 * pi / 180;
rad_lon2 = deg_lon2 * pi / 180;
rad_lat2 = deg_lat2 * pi / 180;
/* Law of Cosines for Spherical Trigonometry (uses radius) */
dist_miles1 = REarth * ARCOS( ( COS(rad_lat1) * COS(rad_lat2) *
COS(rad_lon1-rad_lon2) )
+ ( SIN(rad_lat1) * SIN(rad_lat2) )
);
/* Haversine Formula (uses diameter) */
A = sin( (rad_lat2 - rad_lat1)/2.0 )**2
+ ( cos(rad_lat1)
* cos(rad_lat2)
* sin((rad_lon2 - rad_lon1)/2.0)**2
);
dist_miles2 = (REarth * 2) * atan2(sqrt(A),sqrt(1-A));
/* Calculate bearing */
rad_Bearing = mod(atan2(sin(rad_lon1-rad_lon2)*cos(rad_lat2),
cos(rad_lat1)*sin(rad_lat2)-sin(rad_lat1)*cos(rad_lat2)*cos(rad_lon1-rad
_lon2)), 2*Pi);
/* Convert bearing to degrees and create display variable as
degrees-minutes-seconds */
deg_Bearing = rad_Bearing * 180 / Pi;
Degrees = Int(abs(deg_Bearing));
Minutes = (abs(deg_Bearing) - Degrees) * 60;
Seconds = round((Minutes - Int(Minutes)) * 60);
Bearing_txt = put(degrees,3.) || 'B0'x || put(minutes,2.) || "'" ||
put(seconds,2.) || '"';
run;
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-***@LISTSERV.UGA.EDU] On Behalf Of
***@GMAIL.COM
Sent: Thursday, October 18, 2007 7:08 AM
To: SAS-***@LISTSERV.UGA.EDU
Subject: Lat/Lon Origination Coordinates, Bearing,
Distance....Destination Coordinates?
Does anyone have a formula for calculating the destination lat/lon
based on an origination lat/lon, bearing and distance?
Much Thanks,
Aaron