Does someone have a formula to convert the lat and lon from two

locations (A and B) to bearings (location B relative to location A)?

Does someone have a formula to convert the lat and lon from two

locations (A and B) to bearings (location B relative to location A)?

locations (A and B) to bearings (location B relative to location A)?

Hi,

Here are some posts to get you started:

http://listserv.uga.edu/cgi-bin/wa?A2=ind0605D&L=sas-l&P=R6483

http://listserv.uga.edu/cgi-bin/wa?A2=ind0511C&L=sas-l&P=R32434

Mark Terjeson

Senior Programmer Analyst, IM&R

Russell Investments

253-439-2367

Russell

Global Leaders in Multi-Manager Investing

-----Original Message-----

From: SAS(r) Discussion [mailto: XXXX@XXXXX.COM ] On Behalf Of

XXXX@XXXXX.COM

Sent: Tuesday, June 12, 2007 1:53 PM

To: XXXX@XXXXX.COM

Subject: Converting Latitude/Longitude (decimal degrees) to Bearing?

Does someone have a formula to convert the lat and lon from two

locations (A and B) to bearings (location B relative to location A)?

Hi

Here's a little SAS example to calculate both the bearing and the

distance between two points. It's a bit long-winded for illustration

purposes. I got a lot of help from this web site:

http://www.movable-type.co.uk/scripts/latlong.html

There are two different distance calculation techniques used and either

works for me. I don't claim to understand how any of this works; I just

have this in my SAS toolbox. There are many online discussions out

there if you're interested. It seems to depend on the degree of

accuracy you need. And if you need distance in kilometers, change the

earth radius constant to kilometers (see the comment).

Hope this helps; the code follows.

Bob

data SAMPLE_TABLE1(keep=deg_lat1 deg_lon1 deg_lat2 deg_lon2 distance1

distance2

deg_bearing bearing_txt);

if _n_ = 1 then do;

/* Define Pi and the Radius of the Earth as constants */

Pi = constant('pi'); /* Use the SAS function to improve precision

*/

* REarth = 6367; /* Kilometers */

REarth = 3956.2704; /* Miles */

end;

retain Pi REarth;

/* First point */

deg_lon1 = -81.2238;

deg_lat1 = 35.25927;

/* Second point */

deg_lon2 = -81.1492;

deg_lat2 = 35.22143;

/* First convert degrees to radians */

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

distance1 = 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 );

distance2 = (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: XXXX@XXXXX.COM ] On Behalf Of

XXXX@XXXXX.COM

Sent: Tuesday, June 12, 2007 1:53 PM

To: XXXX@XXXXX.COM

Subject: Converting Latitude/Longitude (decimal degrees) to Bearing?

Does someone have a formula to convert the lat and lon from two

locations (A and B) to bearings (location B relative to location A)?

Here's a little SAS example to calculate both the bearing and the

distance between two points. It's a bit long-winded for illustration

purposes. I got a lot of help from this web site:

http://www.movable-type.co.uk/scripts/latlong.html

There are two different distance calculation techniques used and either

works for me. I don't claim to understand how any of this works; I just

have this in my SAS toolbox. There are many online discussions out

there if you're interested. It seems to depend on the degree of

accuracy you need. And if you need distance in kilometers, change the

earth radius constant to kilometers (see the comment).

Hope this helps; the code follows.

Bob

data SAMPLE_TABLE1(keep=deg_lat1 deg_lon1 deg_lat2 deg_lon2 distance1

distance2

deg_bearing bearing_txt);

if _n_ = 1 then do;

/* Define Pi and the Radius of the Earth as constants */

Pi = constant('pi'); /* Use the SAS function to improve precision

*/

* REarth = 6367; /* Kilometers */

REarth = 3956.2704; /* Miles */

end;

retain Pi REarth;

/* First point */

deg_lon1 = -81.2238;

deg_lat1 = 35.25927;

/* Second point */

deg_lon2 = -81.1492;

deg_lat2 = 35.22143;

/* First convert degrees to radians */

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

distance1 = 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 );

distance2 = (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: XXXX@XXXXX.COM ] On Behalf Of

XXXX@XXXXX.COM

Sent: Tuesday, June 12, 2007 1:53 PM

To: XXXX@XXXXX.COM

Subject: Converting Latitude/Longitude (decimal degrees) to Bearing?

Does someone have a formula to convert the lat and lon from two

locations (A and B) to bearings (location B relative to location A)?

XXXX@XXXXX.COM replied:

Good code is always appreciated. I don't think it is too long for SAS-L.

