Stop using zip codes for geospatial analysis (2019)
133 comments
·February 7, 2025jihadjihad
NelsonMinar
That's not the important problem and there's a simple solution with ZCTAs.
The big problem is zip codes are defined in terms of convenient postal routes and aren't suitable for most geospatial analysis. Census units, as the article explains, are a much better choice.
BiteCode_dev
You ask a customer their census unit on purchase though.
mcphage
> The consequence is that performing any analysis with an assumption that ZIP codes are polygons is bound to be error-prone.
Yeah, but any analysis you're likely to perform is approximate enough that the fact that ZIP codes aren't polygons is basically a rounding error.
Plus, it's a lot easier to get ZIP codes, and they're more reliably correct, so you might still get better results, than you would going with another indicator that is either (a) less reliable or (b) less available.
mattforrest
They aren’t reliable correct actually. The boundaries that the Census publishes are called Zip Code Tabulation Areas which are approximations of zip codes and include overlaps.
wombatpm
ZCTA5 roughly corresponds to the area of a 5 digit zip code. Problem is there are large areas of the west that don’t have permanent residents and no mail delivery. Plus they change over time.
mholt
Yeah. ZIP codes are sets in the abstract-dimensional space of carrier delivery points. I suppose you could think of them as lines, but definitely not polygons.
cogman10
Zip codes (in the US) are machine readable numbers a mail sorter can use to send a parcel to the right delivery truck for final delivery. In the US, they represent the hierarchy of postal centers with the most significant digit representing the primary hub for a region and the smallest number the actual post office that will be in charge of delivering the letter (or truck if you do the extended post code).
They don't represent geography at all, they represent the organizational structure of USPS.
They work by making the address on a letter almost meaningless. For some smaller population zip codes you can practically just put the name and zip code down and achieve delivery.
mywittyname
> For some smaller population zip codes you can practically just put the name and zip code down and achieve delivery.
A 5+4 formatted ZIP code maps to just a handful of addresses. In cities with larger populations, the +4 could map to a single building, and in more sparely populated place, it might include houses on a handful of roads.
For smaller datasets, ZIP+4 might as well be a unique household identifier. I just checked a 10 million address database and 60% of entries had a unique ZIP+4, so one other bit of PII would be enough to be a 99.99% unique identifier per person.
With a geo-coded ZIP+4 database, you could locate people with a precision that's proportional to the population density of their region.
Spivak
Right but this ends up being a good approximation for geography because the reality of logistics is that you end up doing a cute n-ary search of the geography. When you know the regional hub you can say for certain a huge chunk of the US the zip code doesn't represent. And then you keep n-secting. Sometimes the land-mass you get at the end is specific enough for your uses.
You're not going to wind up with a situation where zip codes with the same regional marker end up on different coasts.
alsodumb
I agree that they weren't explicitly meant to represent geography, but implicitly they do, right? Are there cases where this is violated?
In other words, is it safe to assume that for entity in a zip code is less than x distance away from the closest entity in the same zip code?
mattforrest
Well put
jonas21
ZIP codes are an emergent property of the mail delivery system. While the author might consider this a bad thing, this makes them "good enough" on multiple axes in practice. They tend to be:
- Well-known (everybody knows their zip code)
- Easily extracted (they're part of every address, no geocoding required)
- Uniform-enough (not perfect, but in most cases close)
- Granular-enough
- Contiguous-enough by travel time
Notably, the alternatives the author proposes all fail on one or more of these:
- Census units: almost nobody knows what census tract they live in, and it can be non-trivial to map from address to tract
- Spatial cells: uneven distribution of population, and arbitrary division of space (boundaries pass right through buildings), and definitely nobody knows what S2 or H3 cell they live in.
- Address: this option doesn't even make sense. Yes, you can geocode addresses, but you still need to aggregate by something.
ericrallen
This is a tangent, but addresses are also way more complicated than most people realize - especially if you’re relying on a user to input a correct address or if you need to support multiple countries, somewhere with unique addresses like Queens[0], or you need to differentiate between units of a specific street address that uses something other than unit numbers for a unit designation.
At that point you need something like Smarty[1] to validate and parse addresses.
[0]: https://stackoverflow.com/questions/2783155/how-to-distingui...
ghaff
Just last week I had to deal with the fact that my house has the wrong address in multiple databases because things changed when an interstate went in 40-something years ago. It's not a big change--main st. vs N main st. but it was enough to mess up various things. Not as much as when I moved in 30 years ago but still enough to be wrong in old town and telco records. Took me a couple of days to get a permit issued to get electrical hooked back up after a fire as a result because apparently some town clerk insisted the address wasn't valid.
jwnacnud
Here is a little-known (but very useful piece of information).
The US Postal Services has a team of people that handle address updates. This team is localized to different regions so that they generally are aware of local nuances. If you need to talk to the USPS about getting an address issue resolved simply go to this USPS AMS site and enter your zipcode to find the team that handles addresses in that area:
https://postalpro.usps.com/ppro-tools/address-management-sys...
If they don't answer, leave a message. They have helped me thousands of times in my last 14 years working with address validations.
rented_mule
An annoyance for me is that I've yet to see any address validator get my current home address right. They all insist my address is on the road that leads to my road rather than my actual road. It's understandable that they can't be 100% accurate given the scale / complexity of addresses.
Most sites/apps will let me override the validator, but a few won't. The most common ones that insist on using the wrong address are financial institutions that say the law requires them to have my proper physical address and therefore they go with the (incorrectly) validated version.
USPS does not do home delivery in our area, and UPS/FedEx/etc. usually figure it out given that street numbers alone uniquely identify properties in our town.
killjoywashere
Same! My wife ran a business from home during the pandemic and we actually went through the effort to work with Google Maps (they called us) to get it on the map. And of course USPS has no problem. But our address was originally a federal building with a letter, still only has a letter, no number, and there are now all sorts of work-arounds floating around on how resolve addresses in our neighborhood. What's wild is the Post Office is literally down the street from our house, and our house predates the founding of most of the big delivery services, which all manage to deliver to us, given their preferred incantation. If I can't get the shipper to pass the right incantation to their shipping service, shenanigans ensue. My (least?) favorite was an item that went across the Pacific Ocean 3 times over the course of 3 months.
jonathanoliver
Send your address to support@smarty.com and link to this HN thread. I’ll keep an eye watching out for it. I’d love to see what our system does with your address.
We have non-postal addresses and a lot of other mechanisms to help here. We also have contacts at the USPS and others to help fix addresses.
bob1029
Addresses are a huge ordeal in banking. Easily one of the most tortured domain types when it comes to edge cases and integration pain.
Every customer I've worked with insisted on having all addresses ran through the USPS verification API so they could get their bulk mailing discounts.
Even if you get the delivery/cost side under control, you still have to make sure you are talking about the right address from a logical perspective. Mailing, physical, seasonal, etc. address types add a whole extra dimension of fun.
nitwit005
Yes, unfortunately, their assertion that everyone knows their zip code is wrong. People often write a neighboring code, and the post office just delivers it.
Similar issues for city name, of course.
steezeburger
This sounds like the person doesn't know the receiver's zip code. Why are you extending that to not knowing their own zip code? Are they mailing something to themselves?
VWWHFSfQ
Very common in NYC. People will use all of "New York, NY", "Queens, NY", or "Astoria, NY" all interchangeably and the post office will still just deliver it to the same place.
paulddraper
They know their ZIP code far, far better than any other plausible geographic cell.
jonathanoliver
Thanks for the shout-out. Founder of Smarty here.
Regarding article, it really depends on the use case of whether to use ZIP Code (TM), postal code, Canada Post Forward Sortation Area, lat/lon, Census Bureau block and tract, etc.
As has been noted, the ZIP Code is often good enough for aggregating data together and can be a good first step if you don’t know where to start.
ellisv
There are point process models, but, yes, its much more common to want to aggregate to a spatial area.
Another consideration is what kind of reference information is available at different spatial units. There are plenty of Census Bureau data available by ZCTA but some data may only be available at other aggregate units. Zip Codes are often used as political boundaries.
I'd also mention the "best" areal unit depends on the data. There is a well known phenomenon called the modifiable areal unit problem in which spatial effects appear and vanish at different spatial resolutions. It can sort of be thought of as a spatial variation of the ecological fallacy.
raphman
One more advantage: ZIP codes are a good trade-off if you want to gather anonymous data in a survey or provide anonymized data to an outside entity. For example, we recently conducted a survey on mobility patterns within our university. To offer respondents a reasonable amount of anonymity, we just asked for their (German) ZIP code and the location of their primary workplace. This allows us to determine the distance and approximate route people would take between home and university campus - to a degree that is sufficient for our goals.
mattforrest
Well you hit on all the points that discuss the compromises that zip codes offer. Just because you have them in your data doesn't mean that they can produce anything useful. You are correct that no one knows their census unit is (if you are thinking from someone entering this on a website) but collecting location or address will be a lot better.
Fact is a lot of web data contains a zip but if you can collect something better it will usually render better results. Unless you are analyzing shipments then that is fine.
JumpCrisscross
Would add that there are network effects with zip code data. If you collect H2 data, you have fewer sources with which to join.
walrus01
In terms of "good enough", a Canadian postal code, broadly equivalent to a zip code, is much more granular and can often identify an individual apartment building, or single city block. Plenty of large office buildings in major Canadian cities also have their own postal code.
The functionality of it is closer to the "Zip+4" with extension used to have a more granular routing of physical mail for USPS.
https://www.canadapost-postescanada.ca/cpc/en/support/articl...
ssl-3
Sure, and in the States, ZIP+4 could once nail my postal location to a subset of 4 (of a group of 16) mailboxes within a particular set of entry doors on a particular apartment building.
But broadly speaking, nobody knows what their ZIP+4 is, while I imagine that most people in Canada know their postal code by heart.
It is interesting.
bluGill
The plus four changes all the time so it isn't feasable to know it. The use is large mailers can get a discount by looking it up and presorting mail. If the mail coming into my post office has my mail next to my next door neighbors that saves them a lot of time.
throw0101c
> In terms of "good enough", a Canadian postal code, broadly equivalent to a zip code, is much more granular and can often identify an individual apartment building, or single city block.
To the point that StatCan and other agencies have rules on the number of characters that are collected/disseminated with other data to make sure it's not too identifying:
* https://www.canada.ca/en/government/system/digital-governmen...
* https://www12.statcan.gc.ca/nhs-enm/2011/ref/DQ-QD/guide_2-e...
mattforrest
Yeah but Zip+4 represent a collection of houses not a polygon so not useful for aggregations or statistical work
michaelmrose
If you are worrying about address at all instead of tax or legal jurisdiction its probable that you as a business have a physical presence. You can probably correlate better by predicting which location a given address would likely interact with if you don't know already by prior purchases/interaction which they normally do so. I would suggest actual purchase data followed by travel time.
Zip and distance as the crow flies often gives shit data. My zip suggests I'm off in bum fuck and since I'm on the puget sound things that are relatively near as the crow flies can actually be hours away.
stevage
> Easily extracted (they're part of every address, no geocoding required)
That's only true if you can also access the spatial boundaries of the zipcodes themselves.
In Australia, this turns out not to be true: the postal system considers their boundaries to be commercial confidential information and doesn't share them. The best we can do is the Australian Bureau of Statistics' approximations of them, which they dub "postal areas".
jpjoi
Zip codes are just weird to use for anything other than mail in general because they’re set up based off infrastructure.
CGP Grey has a great video on this: https://m.youtube.com/watch?v=1K5oDtVAYzk
diggan
I've noticed more and more super/hypermarkets started asking for your zip/postal code sometime during self-checkout. I'm guessing they use these as approximations about where people travel from, so they can evaluate if to open more stores closer to popular areas, or something like that. Pretty sure there is more use cases for postal codes too.
kjellsbells
Postcodes are very useful (but not perfect) proxies for household socioeconomic status, which is useful for marketing and sales analysis.
That data linked with the payment method that the register collects pretty much gives the store exactly who you are and where you live even if you chose not to sign up to the store's loyalty program.
paraboli
They use bulk mail to send out flyers, coupons, and can use zip codes to AB test these.
Spivak
Wait until you find out that this is the same way phones used to work. The number was the row/colum for the operator needed to plug your line into.
throw0101c
CGP Grey recently posted a video on Zip codes, "The Hidden Pattern in Post Codes":
Cthulhu_
That's what I was thinking of earlier, the succinct version is "your address is where mail needs to go, the zip code is how to get it there". Or in other words, the zip code is the address(es) of the sorting centers and post offices to the destination.
hammock
Great article. Zip codes can be super expedient. But you have to be self aware that for many uses cases they function WORSE than a random grid. Because they have built-in aggregation of a central post office(and surrounding) with a certain radius of rural/less dense surrounding.
So for example, if you are sorting “rural zips” vs “urban zips” it will only take you so far, and may actually be harmful.
Same goes with MSAs/DMAs (media markets). These have to be used for buying media, but for geospatial analysis they are suboptimal for the same reasons.
Easiest way to dip your toe into the water of something better is to start with A-D census counties.
mattforrest
Funny to see this one pop up today (I wrote this one way back when) but I just refreshed it into a video on my channel: https://www.youtube.com/watch?v=x-opv4REEic
funkaster
If you want to learn a bit more, there was a recent, really good Planet Money episode[1] about this exact same topic. They focus on the problems that you might face when using zip code for demographic analysis.
[1]: https://www.npr.org/2025/01/08/1223466587/zip-code-history
serjester
H3 is awesome here! What I don't think many people realize is that H3 cells and normal geographic data (like zips) are not mutually exclusive. You can take zip outlines, and find all the h3 cells within them and allocate your metric accordingly (population, income, etc).
This makes joining disparate data sources quite easy. And this also lets you do all sorts of cool stuff like aggregations, smoothing, flow modeling, etc.
We do some geospatial stuff and I wrote a polars plugin to help with this a while back [1].
hammock
What is the benefit of H3 vs a rectangular grid?
kylebarron
Equal distances to each adjacent neighbor: https://www.uber.com/blog/h3/
ajfriend
They also only have one type of neighbor. Square grids have 2 neighbor types. Triangular grids have 3.
mannyv
Zip codes, zctas, and tiger/line are good enough for what most people need. Maybe you can find an edge by using something more granular...but I'm not sure what edge you'd be looking to get with geodata. Maybe for real estate trends and/or market analysis?
clutchdude
I agree.
Reading their alternatives, it strikes me with "ZCTA's are the worst form of small area aggregation except for all others."
Its not a great geography to use but it is quite useful if you know it's limitations and inaccuracies when you get into it. Stuff like multipolygon entities, island-polys, etc aren't fun to resolve but can be accounted for.
Add on that ZCTA's will historically follow some sort of actual boundary(rivers/highways/etc) they can tell a story in a way Census tracts can't.
temporallobe
I don’t really see the problem but at the same time I understand it’s not a perfect solution. I used to do geospatial work using ESRI products and Zip codes polygons are very useful because people use are often interested in things inside of zip codes especially partial 3-4 digit zip code area, but they are occasionally non-contiguous so you can end up with strange results, visually and mathematically (for example how do you find the “center” of a non-contiguous shape? You don’t).
Edit: I wanted to point out that I recall that ESRI maps used to come “out of the box” with zip code polygon layers. While I agree they are technically not polygons in the strictest sense, they often are or they are fully closed shapes or close enough to it - and even if they are missing a few nodes to make a complete polygon, whoever did the digitizing probably manually closed the loop so to speak. Remember, geospatial maps are used for many different purposes, likely none of them having anything to do with postal routes, so in that sense they are “good enough” for most purposes.
SOLAR_FIELDS
There actually are algorithms that compute the centroid of multi part polygons. So you can in fact find the center of a non-contiguous shape. Now, whether that centroid actually has value in a real world application, I’m not sure.
Anon84
This is an example of the well known Modifiable Areal Unit problem: https://en.wikipedia.org/wiki/Modifiable_areal_unit_problem In general, your statistics depend on how you define your areas and you will get different pictures with different definitions.
paulddraper
People use ZIP codes because they have ZIP codes.
No one has census blocks.
And coordinates can work but lack some inherent advantages, such as human readability and a semblance of pop density normalization.
To put it in plain mathematical language, ZIP codes are not defined as polygons [0]. The consequence is that performing any analysis with an assumption that ZIP codes are polygons is bound to be error-prone.
0: https://manifold.net/doc/mfd8/zip_codes_are_not_areas.htm