GrassGis

Support for scripting GRASS with Ruby.

Installation

Add this line to your application's Gemfile:

gem 'grassgis'

And then execute:

$ bundle

Or install it yourself as:

$ gem install grassgis

Usage

This library can prepare environments to execute GRASS commands from Ruby scripts.

Example:

require 'grassgis'

configuration = {
  gisbase: '/usr/local/Cellar/grass-70/7.0.0/grass-7.0.0',
  location: 'world'
}

GrassGis.session configuration do
  r.resamp.stats '-n', input: "map1@mapset1", output: "map2"
  puts g.list('vect').output
end

Roadmap

Create a DSL to define GRASS processing recipes.

It should handle errors gracefully during recipe execution, take care of logging, parsing command output, temporary files, etc.

A recipe could have requirements such as which other recipes it depends upon (so must be executed first), which map layers must exist, etc.

It could also define what map layers or other files are generated by the recipe.

Example of intended recipe syntax:

recipe :resamp_average do |options = {}|
  input_raster = options[:input]
  input_res = options[:input_res] # TODO: extract from input_rater info
  output_raster = options[:output]
  output_res = options[:output_res] # TODO: extract from output_raster info
  if options[:direction]
    unless raster_exists?("#{input_raster}_sin")
      g.region res: input_res
      r.mapcalc "#{input_raster}_sin = sin(#{input_raster})"
    end
    unless raster_exists?("#{input_raster}_cos")
      g.region res: input_res
      r.mapcalc "#{input_raster}_cos = cos(#{input_raster})"
    end
    g.region res: output_res
    r.resamp.stats input: "#{input_raster}_cos", output: "#{output_raster}_cos"
    r.resamp.stats input: "#{input_raster}_sin", output: "#{output_raster}_sin"
    r.mapcalc "#{output_raster} = atan(#{output_raster}_cos,#{output_raster}_sin)"
    g.remove rast: ["#{output_raster}_cos", "#{output_raster}_sin"]
  else
    g.region res: output_res
    r.resamp.stats input: input_raster, output: output_raster
  end
end

recipe :generate_working_dem do |...|
  define :working_dem, 'dem'
  define :working_slope, 'slope'
  define :working_aspect, 'aspect'
  g.region res: working_res
  apply :resamp_average, input: base_dem, output: working_dem, input_res: base_dem_res, output_res: working_res
  apply :resamp_average, input: base_slope, output: working_slope, input_res: base_dem_res, output_res: working_res
  apply :resamp_average, input: base_aspect, output: working_aspect, input_res: base_dem_res, output_res: working_res, direction: true
  describe working_slope, "Pendiente en grados a #{working_res} #{region_units}"
  # ...
end

Contributing

  1. Fork it ( https://github.com/[my-github-username]/grassgis/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request