Skip to contents

Euclidean distance to nearest neighbour

Usage

get_nearestneighbour(landscape, return_id = FALSE)

Arguments

landscape

SpatRaster or matrix (with x,y,id columns).

return_id

If TRUE, also the patch ID of the nearest neighbour is returned.

Details

Fast and memory safe Rcpp implementation for calculating the minimum Euclidean distances to the nearest patch of the same class in a raster or matrix. All patches need an unique ID (see get_patches). Please be aware that the patch ID is not identical to the patch ID of all metric functions (lsm_). If return_ID = TRUE, for some focal patches several nearest neighbour patches might be returned.

Because the metric is based on distances or areas please make sure your data is valid using check_landscape.

References

Based on RCpp code of Florian Privé florian.prive.21@gmail.com

Examples

# get patches for class 1
landscape <- terra::rast(landscapemetrics::landscape)
class_1 <- get_patches(landscape, class = 2)[[1]][[1]]

# calculate the distance between patches
get_nearestneighbour(class_1)
#> # A tibble: 13 × 3
#>    layer    id  dist
#>    <int> <dbl> <dbl>
#>  1     1    10  4.47
#>  2     1    11  5.83
#>  3     1    12  2.83
#>  4     1    13  4.12
#>  5     1    14  3   
#>  6     1    15  3   
#>  7     1    16  3.16
#>  8     1    17  3   
#>  9     1    18  3   
#> 10     1    19  2.83
#> 11     1    20  5.83
#> 12     1    21  2.83
#> 13     1    22  3   
get_nearestneighbour(class_1, return_id = TRUE)
#> # A tibble: 15 × 4
#>    layer    id  dist id_neighbour
#>    <int> <dbl> <dbl>        <dbl>
#>  1     1    10  4.47           14
#>  2     1    11  5.83           13
#>  3     1    12  2.83           19
#>  4     1    13  4.12           12
#>  5     1    14  3              15
#>  6     1    15  3              14
#>  7     1    16  3.16           15
#>  8     1    17  3              18
#>  9     1    18  3              12
#> 10     1    18  3              17
#> 11     1    19  2.83           12
#> 12     1    19  2.83           21
#> 13     1    20  5.83           22
#> 14     1    21  2.83           19
#> 15     1    22  3              19