Creating GeoRSS with Ruby and REXML (2011-03-28)

GeoRSS on Google Maps:
GeoRSS on Google Maps
-> Samurai zlash! - Google Maps

created GeoRSS file: 20110328_georss_sample.xml

source code:


#!/usr/bin/env ruby
$KCODE='u'
 
require 'time'
require "rexml/document"
 
def create_georss(channel, items)
 
  doc = REXML::Document.new()
  doc.add(REXML::XMLDecl.new(version="1.0", encoding="UTF-8"))
 
  # <rss>
  rss = REXML::Element.new("rss")
  rss.add_attributes({
    "version" => "2.0",
    "xmlns:georss" => "http://www.georss.org/georss",
    "xmlns:gml" => "http://www.opengis.net/gml",
    "xmlns:geo" => "http://www.w3.org/2003/01/geo/wgs84_pos#",
  })
  doc.add_element(rss)
 
  # <channel>
  c = REXML::Element.new("channel", rss)
  channel.each{|k,v|
    if k == '_imageurl'
      # logo image
      image = REXML::Element.new('image', c)
      REXML::Element.new('title', image).add_text(channel['title']) if channel['title']
      REXML::Element.new('link',  image).add_text(channel['link'])  if channel['link']
      REXML::Element.new('url',   image).add_text(channel['_imageurl'])
    else
      # RFC2822 format
      v = v.rfc2822() if v.kind_of?(Time)
      REXML::Element.new(k, c).add_text(v)
    end
  }
 
  # <item>
  items.each{|item|
    i = REXML::Element.new('item', c)
    item.each{|k,a|
      # ex. array of <category>
      a = [a] if a.kind_of?(Array) == false
      a.each{|v|
        if k == '_geo'
          # _geo is #{latitude},#{longitude}
          latlon = v.split(',')
          # 3 patterns : georss.org, GML, w3c
          # GeoRSS-Simple
          # xmlns:georss="http://www.georss.org/georss"
          REXML::Element.new('georss:point', i).add_text(latlon[0] + ' ' + latlon[1])
          # GeoRSS-GML
          # xmlns:gml="http://www.opengis.net/gml"
          where = REXML::Element.new('georss:where', i)
          point = REXML::Element.new('gml:Point', where)
          REXML::Element.new('gml:pos', point).add_text(latlon[0] + ' ' + latlon[1])
          # WGS84 Geo Positioning
          # xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
          REXML::Element.new('geo:lat',  i).add_text(latlon[0])
          REXML::Element.new('geo:long', i).add_text(latlon[1])
        else
          # RFC2822 format
          v = v.rfc2822() if v.kind_of?(Time)
          REXML::Element.new(k, i).add_text(v)
        end
      }
    }
  }
 
  str = ""
  #formatter = REXML::Formatters::Default.new
  formatter = REXML::Formatters::Pretty.new(2, true)
  formatter.write(doc.root, str)
  #doc.write(str, -1, true, true)
  #doc.write(str, 2)
  return str
end
 
# sample data
 
channel ={
  'title'         => 'Samurai zlash!',
  'link'          => 'http://www.nilab.info/zlash/',
  'description'   => 'Samurai Okojo.',
  'copyright'     => 'Copyright(c) 2000-2011 NI-Lab. All Right Reserved.',
  'lastBuildDate' => Time.now,
  '_imageurl'     => 'http://www.nilab.info/zlash/samurai_okojo.jpg', # logo image url
}
 
items = [
  {
    'title'       => 'I am hungry.',
    'link'        => 'http://www.nilab.info/zlash/hungry.html',
    'description' => '<html><body>I am hungry.</body></html>',
    'pubDate'     => 'Tue, 14 Dec 2010 00:07:23 +0900',
    'guid'        => 'http://www.nilab.info/zlash/hungry.html',
    'category'    => 'food',
  },
  {
    'title'       => 'Do GeoRSS Dream of Electric Sheep?',
    'link'        => 'http://www.nilab.info/zlash/newyork.html',
    'description' => '<html><body>I am sleepy... in New york.</body></html>',
    'pubDate'     => Time.local(2011, 1, 1, 11, 11, 11),
    'guid'        => 'http://www.nilab.info/zlash/newyork.html',
    'category'    => ['newyork', 'georss'],
    '_geo'        => '40.728340,-74.010360'
  },
  {
    'title'       => 'Great Lakes',
    'link'        => 'http://www.nilab.info/zlash/lakes.html',
    'description' => '<html><body>Lake Erie, Lake Huron, Lake Michigan, Lake Ontario, Lake Superior</body></html>',
    'pubDate'     => Time.local(2011, 2, 3, 23, 59, 59),
    'guid'        => 'http://www.nilab.info/zlash/lakes.html',
    'category'    => ['lakes', 'georss', 'water'],
    '_geo'        => '45.949069,-84.062850'
  },
]

