Many of us in the Labs found it thrilling to watch the Internet community unite around opposition to the SOPA and PIPA bills yesterday. Even more gratifying was seeing how many participating websites used our APIs to help visitors find their elected representatives. This kind of use is exactly why we built those tools, and why we’ll always make them freely available to anyone who wants to make government more accessible to its citizens.

Still, I’d be lying if I said we don’t occasionally wince when we see someone using our services in a less-than-ideal way. It’s completely understandable, mind you: the problem of figuring out who represents a given citizen is tougher than you might think. But we hate to think that anyone is getting bad information about which office to call — talking to the people who represent you should be simple and easy! Since this comes up with some frequency, it’s probably worth talking about the nature of these problems and how to avoid them.

TL;DR: Looking up congressional districts by zip code is inherently problematic. Our latitude/longitude-based API methods are much more accurate, and should be used whenever possible.

The first complication is probably obvious: zip codes and congressional districts aren’t the same thing. A zip code can span more than one district (or even more than one state!), so if you want to support zip lookups for your users, you’ll have to support cases where more than one matching district is returned. Our API accounts for this, but it’s important that your code do so, too. We err on the side of returning inclusive results when a zip might belong to multiple congressional districts.

Unfortunately, things are actually more complicated than that. Most people don’t realize it, but zip codes describe postal delivery routes — the actual routes that mail carriers travel — not geographically bounded areas. Zip codes are lines, in other words, while congressional districts are polygons. This means that mapping zips to congressional districts is an inherently imperfect process. The government uses something called a zip code tabulation area (ZCTA) to approximate the geographic footprint of a given zip as a polygon, and this is what we use to map zip codes to congressional districts. But it really is just an approximation — it’s far from perfect.

It’s much better to skip the zip code step entirely and simply look up your location against the congressional district shapefiles published by the Census Bureau using a precise geographic coordinate pair instead of a hazy, vague zip code. Thanks to the Chicago Tribune News App Team’s excellent Boundary Service project, we offer exactly this capability. If you can, we strongly encourage you to get a precise latitude/longitude pair from your users (either by geolocating them or geocoding their full address), then use it to determine their representatives.

“But what about house.gov’s ZIP+4 congressional lookup tool?” I hear you asking. It’s true, many House offices use this tool to determine who your representative is (and whether you’re allowed to email them). Unfortunately, just because this tool is on an official site doesn’t mean it’s perfect. Here in the Labs, Kaitlin (who lives in Maryland) can’t write her representative because the ZIP+4 tool gives incorrect results. Besides, not that many people know their full nine-digit ZIP+4 code.

So if you can, use latitude/longitude pairs. If you can’t, and have to depend on zips, we’ll supply results that are very, very good — but not as good as real coordinates would allow.