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
17 use POSIX qw(:math_h);
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 # calc bearing and distance, with arguments in DEGREES
82 # home lat/long -> lat/long
83 # returns bearing (in DEGREES) & distance in KM
90 return (0, 0) if $hn == $n && $he == $e;
91 my $co = cos($he-$e)*cos($hn)*cos($n)+sin($hn)*sin($n);
92 my $ca = $co ? atan(abs(sqrt(1-$co*$co)/$co)) : $pi;
93 $ca = $pi-$ca if $co < 0;
95 my $si = sin($e-$he)*cos($n)*cos($hn);
96 $co = sin($n)-sin($hn)*cos($ca);
97 my $az = $co ? atan(abs($si/$co)) : $pi;
98 $az = $pi - $az if $co < 0;
99 $az = -$az if $si < 0;
100 $az = $az+2*$pi if $az < 0;
101 return (rd($az), $dx);
104 # just the distance - parameters as above
111 return (0, 0) if $hn == $n && $he == $e;
112 my $co = cos($he-$e)*cos($hn)*cos($n)+sin($hn)*sin($n);
113 my $ca = $co ? atan(abs(sqrt(1-$co*$co)/$co)) : $pi;
114 $ca = $pi-$ca if $co < 0;
118 # turn a lat long string into floating point lat and long
121 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])/;
123 $longd += ($longm/60);
124 $longd = 0-$longd if (uc $longl) eq 'W';
126 $latd = 0-$latd if (uc $latl) eq 'S';
127 return ($latd, $longd);
130 # turn a lat and long into a string
133 my ($lat, $long) = @_;
134 my $slat = slat($lat);
135 my $slong = slong($long);
136 return "$slat $slong";