Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cover with values=vector #1741

Closed
tghoward opened this issue Feb 13, 2025 · 3 comments
Closed

cover with values=vector #1741

tghoward opened this issue Feb 13, 2025 · 3 comments

Comments

@tghoward
Copy link

I'm trying to use cover and finding that I may not be able to use a vector in the values argument. Expanding the example from the help files:

library(terra)
# setup (example from cover help page)
r1 <- r2 <- rast(ncols=36, nrows=18)
values(r1) <- 1:ncell(r1)
values(r2) <- runif(ncell(r2))
# reclass to integers
m <- c(-Inf, 0.5, NA,
       0.5,0.7,1,
       0.7,0.9,2,
       0.9,+Inf,3)
rclmat <- matrix(m, ncol = 3, byrow = TRUE)
r2 <- classify(r2, rclmat)
# this appears to work -- it looks like only cells in r2 with 
# a value of 2 are changed to r1 values
r3 <- cover(r2, r1, values = c(2))
all.equal(r2,r3)
##[1] "Mean relative difference: 0.9937267"

# this doesn't appear to change anything
r4 <- cover(r2, r1, values = c(1,2))
all.equal(r2,r4)
##[1] TRUE

Is this expected behavior? Must I first set all cells I want to change to a single value? I certainly could be missing something!
Thanks.

@tghoward
Copy link
Author

Forgot session info:

sessionInfo()
R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8
[2] LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] terra_1.7-78

loaded via a namespace (and not attached):
[1] compiler_4.4.2 tools_4.4.2 rstudioapi_0.16.0 Rcpp_1.0.13
[5] codetools_0.2-20

@rhijmans
Copy link
Member

Thank you for reporting this bug. Fixed now:

library(terra)
r1 <- r2 <- rast(ncols=6, nrows=6)
r1 <- init(r1, "col")
r2 <- init(r2, "row")

r3 <- cover(r2, r1, values = 3:4)
plot(r3)
plot(r3 == r1)

@tghoward
Copy link
Author

Thank you! Fabulous!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants