2 # bearing and distance calculations together with
3 # locator convertions to lat/long and back
5 # some of this is nicked from 'Amateur Radio Software' by
6 # John Morris GM4ANB and tranlated into perl from the original
7 # basic by me - I have factorised it where I can be bothered
9 # Copyright (c) 1998 - Dirk Koopman G1TLH
22 $pi = 3.14159265358979;
24 use vars qw($VERSION $BRANCH);
25 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
26 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
27 $main::build += $VERSION;
28 $main::branch += $BRANCH;
30 # convert a qra locator into lat/long in DEGREES
34 my ($p1, $p2, $p3, $p4, $p5, $p6) = unpack 'AAAAAA', $qra;
35 ($p1, $p2, $p3, $p4, $p5, $p6) = (ord($p1)-ord('A'), ord($p2)-ord('A'), ord($p3)-ord('0'), ord($p4)-ord('0'), ord($p5)-ord('A'), ord($p6)-ord('A') );
37 my $long = ($p1*20) + ($p3*2) + (($p5+0.5)/12) - 180;
38 my $lat = ($p2*10) + $p4 + (($p6+0.5)/24) - 90;
42 # convert a lat, long in DEGREES to a qra locator
49 my ($p1, $p2, $p3, $p4, $p5, $p6);
55 $p1 = chr(ord('A') + $v);
58 $p2 = chr(ord('A') + $v);
63 $p3 = chr(ord('0')+$p3);
64 $p4 = chr(ord('0')+$p4);
65 $p5 = int((12 * $long) );
66 $p6 = int((24 * $lat) );
67 $p5 = chr(ord('A')+$p5);
68 $p6 = chr(ord('A')+$p6);
70 return "$p1$p2$p3$p4$p5$p6";
77 return ($n / $pi) * 180;
84 return ($n / 180) * $pi;
87 # calc bearing and distance, with arguments in DEGREES
88 # home lat/long -> lat/long
89 # returns bearing (in DEGREES) & distance in KM
96 return (0, 0) if $hn == $n && $he == $e;
97 my $co = cos($he-$e)*cos($hn)*cos($n)+sin($hn)*sin($n);
98 my $ca = atan(abs(sqrt(1-$co*$co)/$co));
99 $ca = $pi-$ca if $co < 0;
101 my $si = sin($e-$he)*cos($n)*cos($hn);
102 $co = sin($n)-sin($hn)*cos($ca);
103 my $az = atan(abs($si/$co));
104 $az = $pi - $az if $co < 0;
105 $az = -$az if $si < 0;
106 $az = $az+2*$pi if $az < 0;
107 return (rd($az), $dx);
110 # turn a lat long string into floating point lat and long
113 my ($latd, $latm, $latl, $longd, $longm, $longl) = $_[0] =~ /(\d{1,2})\s+(\d{1,2})\s*([NnSs])\s+(1?\d{1,2})\s+(\d{1,2})\s*([EeWw])/;
115 $longd += ($longm/60);
116 $longd = 0-$longd if (uc $longl) eq 'W';
118 $latd = 0-$latd if (uc $latl) eq 'S';
119 return ($latd, $longd);
122 # turn a lat and long into a string
125 my ($lat, $long) = @_;
126 my $slat = slat($lat);
127 my $slong = slong($long);
128 return "$slat $slong";