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
21 $pi = 3.14159265358979;
23 # half a qra to lat long translation
27 my $lat = ord($l) - ord('A');
28 $lat = $lat * 10 + (ord($n) - ord('0'));
29 $lat = $lat * 24 + (ord($m) - ord('A'));
31 $lat = $lat * ($pi/4320);
34 # convert a qra locator into lat/long in DEGREES
38 my $long = _half_qratoll((unpack 'AAAAAA', $qra)[0,2,4]) * 2;
39 my $lat = _half_qratoll((unpack 'AAAAAA', $qra)[1,3,5]);
40 return (rd($lat), rd($long));
45 my ($t, $f, $n, $e) = @_;
46 $n = $f * ($n - int($n));
47 $e = $f * ($e - int($e));
48 my $q = chr($t+$e) . chr($t+$n);
52 # convert a lat, long in DEGREES to a qra locator
59 $long = $long * $t +.5 ;
60 $lat = $lat * $t * 2 + .5 ;
64 ($q, $lat, $long) = _part_lltoqra(ord('A'), 18, $lat, $long);
66 ($q, $lat, $long) = _part_lltoqra(ord('0'), 10, $lat, $long);
68 ($q, $lat, $long) = _part_lltoqra(ord('A'), 24, $lat, $long);
77 return ($n / $pi) * 180;
84 return ($n / 180) * $pi;
87 # does it look like a qra locator?
91 return $qra =~ /^[A-Za-z][A-Za-z]\d\d[A-Za-z][A-Za-z]$/o;
94 # calc bearing and distance, with arguments in DEGREES
95 # home lat/long -> lat/long
96 # returns bearing (in DEGREES) & distance in KM
103 my $co = cos($he-$e)*cos($hn)*cos($n)+sin($hn)*sin($n);
104 my $ca = atan(abs(sqrt(1-$co*$co)/$co));
105 $ca = $pi-$ca if $co < 0;
107 my $si = sin($e-$he)*cos($n)*cos($hn);
108 $co = sin($n)-sin($hn)*cos($ca);
109 my $az = atan(abs($si/$co));
110 $az = $pi - $az if $co < 0;
111 $az = -$az if $si < 0;
112 $az = $az+2*$pi if $az < 0;
113 return (rd($az), $dx);
116 # turn a lat long string into floating point lat and long
119 my ($latd, $latm, $latl, $longd, $longm, $longl) = split /\s+/, shift;
121 $longd += ($longm/60);
122 $longd = 0-$longd if (uc $longl) eq 'W';
124 $latd = 0-$latd if (uc $latl) eq 'S';
125 return ($latd, $longd);
128 # turn a lat and long into a string
131 my ($lat, $long) = @_;
132 my ($latd, $latm, $longd, $longm);
133 my $latl = $lat > 0 ? 'N' : 'S';
134 my $longl = $long > 0 ? 'E' : 'W';
139 $latm = int (60 * $lat);
144 $longm = int (60 * $long);
145 return "$latd $latm $latl $longd $longm $longl";