diff --git a/R/RSAGA-core.R b/R/RSAGA-core.R index df9eccc..7b5b39a 100644 --- a/R/RSAGA-core.R +++ b/R/RSAGA-core.R @@ -857,6 +857,9 @@ rsaga.html.help = function(lib, module=NULL, use.program.folder = TRUE, env=rsag #' @param check.module.exists logical (default: `TRUE`): call [rsaga.module.exists()] to determine if the specified module can be called in the current SAGA installation #' @param warn logical (default: `TRUE`): for internal purposes - can be used to suppress warning messages generated by failed SAGA_CMD calls; currently used by [rsaga.get.lib.modules()] and related functions; see [options()] argument `warn` for details #' @param argsep character (default: `" "`; currently for internal use): defines the character symbol used as a separator between each argument name and argument value passed to `saga_cmd`. SAGA GIS 2.1.0 (RC1) seems to move toward `"="` as a separator, but `" "` still works and some modules (e.g. the used by `rsaga.pisr`) don't seem to work with `argsep="="`. Future releases of RSAGA may change the default `argsep` value and/or delete or ignore this argument and/or move it to [rsaga.env()]. +#' +#' @param check.parameters logical(default: `TRUE`): Check if correct parameters are used. +#' #' @param ... Additional arguments to be passed to [base::system()]. #' #' @details This workhorse function establishes the interface between the SAGA command line program and R by submitting a system call. This is a low-level function that may be used for directly accessing SAGA; specific functions such as `rsaga.hillshade` are intended to be more user-friendly interfaces to the most frequently used SAGA modules. These higher-level interfaces support default values for the arguments and perform some error checking; they should therefore be preferred if available. @@ -899,7 +902,7 @@ rsaga.geoprocessor = function( prefix = NULL, flags = ifelse(show.output.on.console,"q","s"), cores, env = rsaga.env(), display.command = FALSE, reduce.intern = TRUE, check.module.exists = TRUE, warn = options("warn")$warn, - argsep = " ", ... ) + argsep = " ", check.parameters = TRUE, ... ) { # Issue warning if using SAGA GIS version that has not been tested with RSAGA: if (!is.null(env$version)) { @@ -988,6 +991,42 @@ rsaga.geoprocessor = function( } } + # Check parameters + if(check.parameters) { + # Some rsaga core calls need to suppress warnings + # related to non-zero exit codes of saga_cmd: + oldwarn = options("warn")$warn + on.exit(options(warn = oldwarn), add = TRUE) + options(warn = -1) + + # Get console output + res <- system(paste0(command, " \"", module, "\""), intern = TRUE) + options(warn = oldwarn) + + # Get parameters + i <- grep("Usage:", res) + + # Extract parameter list + unlist(str_extract_all(res[i], "\\[(.*?)\\]")) %>% + str_remove("\\[-") %>% + str_remove("\\]") %>% + str_remove("<(.*?)>") %>% + str_remove("\\s") -> param_list + + # Print error message + if(!all(names(params) %in% param_list)) { + #Get false parameter names + false_paramters <- names(params) %in% param_list + # Get all paramter names + paramters <- names(params) + stop(paste0("Wrong paramters used: ", + paste(paramters[!false_paramter], collapse = " "), + ". ", + "Possible parameters:", + paste(param_list, collapse = " "))) + } + } + if (is.character(module)) module = shQuote(module) command = paste(command, module) if (length(param)>0) { diff --git a/man/rsaga.geoprocessor.Rd b/man/rsaga.geoprocessor.Rd index 4c44d8a..d47bd76 100644 --- a/man/rsaga.geoprocessor.Rd +++ b/man/rsaga.geoprocessor.Rd @@ -9,7 +9,7 @@ rsaga.geoprocessor(lib, module = NULL, param = list(), prefix = NULL, flags = ifelse(show.output.on.console, "q", "s"), cores, env = rsaga.env(), display.command = FALSE, reduce.intern = TRUE, check.module.exists = TRUE, warn = options("warn")$warn, argsep = " ", - ...) + check.parameters = TRUE, ...) } \arguments{ \item{lib}{Name of the SAGA library to be called (see Details).} @@ -42,6 +42,8 @@ rsaga.geoprocessor(lib, module = NULL, param = list(), \item{argsep}{character (default: \code{" "}; currently for internal use): defines the character symbol used as a separator between each argument name and argument value passed to \code{saga_cmd}. SAGA GIS 2.1.0 (RC1) seems to move toward \code{"="} as a separator, but \code{" "} still works and some modules (e.g. the used by \code{rsaga.pisr}) don't seem to work with \code{argsep="="}. Future releases of RSAGA may change the default \code{argsep} value and/or delete or ignore this argument and/or move it to \code{\link[=rsaga.env]{rsaga.env()}}.} +\item{check.parameters}{logical(default: \code{TRUE}): Check if correct parameters are used.} + \item{...}{Additional arguments to be passed to \code{\link[base:system]{base::system()}}.} } \value{ @@ -72,7 +74,7 @@ rsaga.hillshade("dem","hillshade",exaggeration=2) # using the RSAGA geoprocessor: rsaga.geoprocessor("ta_lighting",0,list(ELEVATION="dem.sgrd",SHADE="hillshade",EXAGGERATION=2)) # equivalent DOS command line call: -# saga_cmd.exe ta_lighting 0 -ELEVATION dem.sgrd -SHADE hillshade -EXAGGERATION 2 +# saga_cmd.exe ta_lighting 0 -ELEVATION dem.sgrd -SHADE hillshade -EXAGGERATION 2 } } \references{