Noticed via Tara Calashain’s excellent ResearchBuzz, here’s a great site (maps.huge.info) that lets you view zip code outlines in Google Maps. It’s pretty addictive to type in zip codes (assuming you’re in the U.S., and you have the particular bent of mind that makes you enjoy stuff like Google Maps). For the lazy among you, here’s how it looks:
Notice that it’s doing something cool: after you’ve typed in the first zip code, you can type in more zip codes and it will draw multiple zip codes at once.
Suppose you wanted to do stuff like this yourself. First you’d want the Google Maps API. Next, you’d need a list of all the zip codes in the U.S. This is harder than it should be. From http://www.usps.com/ncsc/faq/:
Q: Where can I get a database or directory of all ZIP Codes and/or ZIP+4 codes along with the corresponding city, state, county, etc.? Is there an FTP site for downloading?
A: The information you are seeking is not available via download but is provided through our National Customer Support Center at (800) 238-3150 ….
[and then later] The Postal Service does not maintain any ZIP Code maps. The only related product we have at this time is the TIGER/ZIP+4 File.
And “TIGER/ZIP+4 File” is a link to a 404 page. Oy, thanks a lot! Grrr. Okay, so pop on over to the U.S. Census Bureau. For example, this page has a link to many good resources, including http://www.census.gov/tiger/tms/gazetteer/zips.txt which has a list of zip codes along with the city, state, latitude, and longitude of each zip code (the lat/lon is for the zip code center).
Technically, the Census Bureau calls their data Zip Code Tabulation Areas or ZCTAs. Why? As they put it,
This new entity was developed to overcome the difficulties in precisely defining the land area covered by each ZIP Code.
For example, zip codes represent postal routes and can’t always be represented with polygons. You can almost feel the contempt seething from the Census Bureau toward the U.S. Postal Service. I can believe that when Census and Postal personnel get in the same room, arguments break out and tempers flare. Sort of like Herbert Kornfeld, the Accounts Receivable Supervisor at The Onion that is always rumbling with Accounts Payable.
Okay, where were we? Postal Service: not helpful at all. Census Bureau: a great, easy-to-parse file. Okay, how about the boundary info with the shape of a zip code/ZCTA? How about this: http://www.census.gov/geo/www/cob/zt_metadata.html Oh, snap! Census Bureau 2, Postal Service 0!
Shall we delve a little deeper? Let’s do it; zip codes are fun. By the way, if you’re not a complete and utter nerd, or you don’t have hours to kill digging into files, the huge.info site sells a DVD with cleaner data in an easier format.
Okay, let’s examine the zip code for 94043 (where Google’s headquarters are located). Go to http://www.census.gov/geo/www/cob/z52000.html#ascii and save the file for California and decompress the zip file. The files are available in three formats, but we’re sticking with straight ASCII. Inside the zip file there are two files, a tiny one and a big one. In the tiny one, look for the zip code you’re interested in (94043 in this example):
This entry means that in the big file, the zip code 94043 is represented with an ID of 2465. If you look in the big file, you’ll find the data for that polygon in latitude/longitude format:
2465 -0.122065371904532E+03 0.374225573395062E+02
The first coordinate appears to be the center of the ZCTA, so chop off the first line and the END line and put the resulting lines in a file called 94043. Now fire up gnuplot and type these commands:
set terminal png
set output "94043.png"
set title "Zip code: 94043"
set xlabel "Longitude"
set ylabel "Latitude"
plot '94043' with lines
Alrighty, let’s see how we did. Here’s the zipcode on the original application:
And here’s our Gnuplot map:
There’s just too many fun things to do in the world. 🙂