We don't bite newbies here... much PerlMonks

Finding the Distance between longitude and latitude pairs

by cacharbe (Curate)
 on Mar 07, 2002 at 16:37 UTC Need Help??
 Category: Miscellaneous Author/Contact Info Chuck Charbeneau (cacharbe) ccharbeneau@lear.com Description: A while back my younger sister asked me to help her with homework from her college trig course. She had to solve a Great Circle Distance problem. After showing her the steps, it got me to thinking, and then reading, and then finally coding. What I found was quite interesting, and from that research spawned an application to find the distances between to corporate locations from their zip codes that I implemented on our intranet. There are actually a couple of different methods for determining the distance between 2 long/lat pairs and you need to be aware of the accuracy of each. The Great Circle method is the least accurate IIRC, but is the most accessible, as it is a part of Math::Trig. I wrote some code to do this using the various methods. As I mentioned, it finds the distances between two zip codes for which the long/lat pairs are known, but I've removed the database interface for brevity and just given the pertinent code. You should also read: this, this and (my favorite) this article. I was pretty explicit in my code so that my non-programming sister could understand it when I showed it too her to explain the different algorithms, but I think it gets the point across. C-. ```use strict; use Math::Trig qw(deg2rad pi great_circle_distance asin acos); ############################ ## Distances are in Miles ## ############################ my (\$lat1, \$long1); my (\$lat2, \$long2); print "Haversine : ". &Haversine(\$lat1, \$long1, \$lat2, \$long2) ."\n" +; print "Law Cosines : ". &LawCosines(\$lat1, \$long1, \$lat2, \$long2)."\n" +; print "Flat-Earth : ". &FlatEarth(\$lat1, \$long1, \$lat2, \$long2) ."\n" +; print "Great Circle: ". &GreatCircle(\$lat1, \$long1, \$lat2, \$long2)."\n +"; sub LawCosines { my (\$lat1, \$long1, \$lat2, \$long2) = @_; my \$r=3956; my \$dist = acos(sin(deg2rad(\$lat1))* sin(deg2rad(\$lat2))+ cos(deg2rad(\$lat1))* cos(deg2rad(\$lat2))* cos(deg2rad(\$long2)- deg2rad(\$long1))) * \$r; return \$dist; } sub FlatEarth { my (\$lat1, \$long1, \$lat2, \$long2) = @_; my \$r=3956; my \$a = (pi/2)- deg2rad(\$lat1); my \$b = (pi/2)- deg2rad(\$lat2); my \$c = sqrt(\$a**2 + \$b**2 - 2 * \$a *\$b *cos(deg2rad(\$long2)-deg2rad(\$long1))); my \$dist = \$c * \$r; return \$dist; } sub Haversine { my (\$lat1, \$long1, \$lat2, \$long2) = @_; my \$r=3956; \$dlong = deg2rad(\$long1) - deg2rad(\$long2); \$dlat = deg2rad(\$lat1) - deg2rad(\$lat2); \$a = sin(\$dlat/2)**2 +cos(deg2rad(\$lat1)) * cos(deg2rad(\$lat2)) * sin(\$dlong/2)**2; \$c = 2 * (asin(sqrt(\$a))); \$dist = \$r * \$c; return \$dist; } sub GreatCircle { my (\$lat1, \$long1, \$lat2, \$long2) = @_; my \$r=3956; my @zip1 = (deg2rad(\$long1), deg2rad(90-\$lat1)); my @zip2 = (deg2rad(\$long2), deg2rad(90-\$lat2)); my \$dist = great_circle_distance(@zip1, @zip2,\$r); return \$dist; } ```
Replies are listed 'Best First'.
•Re: Finding the Distance between longitude and latitude pairs
by merlyn (Sage) on Mar 07, 2002 at 19:24 UTC
Also - WWW::MapBlast by Lee Goddard. He's going to be using some of my code shown above in his next release.

C-.

Create A New User
Node Status?
node history
Node Type: sourcecode [id://150054]
help
Chatterbox?
 [usemodperl]: i'm under no obligation to explain myself mommy, and i'm not insisting, just asking a simple question [Veltro]: So, I'm confused on the matter actually. There are locked-down computers, but they need updates. How does that work? Isn't that contradictary? [usemodperl]: EXACTLY [usemodperl]: and the https only situation makes it even worse [Veltro]: How is that 'exactly'. Locked-down means you don't change or update. So why do you need to access CPAN? [Corion]: choroba: Heh - I think that's an interesting situation - you can't bootstrap a vanilla Perl then via CPAN, as IO::Socket::SSL is not in core, but also can't be installed from a cpan client. You need a wget/curl with SSL built-in to do that :) [usemodperl]: i was thinking of forcing something like archive.org to proxy from meta but they seem all https too :-/ [usemodperl]: why are you so obsessed with why i wanna know why i can't download from cpan via http anymore which break all aosrts of stuff you dont need to understand? [usemodperl]: (Veltro :-) [Corion]: usemodperl: Writing your own SSL-stripping proxy isn't hard, either.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2018-06-24 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (126 votes). Check out past polls.

Notices?