It's basic spherical trigonometry. Behavior of polygons on spherical

surfaces. Of course, it's approximate, since the earth is not really

spherical, but it's a pretty darn good approximation in most cases.

HTH,

David

_________________________________________________________________

Get a preview of Live Earth, the hottest event this summer - only on MSN

http://liveearth.msn.com ?source=msntaglineliveearthhm

Good code is always appreciated. I don't think it is too long for SAS-L.

It's basic spherical trigonometry. Behavior of polygons on spherical

surfaces. Of course, it's approximate, since the earth is not really

spherical, but it's a pretty darn good approximation in most cases.

HTH,

David

_________________________________________________________________

Get a preview of Live Earth, the hottest event this summer - only on MSN

http://liveearth.msn.com ?source=msntaglineliveearthhm

n Jun 13, 8:51 pm, XXXX@XXXXX.COM (David L Cassell) wrote:

Mark, Bob, and David-

Thank you all for you input and advice. i was able to utilize all your

input to make my calculation. I appreciate you all taking the time to

respond.

Aaron

Mark, Bob, and David-

Thank you all for you input and advice. i was able to utilize all your

input to make my calculation. I appreciate you all taking the time to

respond.

Aaron

Similar Threads

1. Calculate new Latitude / Longitude from Bearing and Distance

2. MySQL query for WGS-84 longitude & latitude radius GPS distance calculation

3. Keep trailing 0 decimal when convert from numberic to character

4. Keep trailing 0 decimal when convert from numberic to

On 4/29/08, Guibo Xing < XXXX@XXXXX.COM > wrote: > Hi all, > > when i convert a numeric value to character. i did not get what I want. > For example, when OR is a numberic with length 8, e.g. 1.10, i use > ORC=put(left(or), 4.), then i get ORC=1.1 without the 0 at the end. Can > anybody help ? Thanks in advance. ORC=put(or, 4.2 -L )? Might be worth your while to take 1 minute to read the entry for the PUT function in the online docs. Not only will you get useful information, but links to similar funcitons.

5. Convert decimal values to hexadecimals

6. converting decimal characters to numeric values

Hello, I have a text file with the format as (values seperated by vertical bars | ): line 1 | 0.42 | 1.28 | 4.62 | 2.11 | data perc; infile outtxt pad lrecl=100; array percent[4] percent1-percent4; input lines $char100.; do i=2 to 5; percent[i-1]=input(scan(lines, i, '|'), 5.2); end; run; My question is why the values for percent1-percent4 become: 0 0.01 0.04 0.02 ? Thank you for your help! Yvonne

8. Converting from string to numeric (keeping decimal) in SQL

Hi experts, I run the program in SAS environment against DataMart in Data Warehouse. For some reason the fragment of code below failed. I have no idea why it generated the error message at the bottom. What is invalid character in the character string argument of the function? Actually res_val field is a character type field. I am trying to convert it to numeric and to set 999 to all invalid values. Actually the value can be with decimal point either like 120.5. Probably INTEGER is not good anyway.... Could you please give me a hand? This is what my log gave me ********************************************************************************* 1391 EXECUTE( 1392 declare global temporary table qsrv 1393 ( Id char (29) 1394 , First_Srv_Dt date 1395 , rslt_cd char (13) 1396 , res_val char (18) 1397 , Num_Res_Val smallint 1398 ) 1399 in "&sestblspc" 1400 on commit preserve rows not logged 1401 ) by HDMconnect; 1402 1403 EXECUTE( 1404 insert into session.qsrv 1405 select qu.Id 1406 , qu.dos 1407 , qu.rslt_cd 1408 , qu.res_val, 1409 1410 CASE 1411 WHEN qu.res_val='Invalid' OR 1412 1417 qu.lr_res_val= ' ' 1418 THEN 999 1419 ELSE INTEGER(qu.res_val) 1420 END AS Num_Res_Val 1421 from &clmschema..v_l_data qu 1422 join &tmpschema..num np 1423 on qu.Id = np.Id 1424 1425 ) by mYconnect; ERROR: CLI execute error: [IBM][CLI Driver][DB2/AIX64] SQL0420N Invalid character found in a character string argument of the function "INTEGER". SQLSTATE=22018 ******************************************************************************************** Actually I know that in SAS syntax it would be smth like that: data base_ (drop =_:); set base (rename=(res_val=_res_val)); res_val=input(_res_val, 5.); run; ******************************************************************** But I have beaten my head trying to incorporate it in SQL syntax. Thank You in advance, Irin --------------------------------- Yahoo! Music Unlimited - Access over 1 million songs. Try it free.