GIS::Distance::Formula::Haversine - Exact spherical distance calculations.


GIS-Distance documentation Contained in the GIS-Distance distribution.

Index


Code Index:

NAME

Top

GIS::Distance::Formula::Haversine - Exact spherical distance calculations.

DESCRIPTION

Top

This is the default distance calculation for GIS::Distance as it keeps a good balance between speed and accuracy.

Normally this module is not used directly. Instead GIS::Distance is used which in turn interfaces with the various formula classes.

FORMULA

Top

  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

METHODS

Top

distance

This method is called by GIS::Distance's distance() method.

SEE ALSO

Top

GIS::Distanc

GIS::Distance::Formula::Haversine::Fast

RESOURCES

Top

http://mathforum.org/library/drmath/view/51879.html

http://www.faqs.org/faqs/geography/infosystems-faq/

AUTHOR

Top

Aran Clary Deltac <bluefeet@cpan.org>

LICENSE

Top

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


GIS-Distance documentation Contained in the GIS-Distance distribution.
package GIS::Distance::Formula::Haversine;

use Any::Moose;
use namespace::autoclean;

with 'GIS::Distance::Formula';

use Math::Trig qw( deg2rad );
use Class::Measure::Length qw( length );

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' );
}

__PACKAGE__->meta->make_immutable;

1;
__END__