run:


$ uname -mrsv
Linux 2.6.26-2-amd64 #1 SMP Tue Jan 25 05:59:43 UTC 2011 x86_64
 
$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]
 
$ date
Mon Mar 28 23:59:15 JST 2011
 
$ ruby ./ruby_rexml_georss.rb > 20110328_georss_sample.xml
Time.new.zone=JST
 
$ cat ./20110328_georss_sample.xml
<rss xmlns:georss='http://www.georss.org/georss' xmlns:gml='http://www.opengis.net/gml' xmlns:geo='http://www.w3.org/2003/01/geo/wgs84_pos#' version='2.0'>
  <channel>
    <title>
      Samurai zlash!
    </title>
    <copyright>
      Copyright(c) 2000-2011 NI-Lab. All Right Reserved.
    </copyright>
    <description>
      Samurai Okojo.
    </description>
    <link>
      http://www.nilab.info/zlash/
    </link>
    <image>
      <title>
        Samurai zlash!
      </title>
      <link>
        http://www.nilab.info/zlash/
      </link>
      <url>
        http://www.nilab.info/zlash/samurai_okojo.jpg
      </url>
    </image>
    <lastBuildDate>
      Mon, 28 Mar 2011 23:59:44 +0900
    </lastBuildDate>
    <item>
      <category>
        food
      </category>
      <guid>
        http://www.nilab.info/zlash/hungry.html
      </guid>
      <pubDate>
        Tue, 14 Dec 2010 00:07:23 +0900
      </pubDate>
      <title>
        I am hungry.
      </title>
      <description>
        &lt;html&gt;&lt;body&gt;I am hungry.&lt;/body&gt;&lt;/html&gt;
      </description>
      <link>
        http://www.nilab.info/zlash/hungry.html
      </link>
    </item>
    <item>
      <category>
        newyork
      </category>
      <category>
        georss
      </category>
      <guid>
        http://www.nilab.info/zlash/newyork.html
      </guid>
      <pubDate>
        Sat, 01 Jan 2011 11:11:11 +0900
      </pubDate>
      <title>
        Do GeoRSS Dream of Electric Sheep?
      </title>
      <description>
        &lt;html&gt;&lt;body&gt;I am sleepy... in New
        york.&lt;/body&gt;&lt;/html&gt;
      </description>
      <link>
        http://www.nilab.info/zlash/newyork.html
      </link>
      <georss:point>
        40.728340 -74.010360
      </georss:point>
      <georss:where>
        <gml:Point>
          <gml:pos>
            40.728340 -74.010360
          </gml:pos>
        </gml:Point>
      </georss:where>
      <geo:lat>
        40.728340
      </geo:lat>
      <geo:long>
        -74.010360
      </geo:long>
    </item>
    <item>
      <category>
        lakes
      </category>
      <category>
        georss
      </category>
      <category>
        water
      </category>
      <guid>
        http://www.nilab.info/zlash/lakes.html
      </guid>
      <pubDate>
        Thu, 03 Feb 2011 23:59:59 +0900
      </pubDate>
      <title>
        Great Lakes
      </title>
      <description>
        &lt;html&gt;&lt;body&gt;Lake Erie, Lake Huron, Lake Michigan, Lake
        Ontario, Lake Superior&lt;/body&gt;&lt;/html&gt;
      </description>
      <link>
        http://www.nilab.info/zlash/lakes.html
      </link>
      <georss:point>
        45.949069 -84.062850
      </georss:point>
      <georss:where>
        <gml:Point>
          <gml:pos>
            45.949069 -84.062850
          </gml:pos>
        </gml:Point>
      </georss:where>
      <geo:lat>
        45.949069
      </geo:lat>
      <geo:long>
        -84.062850
      </geo:long>
    </item>
  </channel>
</rss>
tags: ruby georss rss rexml

Posted by NI-Lab. (@nilab)