Skip to content

Commit 24e3f20

Browse files
committed
Filter RAT to exclude NA/masked raster cells after clipping; add unit test verifying RAT only contains non-NA values
1 parent 9b23934 commit 24e3f20

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

R/stream_grid_raster.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,24 @@ stream_grid_raster_terra <- function(
265265
rat_df <- data.frame(Value = seq_len(n_cells), GRD_ID = id_long)
266266
}
267267

268+
# Filter RAT to exclude entries for NA cells (after clipping)
269+
# This ensures the RAT only contains entries for valid raster cells
270+
raster_values <- terra::values(r_final)
271+
valid_values <- unique(raster_values[!is.na(raster_values)])
272+
if (length(valid_values) < n_cells) {
273+
rat_df <- rat_df[rat_df$Value %in% valid_values, ]
274+
if (!quiet) {
275+
message(
276+
" Filtered RAT: ",
277+
nrow(rat_df),
278+
" entries ",
279+
"(removed ",
280+
n_cells - nrow(rat_df),
281+
" entries for masked cells)"
282+
)
283+
}
284+
}
285+
268286
# Format-specific persistence
269287
if (ext %in% c("nc", "kea")) {
270288
# NetCDF/KEA: Native support via levels() + writeRaster

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,53 @@ test_that("inspire_grid streaming errors for HDF5 with include_rat", {
188188
"supported"
189189
)
190190
})
191+
192+
test_that("RAT is filtered to exclude NA cells after clipping", {
193+
skip_if_not_installed("terra")
194+
skip_if_not_installed("sf")
195+
196+
# Create an L-shaped polygon that will force some cells to be masked
197+
test_poly <- sf::st_as_sfc(
198+
"POLYGON((4000000 2800000, 4040000 2800000, 4040000 2840000, 4020000 2840000, 4020000 2820000, 4000000 2820000, 4000000 2800000))"
199+
)
200+
test_poly <- sf::st_set_crs(test_poly, 3035)
201+
202+
tf_clipped <- tempfile(fileext = ".tif")
203+
204+
# Generate grid with clipping and RAT
205+
expect_message(
206+
inspire_grid(
207+
test_poly,
208+
cellsize_m = 10000,
209+
output_type = "spatraster",
210+
dsn = tf_clipped,
211+
include_rat = TRUE,
212+
clip_to_input = TRUE,
213+
quiet = FALSE
214+
),
215+
"Filtered RAT"
216+
)
217+
218+
# Load the raster
219+
r <- terra::rast(tf_clipped)
220+
221+
# Get the RAT
222+
rat <- terra::levels(r)[[1]]
223+
224+
# Get actual non-NA cell values
225+
raster_values <- terra::values(r)
226+
non_na_values <- unique(raster_values[!is.na(raster_values)])
227+
228+
# Key assertion: RAT should only contain entries for non-NA cells
229+
expect_equal(nrow(rat), length(non_na_values))
230+
231+
# Verify all RAT entries correspond to actual cell values
232+
expect_true(all(rat$Value %in% non_na_values))
233+
234+
# Verify no orphaned entries (RAT values not in raster)
235+
orphaned <- setdiff(rat$Value, non_na_values)
236+
expect_equal(length(orphaned), 0)
237+
238+
# Verify there ARE some NA cells (to confirm clipping worked)
239+
expect_true(sum(is.na(raster_values)) > 0)
240+
})

0 commit comments

Comments
 (0)