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 # convert a qra locator into lat/long in DEGREES
28 my ($p1, $p2, $p3, $p4, $p5, $p6) = unpack 'AAAAAA', $qra;
29 ($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') );
31 my $long = ($p1*20) + ($p3*2) + (($p5+0.5)/12) - 180;
32 my $lat = ($p2*10) + $p4 + (($p6+0.5)/24) - 90;
36 # convert a lat, long in DEGREES to a qra locator
43 my ($p1, $p2, $p3, $p4, $p5, $p6);
49 $p1 = chr(ord('A') + $v);
52 $p2 = chr(ord('A') + $v);
57 $p3 = chr(ord('0')+$p3);
58 $p4 = chr(ord('0')+$p4);
59 $p5 = int((12 * $long) );
60 $p6 = int((24 * $lat) );
61 $p5 = chr(ord('A')+$p5);
62 $p6 = chr(ord('A')+$p6);
64 return "$p1$p2$p3$p4$p5$p6";
71 return ($n / $pi) * 180;
78 return ($n / 180) * $pi;
81 # does it look like a qra locator?
85 return $qra =~ /^[A-Za-z][A-Za-z]\d\d[A-Za-z][A-Za-z]$/o;
88 # calc bearing and distance, with arguments in DEGREES
89 # home lat/long -> lat/long
90 # returns bearing (in DEGREES) & distance in KM
97 return (0, 0) if $hn == $n && $he == $e;
98 my $co = cos($he-$e)*cos($hn)*cos($n)+sin($hn)*sin($n);
99 my $ca = atan(abs(sqrt(1-$co*$co)/$co));
100 $ca = $pi-$ca if $co < 0;
102 my $si = sin($e-$he)*cos($n)*cos($hn);
103 $co = sin($n)-sin($hn)*cos($ca);
104 my $az = atan(abs($si/$co));
105 $az = $pi - $az if $co < 0;
106 $az = -$az if $si < 0;
107 $az = $az+2*$pi if $az < 0;
108 return (rd($az), $dx);
111 # turn a lat long string into floating point lat and long
114 my ($latd, $latm, $latl, $longd, $longm, $longl) = split /\s+/, shift;
116 $longd += ($longm/60);
117 $longd = 0-$longd if (uc $longl) eq 'W';
119 $latd = 0-$latd if (uc $latl) eq 'S';
120 return ($latd, $longd);
123 # turn a lat and long into a string
126 my ($lat, $long) = @_;
127 my $slat = slat($lat);
128 my $slong = slong($long);
129 return "$slat $slong";