Return ID of nearest neigbhour
Closed this issue · 2 comments
As discussed in #181 it would be cool if get_nearestneighbour()
returns the focal ID and the neighbour ID.
@frederikvand
Okay, if you install the development version from the corresponding branch ("get_nearestneighbour"; see code below), there now is a new argument which allows you to return also the ID of the neighbouring patch. Because the function needs an already labelled landscape (using get_patches()
), the patch IDs are different to all other lsm_
functions. They go from 1 to n for each class and are not continuously increasing for the whole landscape. Not really happy about this, but I don't see how to improve that at the moment.
library(landscapemetrics)
library(raster)
# # install development version
# remotes::install_github(repo = "r-spatialecology/landscapemetrics",
# ref = "get_nearestneighbour")
# get e.g. patches of class 2
class_2 <- get_patches(landscape)[[2]]
# return distance and id
# (watch out, different labelling to lsm_ functions and several nearest neighbours
# for some patches)
get_nearestneighbour(class_2, return_id = TRUE)
#> # A tibble: 16 x 4
#> layer id dist id_neighbour
#> <int> <dbl> <dbl> <dbl>
#> 1 1 1 4.47 5
#> 2 1 2 4.47 3
#> 3 1 3 3.16 6
#> 4 1 4 2 12
#> 5 1 4 2 6
#> 6 1 5 3 7
#> 7 1 6 2 4
#> 8 1 7 3 5
#> 9 1 7 3 8
#> 10 1 8 3 7
#> 11 1 9 3 10
#> 12 1 10 2.83 4
#> 13 1 11 5.83 14
#> 14 1 12 2 4
#> 15 1 13 2.83 4
#> 16 1 14 3.16 4
Created on 2020-06-12 by the reprex package (v0.3.0)
Will merge to master and close issue. Please reopen if needed.