landscape <- terra::rast(landscapemetrics::landscape)
# calculate full adjacency matrix
get_adjacencies(landscape, 4)
# equivalent with the terra package:
adjacencies <- terra::adjacent(landscape, 1:terra::ncell(landscape), "rook", pairs = TRUE)
table(terra::values(landscape, mat = FALSE)[adjacencies[,1]],
terra::values(landscape, mat = FALSE)[adjacencies[,2]])
# count diagonal neighbour adjacencies
diagonal_matrix <- matrix(c(1, NA, 1,
NA, 0, NA,
1, NA, 1), 3, 3, byrow = TRUE)
get_adjacencies(landscape, diagonal_matrix)
Run the code above in your browser using DataLab