Getting all zip codes within an n mile radius

Claudiu picture Claudiu · Nov 29, 2008 · Viewed 7.3k times · Source

What's the best way to get a function like the following to work:

def getNearest(zipCode, miles):

That is, given a zipcode (07024) and a radius, return all zipcodes which are within that radius?

Answer

Patrick Harrington picture Patrick Harrington · Nov 29, 2008

There is a project on SourceForge that could assist with this:

http://sourceforge.net/projects/zips/

It gives you a database with zip codes and their latitude / longitude, as well as coding examples of how to calculate the distance between two sets of coordinates. There is probably a better way to do it, but you could have your function retrieve the zipcode and its coordinates, and then step through each zipcode in the list and add the zipcode to a list if it falls within the number of miles specified.