r-spatialecology/landscapemetrics

`scale_sample()`: allow irregular buffer size vector and support `sf` as `y` parameter

Closed this issue · 6 comments

Hello,

running some examples for scale_sample() I wanted to do to two things:

  • use a sf point object as y parameter
  • set an irregular vector of buffer sizes. This is particularly interesting (in my opinion) when there is a lot of points and the buffers must go from a small extent (e.g. 100 m) to a very large extent (e.g. 50 km), especially if the resolution of the raster is fine (e.g. 10 m). This can make the computation super slow.
    (even though from a methodological perspective I understand it might be interesting to have a regular vector of buffer sizes)

So I tried a few modifications.

  1. For the sf support, as I understand the internal function construct_buffers() already allows one to have a sf input. So all I did here in scale_sample() was to include sf as a class inherited by the input vector. Seems to work, even though some warnings or error raise might be important (e.g. if the sf object is not of type POINT or MULTIPOINT).

  2. For the buffer size, my suggestion is to keep the parameter max_size = NULL by default, in which case the vector of buffer sizes will be given by the parameter size. Then is is possible to set it as the user wants, e.g. size = c(10, 50, 200, 500, 1000). It is also possible to set a vector in which the increment is not necessarily the minimum buffer size, e.g. size = seq(100, 2000, 300).

  3. Testing

Below I show an example. You can test it from the branch "multibuffer" from my fork of the repo: https://github.com/bniebuhr/landscapemetrics/tree/multibuffer. I can also make changes and then open a PR.

# library(devtools)
# install_github("bniebuhr/landscapemetrics", ref = "multibuffer")
library(landscapemetrics)
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

my_points = matrix(c(1265000, 1250000, 1255000, 1257000),
                   ncol = 2, byrow = TRUE) %>%
    as.data.frame() %>%
    sf::st_as_sf(coords = c(1,2))

scale_sample(landscape = augusta_nlcd, y = my_points,
             size = c(500, 1000, 2000, 3000),
             max_size = NULL, what = c("lsm_c_enn_mn"))
#> Warning: Class 31: ENN = NA for class with only 1 patch.
#> Warning: Class 90: ENN = NA for class with only 1 patch.
#> Warning: Class 82: ENN = NA for class with only 1 patch.
#> # A tibble: 90 x 9
#>    layer level class    id metric  value  size plot_id percentage_inside
#>    <int> <chr> <int> <int> <chr>   <dbl> <dbl>   <int>             <dbl>
#>  1     1 class    11    NA enn_mn 2095.   2000       1              99.5
#>  2     1 class    11    NA enn_mn  627.   3000       1             100  
#>  3     1 class    21    NA enn_mn   60     500       1              98.0
#>  4     1 class    21    NA enn_mn   79.1  1000       1             101. 
#>  5     1 class    21    NA enn_mn   91.2  2000       1              99.5
#>  6     1 class    21    NA enn_mn   74.4  3000       1             100  
#>  7     1 class    22    NA enn_mn   89.1   500       1              98.0
#>  8     1 class    22    NA enn_mn   75.4  1000       1             101. 
#>  9     1 class    22    NA enn_mn   83.1  2000       1              99.5
#> 10     1 class    22    NA enn_mn   88.4  3000       1             100  
#> # ... with 80 more rows

Created on 2022-02-17 by the reprex package (v2.0.1)

Hi @mhesselbarth -- what do you think? Looks fine by me.

Looks good to me, but I think @marcosci write the scale_sample function, so I don't know all the details anymore.

Puh, this is ... quite far in the past 😄 If it works and doesn't break anything ... can't be worse than what I did back then :)

@bniebuhr can you prepare a PR?

Hi @bniebuhr, are you still interested in this? Can you prepare a new pull request?

Function was deprecated quite a while ago given that the exact same behavior can be achieved by constructing the sampling plots in sf beforehand and supply to sample_lsm