File Coverage

blib/lib/GIS/Distance/Haversine.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 34 34 100.0


line stmt bran cond sub pod time code
1             package GIS::Distance::Haversine;
2 1     1   17 use 5.008001;
  1         3  
3 1     1   5 use strictures 2;
  1         6  
  1         30  
4             our $VERSION = '0.20';
5              
6 1     1   162 use parent 'GIS::Distance::Formula';
  1         1  
  1         5  
7              
8 1     1   56 use Math::Trig qw( deg2rad );
  1         2  
  1         36  
9 1     1   15 use GIS::Distance::Constants qw( :all );
  1         2  
  1         85  
10 1     1   5 use namespace::clean;
  1         2  
  1         32  
11              
12             sub _distance {
13 5     5   9 my ($lat1, $lon1, $lat2, $lon2) = @_;
14              
15 5         10 $lon1 = deg2rad($lon1);
16 5         39 $lat1 = deg2rad($lat1);
17 5         29 $lon2 = deg2rad($lon2);
18 5         27 $lat2 = deg2rad($lat2);
19              
20 5         25 my $dlon = $lon2 - $lon1;
21 5         6 my $dlat = $lat2 - $lat1;
22 5         18 my $a = (sin($dlat/2)) ** 2 + cos($lat1) * cos($lat2) * (sin($dlon/2)) ** 2;
23 5         13 my $c = 2 * atan2(sqrt($a), sqrt(abs(1-$a)));
24              
25 5         14 return $KILOMETER_RHO * $c;
26             }
27              
28             # Eric Samuelson recommended this formula.
29             # http://forums.devshed.com/t54655/sc3d021a264676b9b440ea7cbe1f775a1.html
30             # http://williams.best.vwh.net/avform.htm
31             # It seems to produce the same results at the hsin formula, so...
32             #
33             # my $dlon = $lon2 - $lon1;
34             # my $dlat = $lat2 - $lat1;
35             # my $a = (sin($dlat / 2)) ** 2
36             # + cos($lat1) * cos($lat2) * (sin($dlon / 2)) ** 2;
37             # $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
38             #
39             # Maybe test for speed before tossing?
40              
41             1;
42             __END__