Getting Started with ESRI GeoProcessing Using Ruby

ESRI has chosen Python as their language of choice for GeoProcessing. However, they point out in the documentation that any COM compliant langauge will work. So you can also use Ruby. I have found very little discussion of this online so I thought I'd share this to serve as an introduction for anyone wanting to give it a try. The code on this page is just a simple GeoProcessing script. You pass in a shapefile and it opens it up and tells you about the spatial reference and the fields it contains. The two key lines for any GeoProcessing in Ruby are as follows:

require 'win32ole'
gp = WIN32OLE.new('esriGeoprocessing.GpDispatch.1')

The first line loads the win32ole libary that allows us to use COM objects. The second line creates an instance of the ESRI GeoProcessing COM object. We can now start GeoProcessing. As I said this example is pretty simple and it isn't especially useful, but it should demonstrate the basic concepts. To execute this open a command line and you simply pass in a shapefile as an argument (you'll need to include the whole path), for example,

gp_example.rb C:/Docs/countries.shp

The script checks for an argument and that the file exists and then creates the GeoProcessor object. Bear in mind creating the object can be pretty slow as it goes out checking for licenses and such. The next two lines simply access the Describe() method of the GeoProcessor object and use the SpatialReference property to access and print the spatial reference type and name. This might look like:

C:\Docs\countries.shp is currently Geographic
C:\Docs\countries.shp is currently GCS_WGS_1984


The next section simply loops through the fields of the shapefile and prints them out. This again uses the Describe() method of the GeoProcessor object, but this time to access the Fields property. Using the Next() method the fields can be looped through sequentially in a while loop. The output might look like:

FID
Shape
CNTRY_NAME
FIRST_FIRS

The last section is a standard Ruby rescue block in case anything goes wrong. Here we check for errors from the GeoProcessor object using:

err = gp.GetMessages(2)

The two passed into the GetMessages() method asks specifically for error messages. If something is returned that message is printed. However, if there were no errors reported by the GeoProcessor object the script returns the error message and stack trace from the Ruby interpreter.

So there you have it - a simple Ruby based GeoProcessing script that reports back information on a shapefile. Hopefully, this helps set you off on your way to GeoProcessing with Ruby, should you prefer using it over Python. The complete code is listed below.
#!/usr/bin/ruby
require 'win32ole'

#Example:
#gp_example.rb C:/Docs/countries.shp

if ARGV.length != 1
        puts "\nUsage: gp_example.rb <shapefile>"
        exit(integer = 255)
else
        infile = ARGV[0]
end

if !File.exist?(infile)
        puts "Could not find projection file: " + infile
        exit(integer = 255)
end

begin
        #create the geoprocessor object
        gp = WIN32OLE.new('esriGeoprocessing.GpDispatch.1')

        puts infile + " is currently " + gp.Describe(infile).SpatialReference.Type
        puts infile + " is currently " + gp.Describe(infile).SpatialReference.Name

        desc = gp.Describe(infile)
        fields = desc.Fields
        field = fields.next()
        while field
                puts field.Name
                field = fields.next()
        end
rescue => e
        err = gp.GetMessages(2)
        if (err != "")
                puts err
        else
                puts "#{e.message}\n#{e.backtrace}"
        end
end

#Tested Against: ruby 1.8.2 (2004-12-25) [i386-mswin32]