none
U.S. Zip Codes RRS feed

  • General discussion

  • To anyone interested in getting some basic information about U.S. Zip Codes*, I have compiled something that I think you’ll like:

    I have an assembly (a .dll file) that has the data and the methods all in one. The data itself is pretty basic but informative for what it is:

    In the screenshot shown above, the two properties which aren’t showing data (Latitude_String and Longitude_String) have an optional parameter for the number of decimal places to display in the returned string (defaulted to six (6)).

    The Lat/Lon data is primarily from Bing maps, but for those that it couldn’t find, I looked them up on the net – hopefully they’re correct.

    Note also that the property for county is shown as “Primary County” for a reason: Zip codes (in the U.S. anyway) aren’t geographically based at all, so a particular zip code may span counties (in fact, a zip code can span state boundary lines also, or that’s my understanding).

    I don’t have any documentation for this but it’s so simple that you shouldn’t really need any. It only does one thing but it does it pretty well:

    I think you’ll agree that finding a particular zip code entry from 41,695 entries* in less than two milliseconds is pretty fast. There’s an even faster way though: When you give it the zip code as an integer, it’s quite a lot faster as I’ll demonstrate forthwith.

    There are only two methods in this: GetZipCodeInfo and GetZipDictionary. The latter function – which returns a generic dictionary – is how it’s able to get the information so quickly. It’s a Dictionary(Of Integer, US_ZipCodeData).

    The former is overloaded; you can use a zip code as a five-character string or give it the integer value of it. If you give it a five-character string, it just converts that to an integer and hands it off to the overloaded one as an integer.

    Internally, the zip codes themselves are stored as Int32 values. Using that as the key for the generic dictionary is where it gets the impressive speed from. I used the following as a test and I ran it ten (10) times so that I could show you what you’ll expect when using it:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports ZC2017.Data
    
    Public Class Form1
        Private _zc As New US_ZipCodeData
        Private _rand As New Random
    
        Private randomZipCode As String
    
    
    
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
            Dim timeTest1 As TimeSpan = TestLoadTime()
            Dim timeTest2 As TimeSpan = GenerateRandomZip()
            Dim timeTest3 As TimeSpan = TestGetZipCodeData()
            Dim timeTest4 As TimeSpan = TestGetZipCodeData(CInt(randomZipCode))
    
            Dim sb As New System.Text.StringBuilder
    
            sb.AppendLine(String.Format("Load Time: {0:f1} Seconds", timeTest1.TotalSeconds))
            sb.AppendLine(String.Format("Get KVP: {0:f3} Milliseconds", timeTest2.TotalMilliseconds))
            sb.AppendLine(String.Format("Get Random Zip Code Data (String): {0:f3} Milliseconds", timeTest3.TotalMilliseconds))
            sb.AppendFormat("Get Random Zip Code Data (Integer): {0:f3} Milliseconds", timeTest4.TotalMilliseconds)
    
            MessageBox.Show(sb.ToString, "Results")
    
            Stop
    
        End Sub
    
    
    
        Private Function TestLoadTime() As TimeSpan
    
            Dim sw As New Stopwatch
            sw.Start()
    
            _zc.Load()
    
            sw.Stop()
    
            Return sw.Elapsed
    
        End Function
    
    
    
        Private Function GenerateRandomZip() As TimeSpan
    
            Dim sw As New Stopwatch
            sw.Start()
    
            Dim tempList As New List(Of Integer)
    
            For Each kvp As KeyValuePair(Of Integer, US_ZipCodeData) In _zc.GetZipDictionary
                tempList.Add(kvp.Key)
            Next
    
            Dim idx As Integer = _rand.Next(0, tempList.Count)
            randomZipCode = tempList(idx).ToString("00000")
    
            sw.Stop()
    
            Return sw.Elapsed
    
        End Function
    
    
    
        Private Function TestGetZipCodeData() As TimeSpan
    
            Dim sw As New Stopwatch
            sw.Start()
    
            Dim data As US_ZipCodeData = _zc.GetZipCodeInfo(randomZipCode)
    
            sw.Stop()
    
            Return sw.Elapsed
    
        End Function
    
    
    
        Private Function TestGetZipCodeData(ByVal zipCode As Integer) As TimeSpan
    
            Dim sw As New Stopwatch
            sw.Start()
    
            Dim data As US_ZipCodeData = _zc.GetZipCodeInfo(zipCode)
    
            sw.Stop()
    
            Return sw.Elapsed
    
        End Function
    End Class


    The results are shown here.

    The assembly is a little less than 2 megs and I have it uploaded here. It’s accurate as far as I know but obviously I make no guarantees about that. If you do find some errors though, please let me know.

    I hope you find it useful. :)

     

     

    *This zip code data is only for the conterminous states plus Alaska, Hawaii, and District of Columbia.

     


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Wednesday, January 10, 2018 2:33 PM ...updated image URL
    Sunday, September 24, 2017 6:21 PM