## Approaches to search ZIP codes within given radius

There are several approaches to get zip codes within given radius from given zip:

1. Round circle by square if zips given by points;
2. Exact 3D geometry calculate if zips given by points;
3. Exact GIS calculate if zips given by polygons.

#### Approximation by square

We approximate circle by square on surface, where:

cos here is because longitudes distances differ depending on latitudes (say, in polar it is 0).

The databases for 1999 is an accessible open source, but up to date database costs money. Free database:
http://svn.baconbear.com/rails_plugins/acts_as_locateable/trunk/data/zip_code_data.csv

Open source database is about 42 000 records. Up to date is about 72 000. See, for instance:

#### Exact 3D calculations

3D coordinates:

Search points must be within sphere:

It is non-efficient table full-scan implementation.

#### Exact zip polygon databases

Here you have to install open-source PostGIS (http://postgis.refractions.net/) system on your PostgreSQL and buy zip codes polygon database.
After installing PostGIS you can query zip codes within given radius the next way (http://postgis.refractions.net/pipermail/postgis-users/2007-February/014760.html):

`SELECT z.zipcode FROM zipcode z, zipcode z2 WHERE z2.zipcode = '02109' AND expand(z2.the_geom, "whatever radius you want goes here but your spatial ref needs to be in that metric or you need to convert the radius") && z.the_geom AND distance(z.the_geom, z2.the_geom) <= "radius"`

Zip codes polygon databases:

Or, if you use RoR, you can use the following plugins:

As far as I understand they use Google API, so they have some restrictions.