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

terra::adjacent does not include focal cell in the result #808

Closed
josephlewis opened this issue Sep 18, 2022 · 1 comment
Closed

terra::adjacent does not include focal cell in the result #808

josephlewis opened this issue Sep 18, 2022 · 1 comment

Comments

@josephlewis
Copy link

josephlewis commented Sep 18, 2022

Hi @rhijmans

terra::adjacent does not include the focal cell in the returned matrix. This is true when pairs is also TRUE. See raster::adjacent for comparison

library(raster)

r <- raster::raster(nrows=10, ncols=10)
raster::adjacent(r, cells=c(1, 55), directions=8, pairs=TRUE)
raster::adjacent(r, cells=c(1, 55), directions=8, pairs=TRUE, include = TRUE)

library(terra)

r <- terra::rast(nrows=10, ncols=10)
terra::adjacent(r, cells=c(1, 55), directions=8, pairs = TRUE)
terra::adjacent(r, cells=c(1, 55), directions=8, pairs = TRUE, include = TRUE)
@rhijmans
Copy link
Member

rhijmans commented Sep 18, 2022

Thanks for reporting this. We already had

library(terra)
#terra 1.6.20
r <- rast(nrows=10, ncols=10)

adjacent(r, cells=55, directions=4, pairs=F, include=F)
#   [,1] [,2] [,3] [,4]
#54   45   54   56   65

adjacent(r, cells=55, directions=4, pairs=F, include=T)
#     [,1] [,2] [,3] [,4] [,5]
#[1,]   55   45   54   56   65

adjacent(r, cells=55, directions=4, pairs=T, include=F)
#     from to
#[1,]   55 45
#[2,]   55 54
#[3,]   55 56
#[4,]   55 65

And now this one also works as expected:

adjacent(r, cells=55, directions=4, pairs=T, include=T)
#     from to
#[1,]   55 55
#[2,]   55 45
#[3,]   55 54
#[4,]   55 56
#[5,]   55 65

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