| 1 | package GIS::Distance::Lite; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Math::Trig; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Exporter 'import'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | our @EXPORT_OK = qw(distance); | |
| 8 | 1 | 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<GIS::Distance>, 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 | 6 | ▶ | sub distance { |
|
Subroutine
distance: called CC=1 SCAR=0.0
Pod
documented
| |||
| 54 | 6 | my ($lat1, $lon1, $lat2, $lon2) = @_; | |
| 55 | 6 | $lon1 = deg2rad($lon1); | |
| 56 | 6 | $lat1 = deg2rad($lat1); | |
| 57 | 6 | $lon2 = deg2rad($lon2); | |
| 58 | 6 | $lat2 = deg2rad($lat2); | |
| 59 | |||
| 60 | 6 | my $dlon = $lon2 - $lon1; | |
| 61 | 6 | my $dlat = $lat2 - $lat1; | |
| 62 | 6 | my $a = (sin($dlat/2)) ** 2 + cos($lat1) * cos($lat2) * (sin($dlon/2)) ** 2; | |
| 63 | 6 | my $c = 2 * atan2(sqrt($a), sqrt(1-$a)); | |
| 64 | |||
| 65 | 6 | return 6371640 * $c | |
| 66 | } | ||
| 67 | |||
| 68 | 1 | 1; | |
| 69 | |||
| 70 | =head1 SEE ALSO | ||
| 71 | |||
| 72 | Inspired by: L<GIS::Distance> | ||
| 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 <johanb@athega.se>, 2010. http://www.athega.se/ | ||
| 83 | |||
| 84 | =cut | ||