idisposable.net: a blog about web 2.0, search, collaboration, Ruby on Rails, Microsoft, Google, and other fun stuff

Mapping and Location Mashups in Rails: GeoKit and Geonames

If you are building geography based mashups in Rails, or just need some simple calculations for distance, be sure to check out GeoKit plugin and Geonames API Gem. They will save you hours of time.


What is GeoKit?

“Geokit is a Rails plugin for building location-based apps. It provides geocoding, location finders, and distance calculation in one cohesive package. If you have any tables with latitude/longitude columns in your database, or if you every wanted to easily query for “all the stores within a 50 mile radius,” then GeoKit is for you”

It is fantastic. Here is some sample code:

include GeoKit::Geocoders
   def get_location( location )
      loc = MultiGeocoder.geocode( location )   # ask GeoKit to find your city
      return loc
    end
 
    def calculate
     start_city = get_location("Hoboken NJ")
     end_city = get_location("Los Angeles CA")
     distance = start_city.distance_to( dest )
     #thats it!  really?? yes.
    end

What is Geonames

Geonames is a public location database / web service API that allows you to search and reverse lookup geography and location information, such as city names and towns, longitude/latitude, etc.

The excellent Geonames Gem for Ruby provides an easy to use interface into the geonames web service.

Why use geonames? I’ve found it extremely useful for finding “nearby” locations as well as cities/counties/subdivisions within another area. For example, look at the following call:

http://ws.geonames.org/search?q=NJ&maxRows=20

Viola! All the cities and location features in NJ. The real power in geonames (which you don’t necessarily get with geokit) is the ability to restrict searches based on FCL and FCODE designators.

For example, some locations from geonames might these elements attached to them:

<fcl>P</fcl><fcode>PPL</fcode>

What do these codes mean?

FCL and FCODE represent “feature codes.” A complete list of feature codes can be found here.

You can do neat things with feature codes. Witness:

“Geonames?”Yes, Ed

“New Jersey has over 127 miles of beachfront and another 83 miles of bayshore. Where are all of these beaches??

Ed, I am not a person, just an XML web service. You’ll have to ask me a different way.

“Ok, how about this: http://ws.geonames.org/search?q=NJ&featureClass=T&featureCode=BCH

Yumm.

More geonames; simple latitude / longitude lookup:
http://ws.geonames.org/postalCodeSearch?postalcode=10012&country=US

gives us the longitude and latitude for a zip code in Manhattan.

It is a great tool when you don’t want to have a local database with geography information. Put memcached in front of the calls, and you’ll have a zippy, small-footprint mashup in no time.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*
ss_blog_claim=77c0780e64c123f107896646c0ee8870