Module: GrassGis

Defined in:
lib/grassgis/error.rb,
lib/grassgis/module.rb,
lib/grassgis/mapset.rb,
lib/grassgis/version.rb,
lib/grassgis/support.rb,
lib/grassgis/context.rb,
lib/grassgis/location.rb

Defined Under Namespace

Modules: Support Classes: Context, Error, Location, Mapset, Module

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.error(command, error_mode = :raise) ⇒ Object


400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/grassgis/context.rb', line 400

def self.error(command, error_mode = :raise)
  if command
    if command.error # :silent mode for testing/debugging?
      # Errors that prevent command execution
      # (usually ENOENT because the command does not exist)
      # are always raised
      raise command.error unless error_mode == :silent
    elsif error_mode == :raise
      if (command.status_value && command.status_value != 0)
        raise Error.new, error_info(command)
      end
    end
  end
end

.error?(command) ⇒ Boolean


384
385
386
# File 'lib/grassgis/context.rb', line 384

def self.error?(command)
  command && (!!command.error || (command.status_value && command.status_value != 0))
end

.error_info(command) ⇒ Object


388
389
390
391
392
393
394
395
396
397
398
# File 'lib/grassgis/context.rb', line 388

def self.error_info(command)
  if command
    if command.error
      info = "Error (#{command.error.class}):\n"
      info << command.error.to_s
    elsif (command.status_value && command.status_value != 0)
      info = "Exit code #{command.status_value}\n"
      info << command.error_output if command.error_output
    end
  end
end

.session(config, &blk) ⇒ Object

Evaluate a block in a GRASS session environment The configuration must include at leaast:

  • :gibase The base GRASS instalation directory

  • :location The location to work with

Optional parameters:

  • :gisdbase The base GRASS data directory

  • :mapset The default mapset

  • :version The GRASS version

Example:

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: "[email protected]", output: "map2"
  cmd = g.list 'vect'
  puts cmd.output
end

Note that block is evaluated in a spacial context, so that the lexical scope's self and instance variables are not directly available inside it. Local variables in the scope can be used to access self-related information. Also, local values can be injected in the block with the :locals option:

this = self # make self available a local variable
locals = { context: self } # inject locals
GrassGis.session configuration.merge(locals:) do
     r.resamp.stats '-n', input: this.input, output: context.output
end

Other pararameters:

:errors to define the behaviour when a GRASS command fails:

  • :raise is the default and raises on errors

  • :console shows standar error output of commands

  • :quiet error output is retained but not shown; no exceptions are raise except when the command cannot be executed (e.g. when the command name is ill-formed)

If :errors is anything other than :raise, it is up to the user to check each command for errors. With the :console option the standar error output of commands is sent to the console and is not accessible through the command's error_output method.

:log is used to define a loggin file where executed commands and its output is written.

:history is used to define a loggin file where only executed commands are written.

:echo controls what is echoed to the standard output and can be one of the following options:

  • :commands show all executed commands (the default)

  • :output show the output of commands too

  • false don't echo anything

Testing/debugging options:

  • :dry prevents actual execution of any command

  • errors: :silent omits raising exceptions (as :quiet) even when a command cannot be executed (usually because of an invalid command name)


374
375
376
377
378
379
380
381
382
# File 'lib/grassgis/context.rb', line 374

def self.session(config, &blk)
  context = Context.new(config)
  context.allocate
  context.log_header
  create context, config[:create]
  context.session &blk
ensure
  context.dispose if context
end

.version(version) ⇒ Object

Return a comparable Version object from a version number string


416
417
418
# File 'lib/grassgis/context.rb', line 416

def self.version(version)
  Gem::Version.new version
end