Skip to content

Commit f05345f

Browse files
committed
fix gdal_addo()
Now works with config_options, allowing compressed overviews
1 parent 274e4fc commit f05345f

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

R/stars.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ gdal_create = function(f, nxy, values, crs, xlim, ylim) {
404404
#' @export
405405
gdal_addo = function(file, overviews = c(2,4,8,16), method = "NEAREST", layers = integer(0),
406406
options = character(0), config_options = character(0), clean = FALSE, read_only = FALSE) {
407-
stopifnot(length(method) == 1, is.character(method), is.numeric(overviews))
407+
stopifnot(length(method) == 1, is.character(method), is.numeric(overviews), is.character(config_options))
408408
invisible(CPL_gdaladdo(file, method, as.integer(overviews), as.integer(layers), as.character(options),
409-
as.character(config_options), as.logical(clean)[1], as.logical(read_only)[1]))
409+
config_options, as.logical(clean)[1], as.logical(read_only)[1]))
410410
}

tests/testthat/test_gdal.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,55 @@ test_that('gdal_utils work', {
9595
# gdalwarp -t_srs '+proj=utm +zone=11 +datum=WGS84' -overwrite NETCDF:avhrr-only-v2.19810901.nc:anom utm11.tif
9696
# becomes:
9797
# st_gdalwarp("NETCDF:avhrr-only-v2.19810901.nc:anom", "utm11.tif", c("-t_srs", "+proj=utm +zone=11 +datum=WGS84"))
98+
99+
test_that('gdal_addo works', {
100+
101+
skip_on_cran()
102+
103+
has_overviews = function(x){
104+
info = gdal_utils(source = x, quiet = TRUE)
105+
grepl("overview", info, ignore.case = TRUE)
106+
}
107+
108+
has_compressed_overviews = function(x){
109+
# Check if sidecar overview file has compression, x is tif path
110+
path = paste0(x, ".ovr") # overview file
111+
info = gdal_utils(source = path, quiet = TRUE)
112+
if(!file.exists(path))
113+
return(NA)
114+
grepl("compression", info, ignore.case = TRUE)
115+
}
116+
117+
# setup
118+
dir = file.path(tempdir(), "gdal_addo")
119+
dir.create(dir)
120+
on.exit(unlink(dir, recursive = TRUE)) # cleanup when done
121+
tif = file.path(dir, "geomatrix.tif")
122+
file.copy(system.file("tif/geomatrix.tif", package = "sf"),
123+
tif, overwrite = TRUE)
124+
125+
expect_false(has_overviews(tif))
126+
127+
# Default arguments
128+
expect_no_error(gdal_addo(tif)) # internal overview
129+
expect_true(has_overviews(tif))
130+
expect_true(is.na(has_compressed_overviews(tif))) # no overview file
131+
132+
# Clean overviews
133+
expect_no_error(gdal_addo(tif, clean = TRUE))
134+
expect_false(has_overviews(tif))
135+
136+
# Overviews in separate file
137+
expect_no_error(gdal_addo(tif, read_only = TRUE))
138+
expect_false(has_compressed_overviews(tif)) # uncompressed overview file
139+
140+
# Clean overviews
141+
expect_no_error(gdal_addo(tif, clean = TRUE))
142+
expect_false(has_overviews(tif))
143+
144+
# Compression via config_options works
145+
expect_no_error(gdal_addo(tif, read_only = TRUE,
146+
config_options = c(COMPRESS_OVERVIEW="LZW")))
147+
expect_true(has_compressed_overviews(tif))
148+
149+
})

0 commit comments

Comments
 (0)