How do I implement a simple geographic information database with zip/postal code calculations

Hi,

Sorry that this topic is not really rails-specific, but I happen to be implementing it in rails. I'm trying to implement a common scenario, where you get a comprehensive list of states/provinces, cities, and zip/postal codes, and you find nearby cities based on a specified location, like on some of the dating sites. I have no clue where to start, I'm not even sure what keywords I should use to search. If anyone can point out to me how this is usually done, I would really appreciate it.

Thanks!

Bob

Sorry that this topic is not really rails-specific, but I happen to be implementing it in rails. I'm trying to implement a common scenario, where you get a comprehensive list of states/provinces, cities, and zip/postal codes, and you find nearby cities based on a specified location, like on some of the dating sites. I have no clue where to start, I'm not even sure what keywords I should use to search. If anyone can point out to me how this is usually done, I would really appreciate it.

Start here:

Get one of the packages that includes lat/long coordinates. Then just do earth-distance calculations and you're done.

We use that to drive this: http://www.cardplayer.com/poker_room

-philip

Thanks for the recommendation. I was wondering if there are any free ones available that don't do as much. I just need to let the user select a city or zip/postal, and return a list of cities within say 50/100/200 miles. There's no need to be too specific. I am just a student writing this app from my bedroom, so I try not to spend too much. :slight_smile:

Thanks!

Bob

Purely out of curiosity, I decided to investigate this myself, and I found http://www.cfdynamics.com/cfdynamics/zipbase/index.cfm . It’s free, and has lat/long coordinates and cities. It appears to be from 2001, but it may be accurate enough for your needs.

doppler

I haven't looked into this at all, but does the Google Maps API have any calls that would return the exact same information? If so, then just make a call out to their service.

Yes and no. You can get the lat/long for a given address/city/zip, but you can't do things like figure out the state given the zip, etc which is what this guy wants to do...

Also, they have a limit on the number of queries/day, but if you can cache it on insert that might work too.

I just run a quick query below and it finds the state for given zipcode.

Sweet! Guess I need to relook at it :slight_smile:

http://zipcodesearch.rubyforge.org/

Problem solved.

Just gave a talk on this at our local ruby users group last night. (ncl.rb).

Useful plugin is GeoKit ( http://geokit.rubyforge.org/ ) which allows you to geocode addresses using a variety of sources (Google, Yahoo etc) works great if you are in the US or Canada. For UK Codes it becomes a problem due to copyright of the postcode database. I have hacked together an extra geocoder ( http://magpieuk.blogspot.com ) which plugs in to GeoKit which utilises a server I found which offers this for free ( http://emad.fano.us/blog/?p=277 ).

GeoKit gives you the acts_as_mappable which once you add lat and lng fields to it allows you to do nice things such as:

object1.distance_to(object2)

origin = MultiGeocoder.geocode("NE1 4ST")

nearlocs = Model.find(:all, :origin => origin, :order => "distance ASC"

If you need to integrate with Google / Yahoo maps have a look at YM4R ( thepochisuperstarmegashow.com )

Hope this helps

Regards

Lee