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:

  1. latitude +- radius/EARTH_RADUIS
  2. longitude +- radius/(EARTH_RADUIS*cos(latitude))

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:

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


Exact 3D calculations

3D coordinates:

  1. x= EARTH_RADIUS*sin(latitude)*cos(longitude)
  2. y= EARTH_RADIUS*sin(latitude)*sin(longitude)
  3. z= EARTH_RADIUS*cos(latitude)

Search points must be within sphere:

x*x + y*y + z*z <= radius*radius

It is non-efficient table full-scan implementation.


Exact zip polygon databases

Here you have to install open-source PostGIS ( system on your PostgreSQL and buy zip codes polygon database.
After installing PostGIS you can query zip codes within given radius the next way (

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.


Author: Artem's Blog

Working on software and more...

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s