|
-
May 23rd, 2005, 05:10 PM
#1
Thread Starter
Lively Member
distance between two zip codes
Hey guys. Just wanted to know if someone can give me an idea of how to calculate the distance between two zip codes. I was searching the web for information and the only thing I have found is what follows. Can anyone tells me how to convert this into vb.net code? ( I am pretty new at this).
Thanks in advance
code:
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: :::
#::: 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 [email protected] :::
#::: :::
#::: Official Web site: http://www.zipcodeworld.com :::
#::: :::
#::: Hexa Software Development Center © All Rights Reserved 2004 :::
#::: :::
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
$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";
None for now 
-
May 23rd, 2005, 05:22 PM
#2
Re: distance between two zip codes
That's seems like trig to me to work out the distance between two longtitude/latitude positions. Should be pretty straightforward once you get to understand the meaning of it. Also, check this out.
-
May 24th, 2005, 08:35 AM
#3
Hyperactive Member
Re: distance between two zip codes
What language is that?
Keep in mind that, though latitudinal lines are relatively equidistant (I think one degree of latitude is approximately 60 nautical miles)
longitudinal lines (the vertical ones) converge at the poles,
i.e. the further north or south of the equator you go the less distance between the same longitudes.
e.g. at Corpus Christi Texas one degree of longitude ~ 61 statute miles, while at Seattle Washington it's ~ 46 mi.
Anchorage Alaska ~ 33 mi.
North pole ~ 0 mi
i.e. it isn't a simple Pythagorean calc of a hypotenuse.
Talk about 'throwing you a curve'!
I have no idea if the above code accounts for that.
Last edited by DaveBo; May 24th, 2005 at 08:38 AM.
Reason: Oops
"The wise man doesn't know all the answers, but he knows where to find them."
VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15
-
May 24th, 2005, 08:46 AM
#4
Re: distance between two zip codes
Blimey. That looks a bit like Perl to me. My stomach is turning at the thought.
I would have thought its a simple matter of finding the Delta Longitude, and Delta Latitude (as fractions of the earth's circumference) then calculating the hypotenuse. The curvature of the earth shouldn't make much difference because horizontal would cancel out the vertical. Then multiply out of circumference units to get miles or KM as you wish.
This theory may only work inside my head, the universe simulated in here is sometimes a bit wonky and invisible from some angles.
I don't live here any more.
-
May 24th, 2005, 08:56 AM
#5
Hyperactive Member
Re: distance between two zip codes
Again, I have no idea what the code shown is doing, but,
unfortunately it isn't a simple sqrt of the sum of sqrs.
Distances between points on the surface of the earth are not straight lines but arcs, in sailing/navigation they're called "great circles".
try a Google for: "great circle" distances
"The wise man doesn't know all the answers, but he knows where to find them."
VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15
-
May 24th, 2005, 09:10 AM
#6
Re: distance between two zip codes
First off you should note that the code is not getting the distance between zipcodes, but between 2 set of longitude latitudes. You cant really get the distance between zip codes via code. You need to do a look up of, what I guess they are doing here, a center point for the zip codes. Problem is zipcode ranges change all the time. Now with that said, I have translated the code
Code:
Dim pi As Double = System.Math.Atan2(1, 1) * 4
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(Distance(32.9697, -96.80322, 29.46786, -98.53506, "M") & " Miles")
End Sub
Private Function Distance(ByVal lat1 As Double, ByVal lon1 As Double, ByVal lat2 As Double, ByVal lon2 As Double, ByVal unit As String) As Double
Dim theta As Double = lon1 - lon2
Dim dist = System.Math.Sin(deg2rad(lat1)) * System.Math.Sin(deg2rad(lat2)) + System.Math.Cos(deg2rad(lat1)) * System.Math.Cos(deg2rad(lat2)) * System.Math.Cos(deg2rad(theta))
dist = System.Math.Acos(dist)
dist = rad2deg(dist)
dist = dist * 60 * 1.1515
Select Case unit
Case "K"
dist = dist * 1.609344
Case "N"
dist = dist * 0.8684
End Select
Return dist
End Function
Private Function deg2rad(ByVal deg As Double) As Double
Return (deg * pi / 180)
End Function
Private Function rad2deg(ByVal rad As Double) As Double
Return (rad * 180 / pi)
End Function
-
May 24th, 2005, 09:43 AM
#7
Addicted Member
Re: distance between two zip codes
i made the same exact thing in access.
X: ((A lat-B lat)*69.05)^2
Y: ((A lon-B lon)*59)^2
D: ([X]+[Y])^0.5
Simple distance formula.
Just declare you variables and assign values to them.
-
May 25th, 2005, 12:51 PM
#8
Hyperactive Member
Re: distance between two zip codes
Cander, by George, I don't understand it, but that code seems to work very well. Thanks
I imagine that if there are Lat/Lon coords assigned somehow to zip codes, they probably locate the main post office that serves that zip.
DaveBo
"The wise man doesn't know all the answers, but he knows where to find them."
VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15
-
May 25th, 2005, 12:53 PM
#9
Re: distance between two zip codes
 Originally Posted by DaveBo
I imagine that if there are Lat/Lon coords assigned somehow to zip codes, they probably locate the main post office that serves that zip.
DaveBo
Thats a good possiblity.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|