File Coverage

blib/lib/GIS/Distance/Lite.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 24 24 100.0


line stmt bran cond sub pod time code
1             package GIS::Distance::Lite;
2              
3 1     1   24474 use strict;
  1         2  
  1         36  
4 1     1   1612 use Math::Trig;
  1         26427  
  1         214  
5 1     1   12 use Exporter 'import';
  1         8  
  1         224  
6              
7             our @EXPORT_OK = qw(distance);
8             our $VERSION = "1.0";
9              
10             =head1 NAME
11              
12             GIS::Distance::Lite - Calculate geographic distances between coordinates in geodetic WGS84 format.
13              
14             =head1 SYNOPSIS
15              
16             use GIS::Distance::Lite qw(distance);
17            
18             my $distanceInMeters = distance($lat1, $lon1 => $lat2, $lon2);
19              
20              
21             =head1 DESCRIPTION
22              
23             The module provides a method to calculate geographic distances between coordinates in geodetic WGS84 format using the Haversine formula.
24              
25             It is similar to L, but without the extra bells and whistles and without the additional dependencies. Same great taste, less filling.
26             It exists for those who cannot, or prefer not to install Moose and its dependencies.
27              
28              
29             =over 2
30              
31             =item my $distanceInMeters = GIS::Distance::Lite::distance($lat1, $lon1 => $lat2, $lon2);
32              
33             Calculate distance between two set of coordinates.
34              
35             Parameters:
36              
37             $latitude1 (number)
38             $longitude1 (number)
39             $latitude2 (number)
40             $longitude2 (number)
41              
42              
43             Example:
44              
45             my $distanceInMeters = GIS::Distance::Lite::distance(59.26978, 18.03948 => 59.27200, 18.05375);
46              
47             Returns:
48              
49             the distance in meters;
50              
51             =cut
52              
53             sub distance {
54 6     6 1 1443 my ($lat1, $lon1, $lat2, $lon2) = @_;
55 6         20 $lon1 = deg2rad($lon1);
56 6         64 $lat1 = deg2rad($lat1);
57 6         41 $lon2 = deg2rad($lon2);
58 6         42 $lat2 = deg2rad($lat2);
59              
60 6         38 my $dlon = $lon2 - $lon1;
61 6         8 my $dlat = $lat2 - $lat1;
62 6         67 my $a = (sin($dlat/2)) ** 2 + cos($lat1) * cos($lat2) * (sin($dlon/2)) ** 2;
63 6         27 my $c = 2 * atan2(sqrt($a), sqrt(1-$a));
64              
65 6         19 return 6371640 * $c
66             }
67              
68             1;
69              
70             =head1 SEE ALSO
71              
72             Inspired by: L
73              
74             Haversine formula: http://en.wikipedia.org/wiki/Haversine_formula
75              
76             =head1 COPYRIGHT
77              
78             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
79              
80             =head1 AUTHORS
81              
82             Author: Johan Beronius , 2010. http://www.athega.se/
83              
84             =cut