r-spatialecology/landscapemetrics

Add zero for classes that that are present in landscape but not sampling plot

Closed this issue · 4 comments

Currently, the sample_lsm() function only returns rows for classes that are present in the sample plot. So if a class is present in the landscape, but not in a specific sample plot, simply no value is returned for this class. Maybe it makes sense to add an argument that automatically adds a 0 for these classes. On example where this makes sense would be the proportion of landcover.

@mhesselbarth what do you propose to do for sample plots with NAs (partialy or completely covered by NA)?

Good question, probably need to think a bit more about this. Maybe if only NA is present, return NA as well. And if some cells are NA, that shouldn't change anything to how metrics are calculated anyways.

Please have a look at cbfde3f on the branch sample_lsm_update.

sample_lsm() now has an argument all_classes = TRUE that allows returning class level metrics for all classes in the landscape for each sample plot (by adding NA as value if needed).

In the following example, class 1 is not present in both sample plots. So for both class-level metrics an NA row is added. I also added a landscape-level metric to show that different levels can still be combined.

library(landscapemetrics)

sample_points <- matrix(c(25, 15, 5, 25), ncol = 2, byrow = TRUE)

result_sample <- sample_lsm(landscape, y = sample_points,
                            size = 5, shape = "circle",
                            what = c("lsm_c_ca", "lsm_c_pland", "lsm_l_ta"),
                            all_classes = TRUE)
#> Warning: Please use 'check_landscape()' to ensure the input data is valid.

result_sample
#> # A tibble: 14 x 8
#>    layer level     class    id metric   value plot_id percentage_inside
#>    <int> <chr>     <int> <int> <chr>    <dbl>   <int>             <dbl>
#>  1     1 class         1    NA ca     NA            1              102.
#>  2     1 class         2    NA ca      0.0022       1              102.
#>  3     1 class         3    NA ca      0.0058       1              102.
#>  4     1 class         1    NA pland  NA            1              102.
#>  5     1 class         2    NA pland  27.5          1              102.
#>  6     1 class         3    NA pland  72.5          1              102.
#>  7     1 landscape    NA    NA ta      0.008        1              102.
#>  8     1 class         1    NA ca     NA            2              102.
#>  9     1 class         2    NA ca      0.0033       2              102.
#> 10     1 class         3    NA ca      0.0047       2              102.
#> 11     1 class         1    NA pland  NA            2              102.
#> 12     1 class         2    NA pland  41.2          2              102.
#> 13     1 class         3    NA pland  58.8          2              102.
#> 14     1 landscape    NA    NA ta      0.008        2              102.

Created on 2020-05-13 by the reprex package (v0.3.0)

Looks good!