| 1 | package GIS::Distance::Haversine; | ||
| 2 | 1 | ▶ | use 5.008001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | our $VERSION = '0.19'; | |
| 5 | |||
| 6 | 1 | ▶ | use parent 'GIS::Distance::Formula'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Math::Trig qw( deg2rad ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use GIS::Distance::Constants qw( :all ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub _distance { | ||
| 13 | 5 | ▶ | my ($lat1, $lon1, $lat2, $lon2) = @_; |
|
Subroutine
_distance: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 5 | $lon1 = deg2rad($lon1); | |
| 16 | 5 | $lat1 = deg2rad($lat1); | |
| 17 | 5 | $lon2 = deg2rad($lon2); | |
| 18 | 5 | $lat2 = deg2rad($lat2); | |
| 19 | |||
| 20 | 5 | my $dlon = $lon2 - $lon1; | |
| 21 | 5 | my $dlat = $lat2 - $lat1; | |
| 22 | 5 | my $a = (sin($dlat/2)) ** 2 + cos($lat1) * cos($lat2) * (sin($dlon/2)) ** 2; | |
| 23 | 5 | my $c = 2 * atan2(sqrt($a), sqrt(abs(1-$a))); | |
| 24 | |||
| 25 | 5 | 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 | 1; | |
| 42 | __END__ | ||