Skip to content

Commit

Permalink
read_RAST(): implement GDAL-GRASS driver by default (only with terra)
Browse files Browse the repository at this point in the history
  • Loading branch information
florisvdh committed Jun 15, 2024
1 parent 0f0525b commit 4389249
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
70 changes: 41 additions & 29 deletions R/rast_link.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
read_RAST <- function(
vname, cat = NULL, NODATA = NULL,
ignore.stderr = get.ignore.stderrOption(), return_format = "terra",
close_OK = return_format == "SGDF", flags = NULL) {
use_gdal_grass_driver = TRUE, close_OK = return_format == "SGDF",
flags = NULL) {
if (!is.null(cat)) {
if (length(vname) != length(cat)) {
stop("vname and cat not same length")
Expand All @@ -18,6 +19,7 @@ read_RAST <- function(
openedConns <- as.integer(row.names(showConnections()))
}
stopifnot(is.logical(ignore.stderr), !is.na(ignore.stderr))
stopifnot(is.logical(use_gdal_grass_driver), !is.na(use_gdal_grass_driver))

if (!is.null(NODATA)) {
if (any(!is.finite(NODATA)) || any(!is.numeric(NODATA))) {
Expand Down Expand Up @@ -54,20 +56,23 @@ read_RAST <- function(
if (!(requireNamespace("terra", quietly = TRUE))) {
stop("terra required for SpatRaster output")
}
drv <- "RRASTER"
fxt <- ".grd"
ro <- FALSE
o <- execGRASS("r.out.gdal", flags = "l", intern = TRUE)
oo <- grep("RRASTER", o)
if (length(oo) == 0L) ro <- TRUE
if (!ro) {
RR <- o[oo]
RRs <- strsplit(RR, " ")[[1]]
if (length(grep("\\(rw", RRs)) == 0L) ro <- TRUE
}
if (ro) {
drv <- "GTiff"
fxt <- ".tif"
has_grassraster_drv <- gdal_has_grassraster_driver()
if (!has_grassraster_drv || !use_gdal_grass_driver) {
drv <- "RRASTER"
fxt <- ".grd"
ro <- FALSE
o <- execGRASS("r.out.gdal", flags = "l", intern = TRUE)
oo <- grep("RRASTER", o)
if (length(oo) == 0L) ro <- TRUE
if (!ro) {
RR <- o[oo]
RRs <- strsplit(RR, " ")[[1]]
if (length(grep("\\(rw", RRs)) == 0L) ro <- TRUE
}
if (ro) {
drv <- "GTiff"
fxt <- ".tif"
}
}
reslist <- vector(mode = "list", length = length(vname))
names(reslist) <- gsub("@", "_", vname)
Expand Down Expand Up @@ -197,22 +202,29 @@ read_RAST <- function(
} else {
NODATAi <- NODATA[i]
}
tmplist[[i]] <- tempfile(fileext = fxt)
if (is.null(flags)) flags <- c("overwrite", "c", "m")
if (!is.null(cat) && cat[i]) flags <- c(flags, "t")
if (is.null(typei)) {
execGRASS("r.out.gdal",
input = vname[i], output = tmplist[[i]],
format = drv, nodata = NODATAi, flags = flags,
ignore.stderr = ignore.stderr
)
if (has_grassraster_drv && use_gdal_grass_driver) {
args <- list(name = vca[1], type = "raster")
if (length(vca) == 2L) args <- c(args, mapset = vca[2])
tmplist[[i]] <- do.call(generate_header_path, args)
} else {
execGRASS("r.out.gdal",
input = vname[i], output = tmplist[[i]],
format = drv, nodata = NODATAi, type = typei, flags = flags,
ignore.stderr = ignore.stderr
)
tmplist[[i]] <- tempfile(fileext = fxt)
if (is.null(flags)) flags <- c("overwrite", "c", "m")
if (!is.null(cat) && cat[i]) flags <- c(flags, "t")
if (is.null(typei)) {
execGRASS("r.out.gdal",
input = vname[i], output = tmplist[[i]],
format = drv, nodata = NODATAi, flags = flags,
ignore.stderr = ignore.stderr
)
} else {
execGRASS("r.out.gdal",
input = vname[i], output = tmplist[[i]],
format = drv, nodata = NODATAi, type = typei, flags = flags,
ignore.stderr = ignore.stderr
)
}
}
# message("Reading ", tmplist[[i]])
reslist[[i]] <- getMethod("rast", "character")(tmplist[[i]])
}
resa <- getMethod("rast", "list")(reslist)
Expand Down
15 changes: 12 additions & 3 deletions man/readRAST.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Read GRASS raster files from GRASS into R \pkg{terra} \code{"SpatRaster"} or \pk

\usage{
read_RAST(vname, cat=NULL, NODATA=NULL, ignore.stderr=get.ignore.stderrOption(),
return_format="terra", close_OK=return_format=="SGDF", flags=NULL)
return_format="terra", use_gdal_grass_driver = TRUE, close_OK=return_format=="SGDF",
flags=NULL)
write_RAST(x, vname, zcol = 1, NODATA=NULL, flags=NULL,
ignore.stderr = get.ignore.stderrOption(), overwrite=FALSE, verbose=TRUE)
}
Expand All @@ -20,6 +21,7 @@ write_RAST(x, vname, zcol = 1, NODATA=NULL, flags=NULL,
\item{vname}{A vector of GRASS raster file names in mapsets in the current search path, as set by \dQuote{g.mapsets}; the file names may be given as fully-qualified map names using \dQuote{name@mapset}, in which case only the mapset given in the full path will be searched for the existence of the raster; if more than one raster with the same name is found in mapsets in the current search path, an error will occur, in which case the user should give the fully-qualified map name. If the fully-qualified name is used, \code{@} will be replaced by underscore in the output object.}
\item{cat}{default NULL; if not NULL, must be a logical vector matching vname, stating which (CELL) rasters to return as factor}
\item{return_format}{default \code{"terra"}, optionally \code{"SGDF"}}
\item{use_gdal_grass_driver}{Default \code{TRUE}. The \href{https://github.com/OSGeo/gdal-grass}{standalone GDAL-GRASS driver} for the raster format will be used if \code{return_format} is \code{"terra"} and if the driver is installed. The advantage is that no intermediate file(s) need to be written from GRASS GIS and subsequently read into R; instead the raster dataset(s) are read directly from the GRASS GIS database.}
\item{ignore.stderr}{default taking the value set by \code{set.ignore.stderrOption}; can be set to TRUE to silence \code{system()} output to standard error; does not apply on Windows platforms}
\item{close_OK}{default TRUE - clean up possible open connections used for reading metadata; may be set to FALSE to avoid the side-effect of other user-opened connections being broken}
\item{x}{A \pkg{terra} \code{"SpatRaster"} or \pkg{sp} \code{"SpatialGridDataFrame"} object}
Expand Down Expand Up @@ -91,10 +93,17 @@ if (run) {
}
if (run) {
print(system.time(sqdem <- read_RAST(c("sqdemSP", "elevation"),
return_format="terra")))
return_format="terra",
use_gdal_grass_driver = FALSE)))
}
# install the GDAL-GRASS driver to achieve higher speed:
if (run) {
names(sqdem)
print(system.time(sqdem <- read_RAST(c("sqdemSP", "elevation"),
return_format="terra",
use_gdal_grass_driver = TRUE)))
}
if (run) {
names(sqdem)
}
if (run) {
sqdem1 <- read_RAST(c("sqdemSP@PERMANENT", "elevation@PERMANENT"))
Expand Down

0 comments on commit 4389249

Please sign in to comment.