blockCV icon indicating copy to clipboard operation
blockCV copied to clipboard

cv_spatial fails with misleading message when r doesn't overlap

Open AMBarbosa opened this issue 11 months ago • 1 comments

Using one of the examples from the function help file, and an example raster layer:

points <- read.csv(system.file("extdata/", "species.csv", package = "blockCV"))
pa_data <- sf::st_as_sf(points, coords = c("x", "y"), crs = 7845)

library(terra)
r <- rast(ext(pa_data), crs = crs(pa_data))
values(r) <- 1:ncell(r)
plot(r)

sb1 <- cv_spatial(x = pa_data,
                  column = "occ",
                  size = 450000,
                  k = 5,
                  selection = "random",
                  iteration = 50,
                  r = r)

OK. However, if the user inadvertently provides a raster layer that does not overlap the points, there's a misleading error message which complains instead about the block size:

r2 <- shift(r, dx = 1e100, dy = 1e100)

sb2 <- cv_spatial(x = pa_data,
                  column = "occ",
                  size = 450000,
                  k = 5,
                  selection = "random",
                  iteration = 50,
                  r = r2)

  |                                                              |   0%Could not create spatial blocks! possibly because of using a very small block size.
Remember, size is in metres not the unit of the CRS.
Error in .make_blocks(x_obj = if (is.null(r)) x else r, blocksize = size,  : 
  object 'fishnet_poly' not found

The function could just check if 'r' overlaps the study region, and if not, just ignore 'r' with a warning message; but still produce the spatial blocks, which should not depend on 'r' at all.

It would also be helpful if these messages were preceded by a line break \n, so that they don't glue to the progress bar.

Cheers!

AMBarbosa avatar Feb 21 '25 12:02 AMBarbosa

Hi @AMBarbosa, thank you for the issue and suggesstion. I'll add a specific error for this.

rvalavi avatar Feb 24 '25 21:02 rvalavi

Hi @AMBarbosa,

I added a couple of more checks to avoid such issues. I hope these helps more. I'll update the changes in the next couple of days.

library(blockCV)
library(terra)

points <- read.csv(system.file("extdata/", "species.csv", package = "blockCV"))
pa_data <- sf::st_as_sf(points, coords = c("x", "y"), crs = 7845)

r <- rast(ext(pa_data), crs = crs(pa_data))
values(r) <- 1:ncell(r)

# invalid extent
r2 <- shift(r, dx = 1e100, dy = 1e100)

sb1 <- cv_spatial(
    x = pa_data,
    column = "occ",
    r = r2,
    size = 450000,
    k = 5,
    selection = "random",
    iteration = 50
)
#> Error in .check_ext(r): Invalid raster extent: values are non-finite or out of range.

# smaller raster bound
r3 <- rast(ext(pa_data) - 1000, crs = crs(pa_data))
values(r3) <- 1:ncell(r3)

sb2 <- cv_spatial(
    x = pa_data,
    column = "occ",
    r = r3,
    size = 450000,
    k = 5,
    selection = "random",
    iteration = 50
)
#> Error in .check_within(x, r): The x's bounding box lies outside the raster extent.

rvalavi avatar Jun 23 '25 01:06 rvalavi

Hi @AMBarbosa, thanks again for raising the issue. Now, closing the isse. Please open new ones if there was any other issues.

rvalavi avatar Jun 23 '25 05:06 rvalavi