Skip to content

Commit

Permalink
add saga-gis path setting in r
Browse files Browse the repository at this point in the history
  • Loading branch information
SpatLyu committed Aug 15, 2024
1 parent 50997f0 commit 0fbf518
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 58 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Description: Provides an R scripting interface to the open-source 'SAGA-GIS'
License: GPL-3
Encoding: UTF-8
SystemRequirements: SAGA-GIS (>= 2.3.1)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Depends:
R (>= 2.10)
Imports:
Expand Down
115 changes: 59 additions & 56 deletions R/search-saga.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,76 +24,79 @@
#'
#' @export
search_saga <- function() {
# check to see if saga_cmd is recognized (i.e. has been added to path)
saga_cmd <- if (nchar(Sys.which(names = "saga_cmd")) > 0) "saga_cmd" else NULL
if (!is.null(getOption("saga_cmd"))){
return(getOption("saga_cmd"))
} else {
saga_cmd <- if (nchar(Sys.which(names = "saga_cmd")) > 0) "saga_cmd" else NULL

if (is.null(saga_cmd)) {
if (is.null(saga_cmd)) {

# define search paths
if (Sys.info()["sysname"] == "Windows") {
search_paths <- c(
"C:/Program Files/SAGA-GIS",
"C:/Program Files (x86)/SAGA-GIS",
"C:/SAGA-GIS",
"C:/OSGeo4W",
"C:/OSGeo4W64"
)
saga_executable <- "saga_cmd.exe"
# define search paths
if (Sys.info()["sysname"] == "Windows") {
search_paths <- c(
"C:/Program Files/SAGA-GIS",
"C:/Program Files (x86)/SAGA-GIS",
"C:/SAGA-GIS",
"C:/OSGeo4W",
"C:/OSGeo4W64"
)
saga_executable <- "saga_cmd.exe"

} else if (Sys.info()["sysname"] == "Linux") {
search_paths <- c("/usr")
saga_executable <- "saga_cmd$"
} else if (Sys.info()["sysname"] == "Linux") {
search_paths <- c("/usr")
saga_executable <- "saga_cmd$"

} else if (Sys.info()["sysname"] == "Darwin") {
search_paths <- c(
"/Applications/SAGA.app/Contents/MacOS",
"/usr/local/bin",
"/Applications/QGIS.app/Contents/MacOS/bin"
)
} else if (Sys.info()["sysname"] == "Darwin") {
search_paths <- c(
"/Applications/SAGA.app/Contents/MacOS",
"/usr/local/bin",
"/Applications/QGIS.app/Contents/MacOS/bin"
)

saga_executable <- "^saga_cmd$"
}
saga_executable <- "^saga_cmd$"
}

# search for saga_cmd executable
saga_cmd <- c()
for (f in search_paths) {
saga_cmd <- c(saga_cmd, list.files(
path = f,
pattern = saga_executable,
recursive = TRUE,
full.names = TRUE
))
# search for saga_cmd executable
saga_cmd <- c()
for (f in search_paths) {
saga_cmd <- c(saga_cmd, list.files(
path = f,
pattern = saga_executable,
recursive = TRUE,
full.names = TRUE
))
}
}
}

# error is saga_cmd not found
if (length(saga_cmd) == 0) {
rlang::abort(
paste(
"SAGA-GIS installation not found. Need to supply a valid path",
"to the saga_cmd executable"
# error is saga_cmd not found
if (length(saga_cmd) == 0) {
rlang::abort(
paste(
"SAGA-GIS installation not found. Need to supply a valid path",
"to the saga_cmd executable"
)
)
)

return(NULL)
return(NULL)

# automatically use newest version if multiple installations are found
} else if (length(saga_cmd) > 1) {
message("Multiple installations of SAGA-GIS were found at:")
message(paste(saga_cmd, collapse = "\n"))
message(paste(
"Choosing newest version. Manually specify the location when",
"calling saga_gis() to use an older version"
))

# automatically use newest version if multiple installations are found
} else if (length(saga_cmd) > 1) {
message("Multiple installations of SAGA-GIS were found at:")
message(paste(saga_cmd, collapse = "\n"))
message(paste(
"Choosing newest version. Manually specify the location when",
"calling saga_gis() to use an older version"
))
saga_vers <- list()

saga_vers <- list()
for (saga_inst in saga_cmd) {
saga_vers <- append(saga_vers, saga_version(saga_inst))
}

for (saga_inst in saga_cmd) {
saga_vers <- append(saga_vers, saga_version(saga_inst))
saga_cmd <- saga_cmd[which(saga_vers == max(saga_vers))]
}

saga_cmd <- saga_cmd[which(saga_vers == max(saga_vers))]
return(saga_cmd)
}

return(saga_cmd)
}
2 changes: 1 addition & 1 deletion man/Rsagacmd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0fbf518

Please sign in to comment.