Skip to contents

Centroid of patches

Usage

get_centroids(
  landscape,
  directions = 8,
  cell_center = FALSE,
  return_vec = FALSE,
  verbose = TRUE
)

Arguments

landscape

A categorical raster object: SpatRaster; Raster* Layer, Stack, Brick; stars or a list of SpatRasters.

directions

The number of directions in which patches should be connected: 4 (rook's case) or 8 (queen's case).

cell_center

If true, the coordinates of the centroid are forced to be a cell center within the patch.

return_vec

If true, a sf object is returned.

verbose

Print warning messages

Details

Get the coordinates of the centroid of each patch. The centroid is by default defined as the mean location of all cell centers. To force the centroid to be located within each patch, use the cell_center argument. In this case, the centroid is defined as the cell center that is the closest to the mean location.

Examples

# get centroid location
landscape <- terra::rast(landscapemetrics::landscape)
get_centroids(landscape)
#> Warning: Double values will be converted to integer.
#> # A tibble: 28 × 6
#>    layer level class    id     x      y
#>    <int> <chr> <int> <int> <dbl>  <dbl>
#>  1     1 patch     1     1  0.5  29.5  
#>  2     1 patch     1     2  0.7  20.9  
#>  3     1 patch     1     3  7.86 12.0  
#>  4     1 patch     1     4  5.5   0.5  
#>  5     1 patch     1     5 11.5   3.5  
#>  6     1 patch     1     6 19.2  23.2  
#>  7     1 patch     1     7 16.9   8.19 
#>  8     1 patch     1     8 20.5   0.833
#>  9     1 patch     1     9 26.1   2.3  
#> 10     1 patch     2    10  2.65 25.1  
#> # ℹ 18 more rows