package GIS::Distance::Formula::Haversine; =head1 NAME GIS::Distance::Formula::Haversine - Exact spherical distance calculations. =head1 DESCRIPTION This is the default distance calculation for L as it keeps a good balance between speed and accuracy. Normally this module is not used directly. Instead L is used which in turn interfaces with the various formula classes. =head1 FORMULA dlon = lon2 - lon1 dlat = lat2 - lat1 a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 c = 2 * atan2( sqrt(a), sqrt(1-a) ) d = R * c =cut use Moose; extends 'GIS::Distance::Formula'; use Math::Trig qw( deg2rad ); use Class::Measure::Length qw( length ); =head1 METHODS =head2 distance This method is called by L's distance() method. =cut sub distance { my ($self, $lat1, $lon1, $lat2, $lon2) = @_; $lon1 = deg2rad($lon1); $lat1 = deg2rad($lat1); $lon2 = deg2rad($lon2); $lat2 = deg2rad($lat2); my $dlon = $lon2 - $lon1; my $dlat = $lat2 - $lat1; my $a = (sin($dlat/2)) ** 2 + cos($lat1) * cos($lat2) * (sin($dlon/2)) ** 2; my $c = 2 * atan2(sqrt($a), sqrt(1-$a)); return length( $self->kilometer_rho() * $c, 'km' ); } 1; __END__ =head1 SEE ALSO L L =head1 RESOURCES L L =head1 AUTHOR Aran Clary Deltac =head1 LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.