Suffix

Published by Simon Schoeters

GeoRSS with Ruby on Rails

I keep the latitude and longitude for each of these blog post (as you can see in the right hand column). My blog's RSS feed is generated from my posts table. Until now it was a plain simple RSS feed but why not combine the 2 and enhance my RSS feed with the coordinates? They are there anyway and GeoRSS is widely adopted these days.

What is GeoRSS?

GeoRSS is a way to encode location information in RSS feeds. This can be as simple as adding one small XML element to your existing feed. GeoRSS feeds are designed to be consumed by geographic software such as map generators and by doing so your feed can be used in new and different ways: pinpointing on a may, finding other posts close to this one, etc.

What we need

Generating the actual RSS feed is not in the scope of this article, we'll only add location awareness to our existing feed. Please head over to Larry Myers' How To Generate RSS Feeds with Rails article if you don't generate your feed yet.

We'll need to add one small XML element to each item in our feed with the latitude and longitude for the post:

<georss:point>50.8668 4.69714</georss:point>

In Ruby on Rails this looks like the following (where latitude and longitude are 2 fields in our posts table). The syntax looks a little strange as we need a way to add the ‘georss:point’ element and Ruby doesn't like the colon in there (it indicates a symbol).

xml.georss :point do
  xml.text! post.latitude.to_s  + ' ' + post.longitude.to_s
end

Great, now we only need to add the GeoRSS namespace at the top of our feed to tell the feed parsers what we mean.

xml.rss (:version => "2.0", "xmlns:georss" => "http://www.georss.org/georss")

My location aware RSS feed

The full view template should now resembles something like this:

xml.instruct! :xml, :version=>"2.0"
xml.rss (:version => "2.0", "xmlns:georss" => "http://www.georss.org/georss") {
  xml.channel {
    xml.title("My GeoRSS feed")
    xml.link("http://www.example.com")
    xml.description("My posts enhanced with location info")
    xml.language('en-us')
    for post in @posts
      xml.item do
        xml.title(post.title)
        xml.description(post.content)
        xml.author("none@none.be (#{post.author})")
        xml.pubDate(post.created_at.strftime("%a, %d %b %Y %H:%M:%S %z"))
        xml.link( post_permalink_url(post.permalink) )
        xml.guid( post_permalink_url(post.permalink) )
        xml.georss :point do
          xml.text! post.latitude.to_s  + ' ' + post.longitude.to_s
        end
      end
    end
    }
}

That's it. Not too exciting at first but try your RSS feed URL in Google Maps and see how easy it is to pinpoint each location on a map (here is mine).