Spatialite with ruby

Posted by Kashif on July 05, 2009

I would like to detail the process of getting spatialite, the geospatial extension to sqlite3, working with ruby. Its a bit involved so I though others might find it useful.

To begin with one needs to compile sqlite3 with the R*Tree module turned on. By default this is not the case and on my Linux gentoo box I needed to change the sqlite3 ebuild as follows:

Once sqlite3 is installed, we need to install spatialite. On gentoo I created the following ebuild:

Once spatialite is installed we need to add a method to the sqlite3-ruby gem to enable the loading and unloading of extensions. By default this is not allowed due to security reasons, but one needs it to load the libspatialite extension. I have forked Jamis’ repo and pushed the changes to my branch here on github. Feel free to merge or pull from me.

Once this version of sqlite3-ruby is installed, we can use it in ruby as follows:

First Canberra Ruby Crew (CRC) meetup

Posted by Shoaib on January 21, 2008

I suppose the Canberra Rails Crew meet up has officially begun even though unofficially Hugh, Tim and myself met up at the Front December 2007. This week Hugh & Chris couldn’t make it but we had the right mix of food, chatting, hac-king and Guitar Hero (thank you Allan). While hacking I was badly suffering from Mac-envy but it won’t last long as I will soon own a MacBook. I learned a lot from the guys especially about migrating to Rails 2.0 something that’s coming in handy as we update GeoRuby for Rails 2.0. Thank you for making it a worthwhile evening guys. Thank you Ian (my house mate), your hospitality earns you the “honorary geek” for the evening. Next meet up Feb the 15th. If you would like to focus on a particular topic for the evening feel free to suggest ideas in comments.

I can get the ball rolling by picking up the theme of benefit of Rails in the Enterprise and Government and see if we can get some traction by holding a public tutorial on Intro to Ruby/Rails.

Getting Google Earth & rails or merb talking

Posted by Shoaib on January 09, 2008

In this post I’ll demonstrate how your rails application can be made to talk to Google Earth. As a simple example, our rails app will put a cross hair in the center of the Google Earth window, every time the camera moves.

create a rails app rails ge_01

add a new mimetype for kml in config/initializers/mime_types.rb

Mime::Type.register "application/vnd.google-earth.kml+xml", :kml

create a lesson_one controller & index action/view; you may use the controller generator

setup database (sqlite3 will do)

create a google earth network link as a kml this can be done in a number of ways: you can create a kml file by hand or create a builder template to generate the kml

create network_link action which can be given to your clients

create a builder template (network_link.kml.builder)

server = url_for :controller => 'lesson_one',  :only_path => false
xml.instruct!
xml.kml(:xmlns => "http://earth.google.com/kml/2.2") {
xml.Document {
  xml.name("Pass parameters to my Rails app")
  xml.open(1)
  xml.visible(1)
  xml.NetworkLink {
    xml.name("My rails app  being passed parameters")
    xml.open(1)
    xml.visibility(0)
    xml.Link {
      xml.href("#{server}")
      xml.viewRefreshMode("onStop")
      xml.viewRefreshTime(0.5)
      xml.viewFormat("BBOX=[bboxWest],[bboxSouth],[bboxEast],[bboxNorth]&CENTRE=[lookatLon],[lookatLat]")
      }
    }
  }
}

edit the index action in the lesson_one controller to capture the parameters from Google Earth in instance variables holding the Longitude and Latitude values for the centre

create an index.kml.builder template to respond to the request from Google Earth. Here we’ll use instance variables to create a marker for that point on the map.

text =    "
Centre Lng: #{@centre[0]}
Centre Lat: #{@centre[1]}
X Min: #{@bbox[0]}
Y Min: #{@bbox[1]}
X Max: #{@bbox[2]}
Y Max: #{@bbox[3]}
"

xml.instruct! :xml
xml.kml(:xmlns => "http://earth.google.com/kml/2.2") do
  xml.Document {
    xml.Placemark {
      xml.Snippet(:maxLines => "9") {
        xml.cdata!(text)
      }
      xml.name("cross-hair")
      xml.Style {
        xml.LabelStyle {
          xml.scale(0)
        }
        xml.IconStyle {
          xml.color("ffefebde")
          xml.Icon {
            xml.href("root://icons/palette-3.png")
            xml.x(128)
            xml.y(32)
            xml.w(32)
            xml.h(32)
          }
        }
      }
      xml.Point {
        xml.coordinates("#{@centre[0]}, #{@centre[1]}");
      }
    }
  }
