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:


Use this link to trackback from your own site.


Leave a response

  1. John Wed, 26 May 2010 18:28:31 UTC

    Thanks for making your modified sqlite3-ruby available – I’m needing to use spatialite from Ruby scripts too!
    I downloaded your 1.2.5 zip of it, unzipped and ran the setup.rb setup, config and install… it did put the files in \site_ruby\1.8\sqlite3 as expected and I am able to require this and connect to a .sqlite file.
    Unfortunately, I’m still unable to
    “db.enable_load_extension( 1 )”… still getting “undefined method `enable_load_extension’ for # (NoMethodError)” …

    Have I taken a wrong turn somewhere? if I understood correctly, your sqlite3-ruby adds this, right?


  2. Kashif Wed, 26 May 2010 20:45:10 UTC

    Hi john, the latest sqlite3-ruby beta gem now has the extension loading support. So just do: gem install sqlite3-ruby --pre to get this version of the gem and then you should be able to do db.enable_load_extension( 1 ) etc. Hope this helps!