ZIP Code Utility

This article provides an easy method to lookup a U.S. City/State by ZIP Code, or one or more ZIP Codes by City/State. It also describes a method to calculate the distance between two ZIP Codes and find all other ZIP Codes within a radius of X miles of a specified ZIP Code.

Introduction

Intrigued by Ben Fry's zipdecode [^] applet, I decided to write a little ZIP Code utility that allows lookups of U.S. locations by ZIP Code, City/State, or all three. Since the data were already in the database in the form of latitude/longitude pairs, I added the capability to find the distance between two points, and to find all other ZIP Codes within a radius of X miles from the original location.

Background

Database

The MS Access database contains the following fields:

Field Name

Description

ZIP

The ZIP Code

LATITUDE

Latitude coordinate (decimal degrees)

LONGITUDE

Longitude coordinate (decimal degrees)

CITY

City name

STATE

State abbreviation

COUNTY

County name

ZIP_CLASS

ZIP Code class

ZIP Code — City/State lookups

The lookups are straightforward database queries using the OleDb* classes.

Distance calculation

ZIP Codes within a radius of X miles

Most ZIP Codes in the database contain latitude/longitude coordinates. To make the SQL query as simple as possible, I used a square of size 2Rx2R (where R is the radius of the circle) to encompass the search area as shown in the figure below.

This has the unfortunate side effect of searching an area ~22% larger than needed, but these "outliers" are filtered out of the result set on the client side before being returned to the calling application. I could have added a stored procedure to perform the distance calculation, but I didn't want to modify the database in any way. That way, if the author decides to update the data, (hopefully) all the users of this library will have to replace the old database file with the new one.

Now, using this approximation, the SQL query becomes as simple as this:

Limitations

This library relies on data from a free database that doesn't look like it has been updated since September 2001. I cannot vouch for the accuracy of this data. If you plan on using this in a production environment, you may want to invest in a commercial ZIP Codes database that is guaranteed by its maker and that is updated regularly.

To do List

Pending approval from the creator of the database, provide MS SQL and MySQL versions.

Thanks for this link. I found ZipcodeSoft was a very good solution for me when compared to other sites because it provides a free zipcode database and also sample programs in various languages. However, I am yet to receive the actual database from them, even after registering with them and purchasing the product. Do you work here or know someone who can help me get the database?

check this out. This solution contains a Stored Procedure that accepts a zip code and mile radius and returns all zip codes within that radius. (Example: Give me all the zip codes within a 10-mile radius of 50325). The Solution comes with and a dataload script that will create a SQL Server database, the Stored Procedure containing the algorithm, and load over 42,00 zip codes.

The database in the in the link is actually unavaliable on from the site that you have specified, the .mdb is not zipped up and is forbidded from being accessed by asp.net due to if file type. Does anyone have a local copy of the DB that they can upload?

I imagine you could. Why would you want to do that, though? You'd effectively be creating your own database system because you'd have to create your own internal file structure and write code to seek and sort the data. That's a lot of work to do, especially when all that functionality is provided by the database itself.

Plus, if you ever want to update the data, you'll have to recompile and redistribute the DLL.

If your hangup is that the database is an Access database, then you can easily convert it to another format -- say, MySQL or SQL Server. You'll want to get the original database creator's permission before you do that, of course.

Jon SagaraLook at him. He runs like a Welshman. Doesn't he run like a Welshman? Doesn't he? I think he runs like a Welshman.Sagara.org | Blog | My Articles

If your concern is the inefficiency of using a database call or if you are using the data a lot, you could consider using the Enterprise Library Caching Block to cache the city/state info. That way you could still have your information in a database AND have the performance of a locally stored dataset.