Geo-Distance-0.20000755000765000024 012040270270 13074 5ustar00aranstaff000000000000README100644000765000024 46112040270270 14016 0ustar00aranstaff000000000000Geo-Distance-0.20 This archive contains the distribution Geo-Distance, version 0.20: Calculate Distances and Closest Locations This software is copyright (c) 2012 by Aran Deltac. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Changes100644000765000024 720412040270270 14453 0ustar00aranstaff000000000000Geo-Distance-0.20Revision history for Perl extension Geo::Distance. 0.20 2012-10-19 - Fix a test that was comparing floating point numbers and falling over on some platforms. 0.19 2012-04-03 - Fix various typos. - Fix floating point error in the hsin formula. 0.18 2012-03-23 - Fix gcd calculation to use ** instead of ^. 0.17 2011-06-28 - Fix circular package dependencies with Geo::Distance::XS (not really an issue practically, but then again it wasn't really helping). 0.16 - No changes. The 0.15 package was broken. 0.15 - Stop "Too late to run INIT block" warning (gray ) 0.14 - Fix "splice() offset past end of array" as filed in RT 26446. 0.13 - Use Geo::Distance::XS if available. (gray ) - Declare Math::Trig dependency. 0.12 - Noted GIS::Distance in POD. - Some cleanups to install process. - Check for DBI in tests before using it. 0.11 : 2005-09-01 - Fixed some errors in the documentation. - Added an highly accurate ellipsoid formula. - lon_field and lat_field were not being used by closest. (D. Hageman) 0.10 : 2005-07-11 - The closest() method has a changed argument syntax and no longer supports array searches. - The closest() method works! - A real gcd formula (still, hsin is much better). - Tweaked docs. - Added some tests (yay!). 0.09 : 2005-04-01 - Modified the todo list to include ideas for future algorithms. - Fixed the nautical mile, mile, yard, and light second units. - Added the British spellings for kilometre, metre, and centimetre. - Added the poppy seed, barleycorn, rod, pole, perch, chain, furlong, league, fathom, millimeter, and millimetre units. - The totw.pl script was written by Ren and can be used to take over the world. 0.08 : 2005-03-20 - Updated the README description. - Removed debug print()s. Eeek! 0.07 : 2005-03-16 - Intermixed documentation with code so it is easier to keep the docs up-to-date. - OO interface only - method interface completely removed. - By default no units are defined. Call default_units. - Slightly more precise measurement of the base kilometer rho. - Added "nautical mile" unit type. - Reworked the closest() function. 0.06 : 2004-06-29 - Optional Haversine formula. - Misc documentation tweaks. 0.05 : 2003-03-19 - Added a note in the documentation about the inaccuracies of using Math::Trig. - The 'mile' unit was being calculated wrong which meant it was returning very inaccurate distances. - Fixed a silly bug where a sub was being relied on that no longer exists. - Documentation tweaks as usual. 0.04 : 2003-02-18 - Documentation revised once again. - Added reg_unit() for adding your own unit type. - find_closest has been overhauled: - Now accepts more than one field in the field=>'' parameter. - Will now return an array reference of distances instead of attaching the distances to the locations array ref. - A little more effecient. - Now accepts a count argument. - Accepts an array reference for searching. Mostly good for testing, but who knows? - Removed geo_ portion of names for exported functions. - Removed some of the input checking. Just not necessary. - Enhanced tests. Now we're actually doing some real testing. Need more tests tho. 0.03 : 2003-02-15 - Documentation modified. - Added find_closest() which accepts a $dbh for searching in an SQL database. - distance_dirty() can now accept locations as array refs. 0.02 : 2003-02-14 - Based on a suggestion by Jack D. I migrated the code to use Math::Trig for most of the distance math. - POD documentation written. - Object oriented interface created. LICENSE100644000765000024 4365012040270270 14212 0ustar00aranstaff000000000000Geo-Distance-0.20This software is copyright (c) 2012 by Aran Deltac. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2012 by Aran Deltac. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2012 by Aran Deltac. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End META.yml100644000765000024 114112040270270 14423 0ustar00aranstaff000000000000Geo-Distance-0.20--- abstract: 'Calculate Distances and Closest Locations' author: - 'Aran Deltac ' build_requires: Test::Fatal: 0.007 Test::Simple: 0.94 configure_requires: ExtUtils::MakeMaker: 6.30 dynamic_config: 0 generated_by: 'Dist::Zilla version 4.300028, CPAN::Meta::Converter version 2.120921' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Geo-Distance requires: Math::Complex: 0 resources: bugtracker: https://github.com/bluefeet/Geo-Distance/issues repository: git://github.com/bluefeet/Geo-Distance.git version: 0.20 MANIFEST100644000765000024 27012040270270 14265 0ustar00aranstaff000000000000Geo-Distance-0.20Changes LICENSE MANIFEST META.yml Makefile.PL README lib/Geo/Distance.pm t/00-compile.t t/basic.t t/issue-github-2.t t/issue-github-3.t t/release-pod-coverage.t t/release-pod-syntax.t t000755000765000024 012040270270 13260 5ustar00aranstaff000000000000Geo-Distance-0.20basic.t100755000765000024 713012040270270 14672 0ustar00aranstaff000000000000Geo-Distance-0.20/t#!/usr/bin/env perl use strict; use warnings; use Test::More; BEGIN { $ENV{GEO_DISTANCE_PP} = 1; use_ok( 'Geo::Distance' ); } ok(! defined &distance_hsin, 'prevent XS from loading; use pure Perl'); my $geo = eval{ return Geo::Distance->new() }; ok(!$@,'create a Geo::Distance object'); my $dist = eval{ $geo->distance( 'mile', "-81.044","35.244", "-80.8272","35.1935" ) }; ok( (not($@) and int($dist)==12), 'measure a distance by mile' ); SKIP: { eval{ require DBD::SQLite }; skip "DBD::SQLite not installed" if $@; my $dbh = eval{ return DBI->connect( "dbi:SQLite:dbname=test.db", "", "", {AutoCommit => 0} ) }; ok( !$@, 'connect/create SQLite test.db' ); eval{ load_zips( $dbh ) }; ok( !$@, 'populate test.db with sample zip code locations' ); eval{ $geo->closest( dbh=>$dbh, table=>'zips', lon=>'-80.7881', lat=>'35.22', unit=>'mile', distance=>'5' ) }; ok( !$@, 'run closest' ); my $locations; $locations = $geo->closest( dbh=>$dbh, table=>'zips', lon=>'-80.7881', lat=>'35.22', unit=>'mile', distance=>'5' ); ok( (@$locations==11), 'found correct number of locations by mile' ); $locations = $geo->closest( dbh=>$dbh, table=>'zips', lon=>'-80.8577', lat=>'35.1316', unit=>'kilometer', distance=>'5' ); ok( (@$locations==2), 'found correct number of locations by kilometer' ); $locations = $geo->closest( dbh=>$dbh, table=>'zips', lon=>'-80.8577', lat=>'35.1316', unit=>'mile', distance=>'5', count=>3 ); ok( (@$locations==3), 'found correct number of locations limited by count' ); $dbh->disconnect; unlink('test.db'); } sub load_zips { my $dbh = shift; $dbh->do(q{ CREATE TABLE zips ( zip CHAR(5), lon DECIMAL(13,3), lat DECIMAL(13,3) ) }); my $sth = $dbh->prepare(q{ INSERT INTO zips (lon,lat,zip) VALUES (?,?,?) }); $sth->execute("-81.044","35.244","28012"); $sth->execute("-81.0306","35.3119","28120"); $sth->execute("-81.0079","35.0972","28217"); $sth->execute("-80.9604","35.1467","28278"); $sth->execute("-80.9586","35.026","29715"); $sth->execute("-80.9571","35.2731","28214"); $sth->execute("-80.8967","35.1596","28273"); $sth->execute("-80.8964","35.2358","28208"); $sth->execute("-80.8858","35.0709","28134"); $sth->execute("-80.8702","35.2834","28216"); $sth->execute("-80.8647","35.422","28078"); $sth->execute("-80.8583","35.2081","28203"); $sth->execute("-80.8577","35.1316","28210"); $sth->execute("-80.8559","35.1796","28209"); $sth->execute("-80.8419","35.229","28202"); $sth->execute("-80.8272","35.1935","28207"); $sth->execute("-80.8265","35.2522","28206"); $sth->execute("-80.8232","35.2132","28204"); $sth->execute("-80.8209","35.2886","28269"); $sth->execute("-80.8167","35.0869","28226"); $sth->execute("-80.8002","35.1345","28277"); $sth->execute("-80.7932","35.1677","28211"); $sth->execute("-80.7881","35.22","28205"); $sth->execute("-80.776","35.2725","28262"); $sth->execute("-80.7669","35.1355","28270"); $sth->execute("-80.7501","35.3179","28213"); $sth->execute("-80.7448","35.1908","28212"); $sth->execute("-80.7387","35.244","28215"); $sth->execute("-80.7279","34.9553","28173"); $sth->execute("-80.7136","35.1219","28105"); $sth->execute("-80.6846","35.1936","28227"); $sth->execute("-80.6597","35.0831","28079"); $sth->execute("-80.6594","35.3247","28075"); $sth->execute("-80.6162","35.4141","28027"); $sth->execute("-80.5319","35.2477","28107"); $sth->execute("-80.53","35.3716","28025"); $dbh->commit; } done_testing; Makefile.PL100644000765000024 204412040270270 15127 0ustar00aranstaff000000000000Geo-Distance-0.20 use strict; use warnings; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "Calculate Distances and Closest Locations", "AUTHOR" => "Aran Deltac ", "BUILD_REQUIRES" => { "Test::Fatal" => "0.007", "Test::Simple" => "0.94" }, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30" }, "DISTNAME" => "Geo-Distance", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "Geo::Distance", "PREREQ_PM" => { "Math::Complex" => 0 }, "VERSION" => "0.20", "test" => { "TESTS" => "t/*.t" } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); 00-compile.t100644000765000024 312512040270270 15453 0ustar00aranstaff000000000000Geo-Distance-0.20/t#!perl use strict; use warnings; use Test::More; use File::Find; use File::Temp qw{ tempdir }; my @modules; find( sub { return if $File::Find::name !~ /\.pm\z/; my $found = $File::Find::name; $found =~ s{^lib/}{}; $found =~ s{[/\\]}{::}g; $found =~ s/\.pm$//; # nothing to skip push @modules, $found; }, 'lib', ); sub _find_scripts { my $dir = shift @_; my @found_scripts = (); find( sub { return unless -f; my $found = $File::Find::name; # nothing to skip open my $FH, '<', $_ or do { note( "Unable to open $found in ( $! ), skipping" ); return; }; my $shebang = <$FH>; return unless $shebang =~ /^#!.*?\bperl\b\s*$/; push @found_scripts, $found; }, $dir, ); return @found_scripts; } my @scripts; do { push @scripts, _find_scripts($_) if -d $_ } for qw{ bin script scripts }; my $plan = scalar(@modules) + scalar(@scripts); $plan ? (plan tests => $plan) : (plan skip_all => "no tests to run"); { # fake home for cpan-testers # no fake requested ## local $ENV{HOME} = tempdir( CLEANUP => 1 ); like( qx{ $^X -Ilib -e "require $_; print '$_ ok'" }, qr/^\s*$_ ok/s, "$_ loaded ok" ) for sort @modules; SKIP: { eval "use Test::Script 1.05; 1;"; skip "Test::Script needed to test script compilation", scalar(@scripts) if $@; foreach my $file ( @scripts ) { my $script = $file; $script =~ s!.*/!!; script_compiles( $file, "$script script compiles" ); } } } issue-github-2.t100755000765000024 215712040270270 16364 0ustar00aranstaff000000000000Geo-Distance-0.20/t#!/usr/bin/env perl use strict; use warnings; # https://github.com/bluefeet/Geo-Distance/issues/2 # "^" used instead of "**" use Test::More; use Math::Trig qw( asin ); BEGIN { use_ok('Geo::Distance') } my $geo = Geo::Distance->new(); $geo->formula('gcd'); my $new_value = $geo->distance( 'mile', "-81.044","35.244", "-80.8272","35.1935" ); my $old_value = old_gcd( $geo, 'mile', "-81.044","35.244", "-80.8272","35.1935" ); $geo->formula('hsin'); my $control_value = $geo->distance( 'mile', "-81.044","35.244", "-80.8272","35.1935" ); ok( abs($new_value - $control_value) < 0.00000000001, 'gcd now produces same result as hsin' ) or diag "$new_value is not equal to $control_value"; ok( abs($old_value - $control_value) > 0.00000000001, 'old gcd did not produce same result as hsin' ) or diag "$old_value is equal to $control_value"; sub old_gcd { my($geo,$unit,$lon1,$lat1,$lon2,$lat2) = @_; $unit = $geo->{units}->{$unit}; my $c = 2*asin( sqrt( ( sin(($lat1-$lat2)/2) )^2 + cos($lat1) * cos($lat2) * ( sin(($lon1-$lon2)/2) )^2 ) ); return $unit * $c; } done_testing; issue-github-3.t100755000765000024 50712040270270 16342 0ustar00aranstaff000000000000Geo-Distance-0.20/t#!/usr/bin/env perl use strict; use warnings; # https://github.com/bluefeet/Geo-Distance/issues/3 # Square root of negative value use Test::More; use Test::Fatal; BEGIN { use_ok('Geo::Distance') } my $geo = Geo::Distance->new(); is( exception { $geo->distance("mile", 175, 12, -5, -12) }, undef, ); done_testing; Geo000755000765000024 012040270270 14275 5ustar00aranstaff000000000000Geo-Distance-0.20/libDistance.pm100644000765000024 4670212040270270 16556 0ustar00aranstaff000000000000Geo-Distance-0.20/lib/Geopackage Geo::Distance; { $Geo::Distance::VERSION = '0.20'; } use strict; use warnings; =head1 NAME Geo::Distance - Calculate Distances and Closest Locations =head1 SYNOPSIS use Geo::Distance; my $geo = new Geo::Distance; $geo->formula('hsin'); $geo->reg_unit( 'toad_hop', 200120 ); $geo->reg_unit( 'frog_hop' => 6 => 'toad_hop' ); my $distance = $geo->distance( 'unit_type', $lon1,$lat1 => $lon2,$lat2 ); my $locations = $geo->closest( dbh => $dbh, table => $table, lon => $lon, lat => $lat, unit => $unit_type, distance => $dist_in_unit ); =head1 DESCRIPTION This perl library aims to provide as many tools to make it as simple as possible to calculate distances between geographic points, and anything that can be derived from that. Currently there is support for finding the closest locations within a specified distance, to find the closest number of points to a specified point, and to do basic point-to-point distance calculations. =head1 DECOMMISSIONED The L module is being worked on as a replacement for this module. In the near future Geo::Distance will become a lightweight wrapper around GIS::Distance so that legacy code benefits from fixes to GIS::Distance through the old Geo::Distance API. For any new developement I suggest that you look in to GIS::Distance. =head1 STABILITY The interface to Geo::Distance is fairly stable nowadays. If this changes it will be noted here. 0.10 - The closest() method has a changed argument syntax and no longer supports array searches. 0.09 - Changed the behavior of the reg_unit function. 0.07 - OO only, and other changes all over. =cut use Carp; use Math::Trig qw( great_circle_distance deg2rad rad2deg acos pi asin tan atan ); use constant KILOMETER_RHO => 6371.64; =head1 PROPERTIES =head2 UNITS All functions accept a unit type to do the computations of distance with. By default no units are defined in a Geo::Distance object. You can add units with reg_unit() or create some default units with default_units(). =head2 LATITUDE AND LONGITUDE When a function needs a longitude and latitude, they must always be in decimal degree format. Here is some sample code for converting from other formats to decimal: # DMS to Decimal my $decimal = $degrees + ($minutes/60) + ($seconds/3600); # Precision Six Integer to Decimal my $decimal = $integer * .000001; If you want to convert from decimal radians to degrees you can use Math::Trig's rad2deg function. =head1 METHODS =head2 new my $geo = new Geo::Distance; my $geo = new Geo::Distance( no_units=>1 ); Returns a blessed Geo::Distance object. The new constructor accepts one optional argument. no_units - Whether or not to load the default units. Defaults to 0 (false). kilometer, kilometre, meter, metre, centimeter, centimetre, millimeter, millimetre, yard, foot, inch, light second, mile, nautical mile, poppy seed, barleycorn, rod, pole, perch, chain, furlong, league, fathom =cut sub new { my $class = shift; my $self = bless {}, $class; my %args = @_; $self->{formula} = 'hsin'; $self->{units} = {}; if(!$args{no_units}){ $self->reg_unit( KILOMETER_RHO, 'kilometer' ); $self->reg_unit( 1000, 'meter', => 'kilometer' ); $self->reg_unit( 100, 'centimeter' => 'meter' ); $self->reg_unit( 10, 'millimeter' => 'centimeter' ); $self->reg_unit( 'kilometre' => 'kilometer' ); $self->reg_unit( 'metre' => 'meter' ); $self->reg_unit( 'centimetre' => 'centimeter' ); $self->reg_unit( 'millimetre' => 'millimeter' ); $self->reg_unit( 'mile' => 1609.344, 'meter' ); $self->reg_unit( 'nautical mile' => 1852, 'meter' ); $self->reg_unit( 'yard' => 0.9144, 'meter' ); $self->reg_unit( 3, 'foot' => 'yard' ); $self->reg_unit( 12, 'inch' => 'foot' ); $self->reg_unit( 'light second' => 299792458, 'meter' ); $self->reg_unit( 'poppy seed' => 2.11, 'millimeter' ); $self->reg_unit( 'barleycorn' => 8.467, 'millimeter' ); $self->reg_unit( 'rod' => 5.0292, 'meter' ); $self->reg_unit( 'pole' => 'rod' ); $self->reg_unit( 'perch' => 'rod' ); $self->reg_unit( 'chain' => 20.1168, 'meter' ); $self->reg_unit( 'furlong' => 201.168, 'meter' ); $self->reg_unit( 'league' => 4.828032, 'kilometer' ); $self->reg_unit( 1.8288, 'fathom' => 'meter' ); } # Number of units in a single degree (lat or lon) at the equator. # Derived from: $geo->distance( 'kilometer', 10,0, 11,0 ) / $geo->{units}->{kilometer} $self->{deg_ratio} = 0.0174532925199433; return $self; } =head2 formula if($geo->formula eq 'hsin'){ ... } $geo->formula('cos'); Allows you to retrieve and set the formula that is currently being used to calculate distances. The available formulas are hsin, polar, cos and mt. hsin is the default and mt/cos are deprecated in favor of hsin. Polar should be used when calculating coordinates near the poles. =cut sub formula { my $self = shift; my $formula = shift; if( $formula !~ /^(mt|cos|hsin|polar|gcd|tv)$/s ){ croak('Invalid formula (only mt, cos, hsin, polar, gcd and tv are supported)'); }else{ $self->{formula} = $formula; } return $formula; } =head2 reg_unit $geo->reg_unit( $radius, $key ); $geo->reg_unit( $key1 => $key2 ); $geo->reg_unit( $count1, $key1 => $key2 ); $geo->reg_unit( $key1 => $count2, $key2 ); $geo->reg_unit( $count1, $key1 => $count2, $key2 ); This method is used to create custom unit types. There are several ways of calling it, depending on if you are defining the unit from scratch, or if you are basing it off of an existing unit (such as saying 12 inches = 1 foot ). When defining a unit from scratch you pass the name and rho (radius of the earth in that unit) value. So, if you wanted to do your calculations in human adult steps you would have to have an average human adult walk from the crust of the earth to the core (ignore the fact that this is impossible). So, assuming we did this and we came up with 43,200 steps, you'd do something like the following. # Define adult step unit. $geo->reg_unit( 43200, 'adult step' ); # This can be read as "It takes 43,200 adult_steps to walk the radius of the earth". Now, if you also wanted to do distances in baby steps you might think "well, now I gotta get a baby to walk to the center of the earth". But, you don't have to! If you do some research you'll find (no research was actually conducted) that there are, on average, 4.7 baby steps in each adult step. # Define baby step unit. $geo->reg_unit( 4.7, 'baby step' => 'adult step' ); # This can be read as "4.7 baby steps is the same as one adult step". And if we were doing this in reverse and already had the baby step unit but not the adult step, you would still use the exact same syntax as above. =cut sub reg_unit { my $self = shift; my $units = $self->{units}; my($count1,$key1,$count2,$key2); $count1 = shift; if($count1=~/[^\.0-9]/ or !@_){ $key1=$count1; $count1=1; } else{ $key1 = shift; } if(!@_){ $units->{$key1} = $count1; }else{ $count2 = shift; if($count2=~/[^\.0-9]/ or !@_){ $key2=$count2; $count2=1; } else{ $key2 = shift; } ($key1,$key2) = ($key2,$key1) if( defined $units->{$key1} ); $units->{$key1} = ($units->{$key2}*$count1) / $count2; } } =head2 distance my $distance = $geo->distance( 'unit_type', $lon1,$lat1 => $lon2,$lat2 ); Calculates the distance between two lon/lat points. =cut sub distance { my($self,$unit,$lon1,$lat1,$lon2,$lat2) = @_; croak('Unkown unit type "'.$unit.'"') unless($unit = $self->{units}->{$unit}); if($self->{formula} eq 'mt'){ return great_circle_distance( deg2rad($lon1), deg2rad(90 - $lat1), deg2rad($lon2), deg2rad(90 - $lat2), $unit ); }else{ $lon1 = deg2rad($lon1); $lat1 = deg2rad($lat1); $lon2 = deg2rad($lon2); $lat2 = deg2rad($lat2); my $c; if($self->{formula} eq 'cos'){ my $a = sin($lat1) * sin($lat2); my $b = cos($lat1) * cos($lat2) * cos($lon2 - $lon1); $c = acos($a + $b); } elsif($self->{formula} eq 'hsin'){ my $dlon = $lon2 - $lon1; my $dlat = $lat2 - $lat1; my $a = (sin($dlat/2)) ** 2 + cos($lat1) * cos($lat2) * (sin($dlon/2)) ** 2; $c = 2 * atan2(sqrt($a), sqrt(abs(1-$a))); } elsif($self->{formula} eq 'polar'){ my $a = pi/2 - $lat1; my $b = pi/2 - $lat2; $c = sqrt( $a ** 2 + $b ** 2 - 2 * $a * $b * cos($lon2 - $lon1) ); } elsif($self->{formula} eq 'gcd'){ $c = 2*asin( sqrt( ( sin(($lat1-$lat2)/2) )**2 + cos($lat1) * cos($lat2) * ( sin(($lon1-$lon2)/2) )**2 ) ); # Eric Samuelson recommended this formula. # http://forums.devshed.com/t54655/sc3d021a264676b9b440ea7cbe1f775a1.html # http://williams.best.vwh.net/avform.htm # It seems to produce the same results at the hsin formula, so... #my $dlon = $lon2 - $lon1; #my $dlat = $lat2 - $lat1; #my $a = (sin($dlat / 2)) ** 2 # + cos($lat1) * cos($lat2) * (sin($dlon / 2)) ** 2; #$c = 2 * atan2(sqrt($a), sqrt(1 - $a)); } elsif($self->{formula} eq 'tv'){ my($a,$b,$f) = (6378137,6356752.3142,1/298.257223563); my $l = $lon2 - $lon1; my $u1 = atan((1-$f) * tan($lat1)); my $u2 = atan((1-$f) * tan($lat2)); my $sin_u1 = sin($u1); my $cos_u1 = cos($u1); my $sin_u2 = sin($u2); my $cos_u2 = cos($u2); my $lambda = $l; my $lambda_pi = 2 * pi; my $iter_limit = 20; my($cos_sq_alpha,$sin_sigma,$cos2sigma_m,$cos_sigma,$sigma); while( abs($lambda-$lambda_pi) > 1e-12 && --$iter_limit>0 ){ my $sin_lambda = sin($lambda); my $cos_lambda = cos($lambda); $sin_sigma = sqrt(($cos_u2*$sin_lambda) * ($cos_u2*$sin_lambda) + ($cos_u1*$sin_u2-$sin_u1*$cos_u2*$cos_lambda) * ($cos_u1*$sin_u2-$sin_u1*$cos_u2*$cos_lambda)); $cos_sigma = $sin_u1*$sin_u2 + $cos_u1*$cos_u2*$cos_lambda; $sigma = atan2($sin_sigma, $cos_sigma); my $alpha = asin($cos_u1 * $cos_u2 * $sin_lambda / $sin_sigma); $cos_sq_alpha = cos($alpha) * cos($alpha); $cos2sigma_m = $cos_sigma - 2*$sin_u1*$sin_u2/$cos_sq_alpha; my $cc = $f/16*$cos_sq_alpha*(4+$f*(4-3*$cos_sq_alpha)); $lambda_pi = $lambda; $lambda = $l + (1-$cc) * $f * sin($alpha) * ($sigma + $cc*$sin_sigma*($cos2sigma_m+$cc*$cos_sigma*(-1+2*$cos2sigma_m*$cos2sigma_m))); } undef if( $iter_limit==0 ); my $usq = $cos_sq_alpha*($a*$a-$b*$b)/($b*$b); my $aa = 1 + $usq/16384*(4096+$usq*(-768+$usq*(320-175*$usq))); my $bb = $usq/1024 * (256+$usq*(-128+$usq*(74-47*$usq))); my $delta_sigma = $bb*$sin_sigma*($cos2sigma_m+$bb/4*($cos_sigma*(-1+2*$cos2sigma_m*$cos2sigma_m)- $bb/6*$cos2sigma_m*(-3+4*$sin_sigma*$sin_sigma)*(-3+4*$cos2sigma_m*$cos2sigma_m))); $c = ( $b*$aa*($sigma-$delta_sigma) ) / $self->{units}->{meter}; } else{ croak('Unkown distance formula "'.$self->{formula}.'"'); } return $unit * $c; } } =head2 closest my $locations = $geo->closest( dbh => $dbh, table => $table, lon => $lon, lat => $lat, unit => $unit_type, distance => $dist_in_unit ); This method finds the closest locations within a certain distance and returns an array reference with a hash for each location matched. The closest method requires the following arguments: dbh - a DBI database handle table - a table within dbh that contains the locations to search lon - the longitude of the center point lat - the latitude of the center point unit - the unit of measurement to use, such as "meter" distance - the distance, in units, from the center point to find locations The following arguments are optional: lon_field - the name of the field in the table that contains the longitude, defaults to "lon" lat_field - the name of the field in the table that contains the latitude, defaults to "lat" fields - an array reference of extra field names that you would like returned with each location where - additional rules for the where clause of the sql bind - an array reference of bind variables to go with the placeholders in where sort - whether to sort the locations by their distance, making the closest location the first returned count - return at most these number of locations (implies sort => 1) This method uses some very simplistic calculations to SQL select out of the dbh. This means that the SQL should work fine on almost any database (only tested on MySQL and SQLite so far) and this also means that it is fast. Once this sub set of locations has been retrieved then more precise calculations are made to narrow down the result set. Remember, though, that the farther out your distance is, and the more locations in the table, the slower your searches will be. =cut sub closest { my $self = shift; my %args = @_; # Set defaults and prepare. my $dbh = $args{dbh} || croak('You must supply a database handle'); $dbh->isa('DBI::db') || croak('The dbh must be a DBI database handle'); my $table = $args{table} || croak('You must supply a table name'); my $lon = $args{lon} || croak('You must supply a longitude'); my $lat = $args{lat} || croak('You must supply a latitude'); my $distance = $args{distance} || croak('You must supply a distance'); my $unit = $args{unit} || croak('You must specify a unit type'); my $unit_size = $self->{units}->{$unit} || croak('This unit type is not known'); my $degrees = $distance / ( $self->{deg_ratio} * $unit_size ); my $lon_field = $args{lon_field} || 'lon'; my $lat_field = $args{lat_field} || 'lat'; my $fields = $args{fields} || []; unshift @$fields, $lon_field, $lat_field; $fields = join( ',', @$fields ); my $count = $args{count} || 0; my $sort = $args{sort} || ( $count ? 1 : 0 ); my $where = qq{$lon_field >= ? AND $lat_field >= ? AND $lon_field <= ? AND $lat_field <= ?}; $where .= ( $args{where} ? " AND ($args{where})" : '' ); my @bind = ( $lon-$degrees, $lat-$degrees, $lon+$degrees, $lat+$degrees, ( $args{bind} ? @{$args{bind}} : () ) ); # Retrieve locations. my $sth = $dbh->prepare(qq{ SELECT $fields FROM $table WHERE $where }); $sth->execute( @bind ); my $locations = []; while(my $location = $sth->fetchrow_hashref){ push @$locations, $location; } # Calculate distances. my $closest = []; foreach my $location (@$locations){ $location->{distance} = $self->distance( $unit, $lon, $lat, $location->{$lon_field}, $location->{$lat_field} ); if( $location->{distance} <= $distance ){ push @$closest, $location; } } $locations = $closest; # Sort. if( $sort ){ my $location; for(my $i=@$locations-1; $i>=0; $i--){ for(my $j=$i-1; $j>=0; $j--){ if($locations->[$i]->{distance} < $locations->[$j]->{distance}){ $location = $locations->[$i]; $locations->[$i] = $locations->[$j]; $locations->[$j] = $location; } } } } # Split for count. if( $count and $count < @$locations ){ splice @$locations, $count; } return $locations; } unless( $ENV{GEO_DISTANCE_PP} ) { eval "use Geo::Distance::XS"; } 1; __END__ =head1 FORMULAS Currently Geo::Distance only has spherical and flat type formulas. If you have any information concerning ellipsoid and geoid formulas, the author would much appreciate some links to this information. =head2 tv: Thaddeus Vincenty Formula This is a highly accurate ellipsoid formula. For most applications hsin will be faster and accurate enough. I've read that this formula can be accurate to within a few millimeters. This formula is still considered alpha quality. It has not been tested enough to be used in production. =head2 hsin: Haversine 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 The hsin formula is the new standard formula for Geo::Distance because of it's improved accuracy over the cos formula. =head2 polar: Polar Coordinate Flat-Earth Formula a = pi/2 - lat1 b = pi/2 - lat2 c = sqrt( a^2 + b^2 - 2 * a * b * cos(lon2 - lon1) ) d = R * c While implimented, this formula has not been tested much. If you use it PLEASE share your results with the author! =head2 cos: Law of Cosines for Spherical Trigonometry a = sin(lat1) * sin(lat2) b = cos(lat1) * cos(lat2) * cos(lon2 - lon1) c = arccos(a + b) d = R * c Although this formula is mathematically exact, it is unreliable for small distances because the inverse cosine is ill-conditioned. =head2 gcd: Great Circle Distance. c = 2 * asin( sqrt( ( sin(( lat1 - lat2 )/2) )^2 + cos( lat1 ) * cos( lat2 ) * ( sin(( lon1 - lon2 )/2) )^2 ) ) Similar notes to the mt and cos formula, not too terribly accurate. =head2 mt: Math::Trig great_circle_distance This formula uses Meth::Trig's great_circle_distance function which at this time uses math almost exactly the same as the cos formula. If you want to use the cos formula you may find that mt will calculate faster (untested assumption). For some reason mt and cos return slight differences at very close distances. The mt formula has the same drawbacks as the cos formula. This is the same formula that was previously the only one used by Geo::Distance (ending at version 0.06) and was wrongly called the "gcd" formula. Math::Trig states that the formula that it uses is: lat0 = 90 degrees - phi0 lat1 = 90 degrees - phi1 d = R * arccos(cos(lat0) * cos(lat1) * cos(lon1 - lon01) + sin(lat0) * sin(lat1)) =head1 NOTES If L is installed, this module will use it. You can stick with the pure Perl version by setting the GEO_DISTANCE_PP environment variable before using this module. =head1 TODO =over 4 =item * A second pass should be done in closest before distance calculations are made that does an inner radius simplistic calculation to find the locations that are obviously within the distance needed. =item * Tests! We need more tests! =item * For NASA-quality accuracy a geoid forumula. =item * The closest() method needs to be more flexible and (among other things) allow table joins. =back =head1 SEE ALSO L - Inverse and hyperbolic trigonemetric Functions. L - A overview of calculating distances. L - Aviation Formulary. =head1 AUTHOR Aran Clary Deltac =head1 CONTRIBUTORS gray, =head1 LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. release-pod-syntax.t100644000765000024 45012040270270 17310 0ustar00aranstaff000000000000Geo-Distance-0.20/t#!perl BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use Test::More; eval "use Test::Pod 1.41"; plan skip_all => "Test::Pod 1.41 required for testing POD" if $@; all_pod_files_ok(); release-pod-coverage.t100644000765000024 76512040270270 17566 0ustar00aranstaff000000000000Geo-Distance-0.20/t#!perl BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use Test::More; eval "use Test::Pod::Coverage 1.08"; plan skip_all => "Test::Pod::Coverage 1.08 required for testing POD coverage" if $@; eval "use Pod::Coverage::TrustPod"; plan skip_all => "Pod::Coverage::TrustPod required for testing POD coverage" if $@; all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' });