Module: GrassGis
- Defined in:
- lib/grassgis/error.rb,
lib/grassgis/tools.rb,
lib/grassgis/mapset.rb,
lib/grassgis/module.rb,
lib/grassgis/context.rb,
lib/grassgis/support.rb,
lib/grassgis/version.rb,
lib/grassgis/location.rb
Defined Under Namespace
Modules: Support, Tools Classes: Context, Error, Location, Mapset, Module
Constant Summary collapse
- VERSION =
"0.5.0"
Class Method Summary collapse
- .error(command, error_mode = :raise) ⇒ Object
- .error?(command) ⇒ Boolean
- .error_info(command) ⇒ Object
-
.session(config, &blk) ⇒ Object
Evaluate a block in a GRASS session environment The configuration must include at leaast:.
-
.version(version) ⇒ Object
Return a comparable Version object from a version number string.
Class Method Details
.error(command, error_mode = :raise) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/grassgis/context.rb', line 406 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
390 391 392 |
# File 'lib/grassgis/context.rb', line 390 def self.error?(command) command && (!!command.error || (command.status_value && command.status_value != 0)) end |
.error_info(command) ⇒ Object
394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/grassgis/context.rb', line 394 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: "map1@mapset1", 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)
380 381 382 383 384 385 386 387 388 |
# File 'lib/grassgis/context.rb', line 380 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
422 423 424 |
# File 'lib/grassgis/context.rb', line 422 def self.version(version) Gem::Version.new version end |