Skip to content

Commit

Permalink
as_qgis_argument.SpatRaster(): handle band mismatches wrt source file
Browse files Browse the repository at this point in the history
  • Loading branch information
florisvdh committed Jun 3, 2023
1 parent 08abc26 commit 86de86c
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions R/compat-terra.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,24 @@ as_qgis_argument_terra <- function(x, spec = qgis_argument_spec(),
}

if (!identical(sources, "") && length(sources) == 1) {
accepted_ext <- c("grd", "asc", "sdat", "rst", "nc", "tif", "tiff", "gtiff", "envi", "bil", "img")
file_ext <- stringr::str_to_lower(tools::file_ext(sources))
if (file_ext %in% c("grd", "asc", "sdat", "rst", "nc", "tif", "tiff", "gtiff", "envi", "bil", "img")) {
return(sources)
if (file_ext %in% accepted_ext) {
names_match <- identical(names(x), names(terra::rast(sources)))
if (names_match) {
return(sources)
} else if (terra::nlyr(x) > 1L) {
message(glue(
"Rewriting the multi-band SpatRaster object as a temporary file before passing to QGIS, since ",
"its bands (order, selection) differ from those in the source file '{ sources }'."
))
} else {
message(glue(
"Rewriting the '{ names(x) }' band of '{ sources }' as a temporary file, otherwise ",
"QGIS may use another or all bands of the source file if ",
"passing its filepath."
))
}
}
}

Expand Down

0 comments on commit 86de86c

Please sign in to comment.