Skip to content

Commit b75b824

Browse files
committed
Refuse windowed and subset in coregister
1 parent 27e39e0 commit b75b824

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# HSItools (development version)
22

3+
- `hsi_coregister()` now errors when `x` is a window, layer subset or combination of files. It warps the file on disk, so these inputs previously produced silently misregistered output.
4+
35
- `hsi_coregister()` now returns a readable raster; previously the returned object pointed at a deleted temporary file. It also warps directly to `filename` without an intermediate copy.
46

57
# HSItools 0.5.3

R/hsi_coregister.R

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#'
33
#' @family HSI Co-registration
44
#'
5-
#' @param x A [`SpatRaster`][terra::SpatRaster-class] to warp. Must have a
6-
#' file source on disk.
5+
#' @param x A [`SpatRaster`][terra::SpatRaster-class] to warp. Must be a
6+
#' whole file on disk, not a window, layer subset or combination of files.
77
#' @param y A [`SpatRaster`][terra::SpatRaster-class] defining the output grid.
88
#' Output extent, resolution, and dimensions are taken from this raster.
99
#' @param gcp A [data.frame] or [tibble][tibble::tibble] of matched GCPs from
@@ -129,6 +129,26 @@ hsi_coregister <- function(
129129
)
130130
}
131131

132+
# Source must be the whole file: GDAL warps the file, not a view of it
133+
x_sources <- terra::sources(x, bands = TRUE)
134+
135+
x_file <- terra::rast(source_path)
136+
137+
is_whole_file <- length(unique(x_sources$source)) == 1 &&
138+
identical(as.integer(x_sources$bands), seq_len(terra::nlyr(x_file))) &&
139+
identical(as.vector(terra::ext(x)), as.vector(terra::ext(x_file)))
140+
141+
if (!is_whole_file) {
142+
cli::cli_abort(
143+
c(
144+
"{.arg x} is not a whole file on disk.",
145+
"i" = "Windows, layer subsets and combined rasters cannot be warped.",
146+
"i" = "Write it to disk first with {.code terra::writeRaster()}."
147+
),
148+
class = "hsitools_error"
149+
)
150+
}
151+
132152
# Temp VRT for GCP embedding
133153
vrt_path <- withr::local_tempfile(fileext = ".vrt")
134154

man/hsi_coregister.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-hsi_coregister.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,29 @@ test_that("hsi_coregister errors when x has no file source", {
232232
)
233233
})
234234

235+
test_that("hsi_coregister errors when x is a window or layer subset of its file", {
236+
# Views of the file on disk, derived from copies so the fixture is untouched
237+
windowed <- terra::subset(
238+
test_reflectance,
239+
seq_len(terra::nlyr(test_reflectance))
240+
)
241+
terra::window(windowed) <- terra::ext(test_reflectance) -
242+
terra::res(test_reflectance)
243+
244+
layer_subset <- terra::subset(test_reflectance, 1:3)
245+
246+
expect_error(
247+
hsi_coregister(windowed, test_target, test_gcp),
248+
"not a whole file",
249+
class = "hsitools_error"
250+
)
251+
expect_error(
252+
hsi_coregister(layer_subset, test_target, test_gcp),
253+
"not a whole file",
254+
class = "hsitools_error"
255+
)
256+
})
257+
235258
test_that("hsi_coregister errors with invalid method", {
236259
expect_error(
237260
hsi_coregister(test_reflectance, test_target, test_gcp, method = "invalid"),

0 commit comments

Comments
 (0)