end

All this works pretty much unchanged in merb. One only needs to register the kml mime type by adding this to config/merb_init.rb:

Merb.add_mime_type(:kml, :to_kml, %w[application/vnd.google-earth.kml+xml], :Encoding => "UTF-8")

Time permitting I’ll try to make a screen-cast of this.
ge_rails

“Angelo, why is that angel mooning god?” Dave Thomas’s Keynote

Posted by Shoaib on September 18, 2007

Keynote Dave Thomas:

Today’s keynote was by Dave Thomas who as David. A. Black said needs no introduction. That talk was titled “Art and Software Engineering” and contained gems of wisdom. It successfully drew analogies between art/artists & poets and software/programmers and had some hilarious metaphors from the art-world. Stories about being “off-by-one robotic cameras”, “poets who dislike people” and “angels mooning god”. Dave showed that these art-metaphors applied equally to the programming-world and that programmers should be learning from them. So what did I get out of the talk? I will try to summarize some of the talk in dot-point

  • Know who this man is (or live in shame)
  • Read his book (again if you haven’t recently)
  • Software development is a creative process (like the creative arts), since software development unlike other engineering activities is not constrained by the physical medium
  • Just as authors overcome writer’s block by beginning the writing process by quickly putting down thoughts on paper and throwing them away if they are unhappy with them, as programmers we should be experimenting, creating prototypes and tracer-bullets and be throwing some these away as we home-in on what we are happy with
  • Artists when working on large/complex paintings divide up the panting into smaller self contained paintings (e.g. Sistine Chapel Ceiling) with minimal coupling. Here Dave gave the hysterical analogy of a client wanting to change part of Sistine Chapel because they were unhappy with the portrayal of an “angel mooning god”.
  • There is a difference between portraits and pictures. Portraits require a deeper knowledge of the subject and artists who draw them need to understand their subject at a much deeper level than someone just drawing a mediocre picture. Similarly the programmer needs to understand their clients underlying needs. Here Dave gave the example of NASA and the pen manufacturer – where the product designers understood the needs of the client better than the client themselves. So the upshot of the story was: Don’t listen to your client! But rather work with them to understand their underlying needs
  • without art there is no engineering
  • without engineering there is no art
  • Take pride in your work
  • Ruby is your Paint; Rails is your Canvas; Use them and create something beautiful

Rails GIS hacks tutorial slides

Posted by Shoaib on September 18, 2007

Aleks has just sent us the updated slides for the Rails GIS hacks tutorial. So if you attended today’s tutorial and were wondering where to get the slides – download them while they’re hot: Rails GIS Hacks Slides. Don’t forget to leave us feedback if you have any. See the RailsConfEurope page for all the tutorial material

RailsConf Europe 2007 (Day 1: Why am I so stressed?)

Posted by Shoaib on September 18, 2007

Although for most attendees the conference started at 8.00AM our day at the conference didn’t begin until 12 noon, as first-time speakers at RailsConf Europe 07. The reason being all the late night partying… ahem …. I mean talk practice sessions we had been doing. It was beautiful sunny day with a t-shirt temperature of 24 degrees.

Lunch at the conference was very elaborate, grand and delicious. After setting up the Audio Visual at 12.30 Kashif, Aleks and I ate some lunch and waited patiently for the approaching talk.

At 1PM folks started to trickle in and by 1.30PM over a 150 people were packed in the brightly lit ball-room. I must say it was fairly daunting to see so many bums on seats.

And so we got started on time and as it often happens with talks after lunch you could notice some people yawning … especially when you have a buffet of that magnitude! Anyways the talk went according to plan. We got a few interested attendees who chatted with us about their particular applications. Feed back after the talk from a handful of people so far has been positive saying it contained a lot of very useful information and was well worth attending. Some may have found it a bit overwhelming but all were confident that the hand-out we provided was going to help overcome that issue. We will be following up on some questions with blog entries so look out for a tutorial on setting up the Rails GeoStack on Mac OSX, references to free geocoded address databases and free routing applications and web services.

Kashif & Aleks haven’t been getting much sleep lately due to all the hard work they put into the Rails GIS Hacks tutorial and so after the talk Aleks left to get some rest (as she has a d’day party to attend tonight) and Kashif is also fast asleep as I write this.

What happened to the cognac? The infamous bottle of cognac seems to have mysteriously disappeared – I am waiting to ask Aleks about it when she returns from the party.