locationgeographyzipcode

Getting all zip codes within an n mile radius


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?


Solution

  • 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.