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   18 use 5.008001;
  1         4  
3 1     1   6 use strictures 2;
  1         6  
  1         33  
4             our $VERSION = '0.18';
5              
6 1     1   193 use parent 'GIS::Distance::Formula';
  1         3  
  1         5  
7              
8 1     1   75 use Math::Trig qw( deg2rad );
  1         1  
  1         55  
9 1     1   6 use GIS::Distance::Constants qw( :all );
  1         2  
  1         91  
10 1     1   11 use namespace::clean;
  1         2  
  1         6  
11              
12             sub _distance {
13 5     5   11 my ($lat1, $lon1, $lat2, $lon2) = @_;
14              
15 5         13 $lon1 = deg2rad($lon1);
16 5         49 $lat1 = deg2rad($lat1);
17 5         34 $lon2 = deg2rad($lon2);
18 5         33 $lat2 = deg2rad($lat2);
19              
20 5         31 my $dlon = $lon2 - $lon1;
21 5         7 my $dlat = $lat2 - $lat1;
22 5         21 my $a = (sin($dlat/2)) ** 2 + cos($lat1) * cos($lat2) * (sin($dlon/2)) ** 2;
23 5         16 my $c = 2 * atan2(sqrt($a), sqrt(abs(1-$a)));
24              
25 5         16 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__