Results 1 to 9 of 9

Thread: distance between two zip codes

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Location
    New York City
    Posts
    69

    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

  2. #2
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    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.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  3. #3
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    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

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  5. #5
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    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

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    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
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7
    Addicted Member
    Join Date
    May 2005
    Posts
    162

    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.

  8. #8
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    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

  9. #9
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Re: distance between two zip codes

    Quote 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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width