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

extract() not working as expected with layers #1332

Closed
ewanwakefield opened this issue Nov 1, 2023 · 1 comment
Closed

extract() not working as expected with layers #1332

ewanwakefield opened this issue Nov 1, 2023 · 1 comment

Comments

@ewanwakefield
Copy link

Hi,

Not sure if this is a bug or a misunderstanding of the way the function works. In short, the issue is that when I specify layer as numeric in the extract function, values from the layer n-1 are extracted. Here's an example. I'm using Windows and version 1.7.46 of terra.

Thanks,

Ewan


library(terra)

r <- rast(ncol=4, nrow=4, nlyr=3, extent=ext(-2, 2, -2, 2))

values(r[[1]]) <- 1
values(r[[2]]) <- 2
values(r[[3]]) <- 3

xy <- data.frame(x=-1:1,y=-1:1)

extract(r, xy[,1:2]) # Behaves as expected
extract(r, xy[,1:2], ID=F, layer=c(1,1,1)) # Returns error
extract(r, xy[,1:2], ID=F, layer=c(2,2,2)) # Extracts from layer 1 not 2!
extract(r, xy[,1:2], ID=F, layer=c(3,3,3)) # Extracts from layer 2 not 3!
extract(r, xy[,1:2], ID=F, layer=c(4,4,4)) # Does not extract from layer 3!

@rhijmans
Copy link
Member

Thank you very much for reporting this. I now get:

library(terra)
#terra 1.7.60
r <- rast(ncol=4, nrow=4, nlyr=3, extent=ext(-2, 2, -2, 2))
values(r) <- rbind(11:13)
xy <- data.frame(x=-1:1,y=-1:1)
extract(r, xy)
#  ID lyr.1 lyr.2 lyr.3
#1  1    11    12    13
#2  2    11    12    13
#3  3    11    12    13
extract(r, xy, ID=FALSE, layer=1:3)
#  layer value
#1 lyr.1    11
#2 lyr.2    12
#3 lyr.3    13
extract(r, xy, ID=TRUE, layer=1:3)
#  ID layer value
#1  1 lyr.1    11
#2  2 lyr.2    12
#3  3 lyr.3    13

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