Important alert: (current site time 7/15/2013 9:15:00 PM EDT)
 

VB icon

Calculate Distance and Radius using Latitude and Longitude in Perl

Email
Submitted on: 11/15/2005 9:05:16 PM
By: ZipCodeWorld  
Level: Intermediate
User Rating: Unrated
Compatibility: 5.0 (all versions)
Views: 10921
 
     This function calculates the distance between two locations by using latitude and longitude from ZIP code, postal code or postcode. The result is available in miles, kilometers or nautical miles based on great circle distance calculation.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: Calculate Distance and Radius using Latitude and Longitude in Perl
= Description:This function calculates the distance between two locations by using latitude and longitude from ZIP code, postal code or postcode. The result is available in miles, kilometers or nautical miles based on great circle distance calculation.
= By: ZipCodeWorld
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=670&lngWId=6=for details.=**************************************

#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: :::
#::: This routine calculates the distance between two points (given the :::
#::: latitude/longitude of those points). It is being used to calculate :::
#::: the distance between two ZIP Codes or Postal Codes using our:::
#::: ZIPCodeWorld(TM) and PostalCodeWorld(TM) products. :::
#::: :::
#::: Definitions::::
#:::South latitudes are negative, east longitudes are positive:::
#::: :::
#::: Passed to function::::
#:::lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :::
#:::lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :::
#:::unit = the unit you desire for results:::
#:::where: 'M' is statute miles:::
#::: 'K' is kilometers (default):::
#::: 'N' is nautical miles :::
#::: :::
#::: United States ZIP Code/ Canadian Postal Code databases with latitude:::
#::: & longitude are available at http://www.zipcodeworld.com:::
#::: :::
#::: For enquiries, please contact sales@zipcodeworld.com:::
#::: :::
#::: Official Web site: http://www.zipcodeworld.com :::
#::: :::
#::: Hexa Software Development Center © All Rights Reserved 2005:::
#::: :::
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
$pi = atan2(1,1) * 4;
sub distance {
	my ($lat1, $lon1, $lat2, $lon2, $unit) = @_;
	my $theta = $lon1 - $lon2;
	my $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
 $dist = acos($dist);
 $dist = rad2deg($dist);
 $dist = $dist * 60 * 1.1515;
 if ($unit eq "K") {
 	$dist = $dist * 1.609344;
 } elsif ($unit eq "N") {
 	$dist = $dist * 0.8684;
		}
	return ($dist);
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: This function get the arccos function using arctan function:::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub acos {
	my ($rad) = @_;
	my $ret = atan2(sqrt(1 - $rad**2), $rad);
	return $ret;
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: This function converts decimal degrees to radians :::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub deg2rad {
	my ($deg) = @_;
	return ($deg * $pi / 180);
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: This function converts radians to decimal degrees :::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub rad2deg {
	my ($rad) = @_;
	return ($rad * 180 / $pi);
}
print distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles\n";
print distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers\n";
print distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles\n";


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.