Ruby Bindings for GEOS

One of the things I do is help maintain the SWIG bindings for the GEOS project. GEOS, which stands for the Geometry Engine Open Source, is a library for working with 2D geometries. It includes support for various spatial operators and relationships (for some nice pictures that visually explain what these are take a look at the PDF documentation for a closely related project called JTS).

The original SWIG binding was done by Sean Gillies for Python about a year ago. The upcoming GEOS 2.2.2 release will include a Ruby binding that I’ve put together. Since 2.2.2 is a bug fix release, the new Ruby binding is not part of the standard build process. This has already been fixed in CVS head, and both the Python and Ruby bindings will be fully integrated into the standard build sequence for GEOS 3.

For those intrepid souls who would like to try out this library here are the instructions on how to build it.

If you are using Linux or cygwin or mingW and msys on Windows:

# Change to the directory where you installed geos
$ cd /usr/src/geos/swig/ruby (or wherever you installed geos)

# Download the SWIG interface file or
create it using SWIG 1.3.29 or higher
$ swig -c++ -ruby -autorename -o geos_wrap.cxx ../geos.i

# Compile the SWIG wrapper
$ g++ -c -I../../source/headers/ 
         -I/usr/lib/ruby/1.8/i386-linux geos_wrap.cxx

# Create a shared library
$ g++ -shared -lgeos -lruby geos_wrap.o -o

# Install the shared library
$ sudo cp /usr/lib/site_ruby/1.8/i386-linux/

# Test out the new extension
$ irb

# Load the extension in IRB
irb(main):004:0> require 'geos'
=> true

If you are running on Fedora Core 5, you will run into this error when trying to load the ruby extension:

cannot restore segment prot after reloc: Permission denied

Dan Walsh recently explained that this is caused by SELinux permission checks. To work around it:

$ chcon -t textrel_shlib_t 

You can also build GEOS and the Ruby extension with Visual Studio 2005. Unfortunately, the Visual Studio project file that ship with GEOS 2.2.1 does not work and as a result have been removed from the distribution. If people are interested, I’d be happy to post my custom solution

Once you’ve built the extension comes the fun part – using it. To get started, take a look at the test suite I’ve put together in ruby/test. You’ll see examples that show how to use a significant part of GEOS’s functionality, including:

  • Importing and exporting geometries using the well know binary and text formats
  • Spatial operators such as buffers, centroids, etc.
  • Spatial relationships such as overlap, within, etc.

Good luck and let me know what you find!

Update – Good news. It turns out that Hobu has been maintaining a Visual Studio makefile that works with Geos. Its under geos/source/ I’ll see if I can put together a for the Ruby bindings.

Leave a Reply

Your email address will not be published.