Skip to content

Commit 22b3c31

Browse files
committed
Use output file extension for temp masked raster, require extension for spatraster outputs, and add tests for format-preservation and missing-extension error
1 parent 4758513 commit 22b3c31

3 files changed

Lines changed: 221 additions & 3 deletions

File tree

R/stream_grid_raster.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ stream_grid_raster_terra <- function(
195195
message("Applying mask (clipping to input)...")
196196
}
197197

198-
# We need a temp file for the masked output if we want to overwrite 'dsn'
199-
temp_masked <- tempfile(fileext = ".tif")
198+
# Use the same extension as the output file to preserve format
199+
output_ext <- tools::file_ext(dsn)
200+
temp_masked <- tempfile(
201+
fileext = if (nzchar(output_ext)) paste0(".", output_ext) else ""
202+
)
200203

201204
# Load the raster we just created
202205
r_raw <- terra::rast(dsn)

R/utils.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,15 @@ validate_disk_compatibility <- function(output_type, dsn) {
389389
)
390390
}
391391

392-
# 2. Prevent SpatRaster -> Non-Raster Formats
392+
# 2. Prevent Raster -&gt; Missing Extension
393+
if (is_raster && !nzchar(ext)) {
394+
stop(
395+
"Output type 'spatraster' requires a file extension to determine the format.\n Please specify a raster format extension, e.g., 'output.tif', 'output.nc', or 'output.kea'.",
396+
call. = FALSE
397+
)
398+
}
399+
400+
# 3. Prevent SpatRaster -> Non-Raster Formats
393401
if (is_raster && !is_raster_format) {
394402
stop(
395403
sprintf(

tests/testthat/test-inspire_grid-raster-streaming.R

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ test_that("inspire_grid streaming errors for HDF5 with include_rat", {
218218
)
219219
})
220220

221+
test_that("inspire_grid streaming errors for missing file extension", {
222+
skip_if_not_installed("terra")
223+
skip_if_not_installed("sf")
224+
225+
expect_error(
226+
inspire_grid(
227+
c(0, 0, 1000, 1000),
228+
100,
229+
output_type = "spatraster",
230+
crs = 3035,
231+
dsn = "testfile_no_extension"
232+
),
233+
"requires a file extension"
234+
)
235+
})
236+
221237
test_that("RAT is filtered to exclude NA cells after clipping", {
222238
skip_if_not_installed("terra")
223239
skip_if_not_installed("sf")
@@ -267,3 +283,194 @@ test_that("RAT is filtered to exclude NA cells after clipping", {
267283
# Verify there ARE some NA cells (to confirm clipping worked)
268284
expect_true(sum(is.na(raster_values)) > 0)
269285
})
286+
287+
test_that("File format is preserved after clipping for GeoTIFF", {
288+
skip_if_not_installed("terra")
289+
skip_if_not_installed("sf")
290+
291+
# Create test polygon for clipping
292+
test_poly <- sf::st_as_sfc(
293+
"POLYGON((4000000 2800000, 4040000 2800000, 4040000 2840000, 4000000 2840000, 4000000 2800000))"
294+
)
295+
test_poly <- sf::st_set_crs(test_poly, 3035)
296+
297+
tf <- tempfile(fileext = ".tif")
298+
299+
inspire_grid(
300+
test_poly,
301+
cellsize_m = 10000,
302+
output_type = "spatraster",
303+
dsn = tf,
304+
clip_to_input = TRUE,
305+
quiet = TRUE
306+
)
307+
308+
# Verify file exists and can be read
309+
expect_true(file.exists(tf))
310+
r <- terra::rast(tf)
311+
expect_s4_class(r, "SpatRaster")
312+
313+
# Verify format by checking GDAL driver
314+
info <- terra::describe(tf)
315+
expect_true(any(grepl("GTiff", info, ignore.case = TRUE)))
316+
})
317+
318+
test_that("File format is preserved after clipping for .tiff extension", {
319+
skip_if_not_installed("terra")
320+
skip_if_not_installed("sf")
321+
322+
test_poly <- sf::st_as_sfc(
323+
"POLYGON((4000000 2800000, 4040000 2800000, 4040000 2840000, 4000000 2840000, 4000000 2800000))"
324+
)
325+
test_poly <- sf::st_set_crs(test_poly, 3035)
326+
327+
tf <- tempfile(fileext = ".tiff")
328+
329+
inspire_grid(
330+
test_poly,
331+
cellsize_m = 10000,
332+
output_type = "spatraster",
333+
dsn = tf,
334+
clip_to_input = TRUE,
335+
quiet = TRUE
336+
)
337+
338+
# Verify file exists with correct extension
339+
expect_true(file.exists(tf))
340+
expect_true(grepl("\\.tiff$", tf))
341+
r <- terra::rast(tf)
342+
expect_s4_class(r, "SpatRaster")
343+
})
344+
345+
test_that("File format is preserved after clipping for NetCDF", {
346+
skip_if_not_installed("terra")
347+
skip_if_not_installed("sf")
348+
349+
# Check for NetCDF driver
350+
drivers <- terra::gdal(lib = "drivers")
351+
has_netcdf <- if (is.data.frame(drivers)) {
352+
"netCDF" %in% drivers$name
353+
} else {
354+
any(grepl("netCDF", drivers))
355+
}
356+
357+
if (!has_netcdf) {
358+
skip("NetCDF driver not available in terra/GDAL")
359+
}
360+
361+
test_poly <- sf::st_as_sfc(
362+
"POLYGON((4000000 2800000, 4040000 2800000, 4040000 2840000, 4000000 2840000, 4000000 2800000))"
363+
)
364+
test_poly <- sf::st_set_crs(test_poly, 3035)
365+
366+
tf <- tempfile(fileext = ".nc")
367+
368+
inspire_grid(
369+
test_poly,
370+
cellsize_m = 10000,
371+
output_type = "spatraster",
372+
dsn = tf,
373+
clip_to_input = TRUE,
374+
quiet = TRUE
375+
)
376+
377+
# Verify file exists and can be read
378+
expect_true(file.exists(tf))
379+
r <- terra::rast(tf)
380+
expect_s4_class(r, "SpatRaster")
381+
382+
# Verify format by checking GDAL driver
383+
info <- terra::describe(tf)
384+
expect_true(any(grepl("netCDF", info, ignore.case = TRUE)))
385+
})
386+
387+
test_that("File format is preserved after clipping for KEA", {
388+
skip_if_not_installed("terra")
389+
skip_if_not_installed("sf")
390+
391+
# Check for KEA driver
392+
drivers <- terra::gdal(lib = "drivers")
393+
has_kea <- if (is.data.frame(drivers)) {
394+
"KEA" %in% drivers$name
395+
} else {
396+
any(grepl("KEA", drivers))
397+
}
398+
399+
if (!has_kea) {
400+
skip("KEA driver not available in terra/GDAL")
401+
}
402+
403+
test_poly <- sf::st_as_sfc(
404+
"POLYGON((4000000 2800000, 4040000 2800000, 4040000 2840000, 4000000 2840000, 4000000 2800000))"
405+
)
406+
test_poly <- sf::st_set_crs(test_poly, 3035)
407+
408+
tf <- tempfile(fileext = ".kea")
409+
410+
inspire_grid(
411+
test_poly,
412+
cellsize_m = 10000,
413+
output_type = "spatraster",
414+
dsn = tf,
415+
clip_to_input = TRUE,
416+
quiet = TRUE
417+
)
418+
419+
# Verify file exists and can be read
420+
expect_true(file.exists(tf))
421+
r <- terra::rast(tf)
422+
expect_s4_class(r, "SpatRaster")
423+
424+
# Verify format by checking GDAL driver
425+
info <- terra::describe(tf)
426+
expect_true(any(grepl("KEA", info, ignore.case = TRUE)))
427+
})
428+
429+
test_that("Format preservation works with both clipping and RAT", {
430+
skip_if_not_installed("terra")
431+
skip_if_not_installed("sf")
432+
433+
# Check for NetCDF driver
434+
drivers <- terra::gdal(lib = "drivers")
435+
has_netcdf <- if (is.data.frame(drivers)) {
436+
"netCDF" %in% drivers$name
437+
} else {
438+
any(grepl("netCDF", drivers))
439+
}
440+
441+
if (!has_netcdf) {
442+
skip("NetCDF driver not available in terra/GDAL")
443+
}
444+
445+
test_poly <- sf::st_as_sfc(
446+
"POLYGON((4000000 2800000, 4040000 2800000, 4040000 2840000, 4000000 2840000, 4000000 2800000))"
447+
)
448+
test_poly <- sf::st_set_crs(test_poly, 3035)
449+
450+
tf <- tempfile(fileext = ".nc")
451+
452+
inspire_grid(
453+
test_poly,
454+
cellsize_m = 10000,
455+
output_type = "spatraster",
456+
dsn = tf,
457+
clip_to_input = TRUE,
458+
include_rat = TRUE,
459+
quiet = TRUE
460+
)
461+
462+
# Verify file exists and can be read
463+
expect_true(file.exists(tf))
464+
r <- terra::rast(tf)
465+
expect_s4_class(r, "SpatRaster")
466+
467+
# Verify format
468+
info <- terra::describe(tf)
469+
expect_true(any(grepl("netCDF", info, ignore.case = TRUE)))
470+
471+
# Verify RAT exists
472+
cats <- terra::cats(r)
473+
expect_true(length(cats) >= 1)
474+
expect_true(!is.null(cats[[1]]))
475+
expect_true(nrow(cats[[1]]) > 0)
476+
})

0 commit comments

Comments
 (0)