Skip to content

Commit 7578cca

Browse files
committed
fix: wbt_source: short circuit for common single-layer geotiff case
1 parent 629da64 commit 7578cca

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

R/wbt_source.R

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,17 @@ wbt_source <- function(x,
7070
stop("Failed to write `x` (", x, ") to Shapefile: ", fp, "\n", res[1], call. = FALSE)
7171
}
7272
} else if (inherits(x2, 'try-error')) {
73-
if (!grepl("\\.tiff?$", x, ignore.case = TRUE) || length(layer) > 0) {
73+
is_geotiff <- grepl("\\.tiff?$", x, ignore.case = TRUE)
74+
75+
# check if we need to write a new file
76+
# - not a geotiff OR
77+
# - a layer is specified that is not the first layer
78+
write_new_file <- !is_geotiff ||
79+
(length(layer) > 0 &&
80+
layer[1] != 1 &&
81+
layer[1] != names(terra::rast(x))[1])
82+
83+
if (write_new_file) {
7484
# try reading a raster file and writing to geotiff
7585
fp <- paste0(fp, ".tif")
7686
if (length(layer) > 0) {
@@ -82,7 +92,15 @@ wbt_source <- function(x,
8292
if (!inherits(res, 'try-error') && file.exists(fp)) {
8393
x <- fp
8494
} else {
85-
stop("Failed to write `x` (", x, ") to GeoTIFF: ", fp, "\n", res, call. = FALSE)
95+
stop(
96+
"Failed to write `x` (",
97+
x,
98+
") to GeoTIFF: ",
99+
fp,
100+
"\n",
101+
res,
102+
call. = FALSE
103+
)
86104
}
87105
}
88106
x <- terra::rast(x)

0 commit comments

Comments
 (0)