Wednesday, October 10, 2007

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 differs depending on latitudes (say, in polar it is 0).

The databases for 1999 is 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

23 comments:

Иван Немытченко said...

Дак оно же есть все уже реализованное на несколько раз внутри плагинов YM4R/GM и GeoKit, если я не ошибаюсь

Artem said...

Да, но насколько я понимаю, используется google API, соответсвтвенно, есть ряд ограничений :)

Krista said...

You write very well.

Anonymous said...

predictable noises compendium prediction hmso admirable wheeler alfragide medicolabs paarmann psivida
lolikneri havaqatsu

Anonymous said...

салют!
постинг dle %PP 8376

Anonymous said...

nizoral tinea versicolor :-[[[ 5841 zantac medication -) 1263 buy protonix :-( 4537

Anonymous said...

Привет! партнерские программы
-)) 4692

Anonymous said...

Buy Diflucan Online =DD

Anonymous said...

insomnia lexapro -)

Anonymous said...

Секс Запознанства :) 95172

Anonymous said...

Goldbaby Fatso Synths Vol.1 Multiformat (1 cd) 17459 -)

Anonymous said...

Alchemy Mindworks Graphic Workshop Professional 3.0a41 14796 =DD

Anonymous said...

belgia randki -) 84239

Anonymous said...

gumtree randki !) 98512

Anonymous said...

Набрел сайт в интеренете. Сразу после регистрации дают [b]халявные 100$[/b].
Я уже получил. Спешите и Вы http://www.bidbroker.org

Anonymous said...

виагра инструкция !)







виагра продажа 297481
821 виагра 100
действие виагры 6953

Anonymous said...

дженерик сиалис софт !)







сиалис побочные 159246
381 действие сиалиса
купить сиалис с доставкой 6213

Anonymous said...

randki na czacie %)







dziewczyny na randki 793658
619 randki w
randki za darmo 7634

Anonymous said...

однотрахники сколько стоит регистрация :(







сайт однотрахники 319854
483 однотрахники ру
порно однотрахники 6842

Anonymous said...

Октябрь 25 Октябрь 05 Lancia Daimler Limousine $70000 1993 Сентябрь11 Samui Amanda Resort Carbodies Taxi 4.2л., $77900, 2006 Chrysler Le Baron, 1998 г.в. Holden Zafira, 1990 Ikbal De Luxe Ватикан Hotel Garni Forsterheim Minelli TF 1800, 1995 Crystal Energy Октябрь 04 Октябрь 06 Rosy Hotel Geely Beauty Leopard 6.1л., $18200, 2007 г.в. Испания Corfu Residence 4* Tien Dat 5* Chrysler Pacifica 2.5л, $11500, 1998 monako-resort.info Honda Lagreat 6.5л, $84700, 2003 г.в. узнать

Anonymous said...

Вот новейшие киноленты что я нашла в сети из рабочих

Anonymous said...

Николай Басков нашел себе новую возлюбленную?! Подробности только у нас http://dailycomp.ru/page/14/

Anonymous said...

[url=http://god-n-devil-inc.livejournal.com/13959.html]Дизайн внутренних состояний[/url]