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:
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:

  1. http://www.zip-codes.com/
  2. http://www.zip-code-database.org/?
  3. http://www.google.com/search?q=zip+codes+database?

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.

See http://en.wikipedia.org/wiki/Spherical_coordinate_system

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:

  1. http://maps.huge.info/blog/2007/04/zip_code_polygon_databases.html
  2. http://www.google.com/search?q=zip+codes+poligon+database

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.

Advertisement

Author: Artem's Blog

Working on software and more...

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s

%d bloggers like this: