knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(landscapemetrics) library(terra) library(dplyr) # internal data needs to be read landscape <- terra::rast(landscapemetrics::landscape) augusta_nlcd <- terra::rast(landscapemetrics::augusta_nlcd) podlasie_ccilc <- terra::rast(landscapemetrics::podlasie_ccilc)
Most functions in landscapemetrics start with lsm_
(for landscapemetrics).
The second part of the name specifies the level (patch - p
, class - c
or landscape - l
).
The last part of the function name is the abbreviation of the corresponding metric (e.g., enn
for the euclidean nearest-neighbor distance):
# general structure lsm_"level"_"metric" # Patch level ## lsm_p_"metric" lsm_p_enn() # Class level ## lsm_c_"metric" lsm_c_enn() # Landscape level ## lsm_p_"metric" lsm_l_enn()
All lsm_
functions return an identically structured tibble:
| layer | level | class | id | metric | value | ------------- | ------------- | ------------- | ------------- | ------------- | ------------- | | 1 | patch | 1 | 1 | landscape metric | x | | 1 | class | 1 | NA | landscape metric | x | | 1 | landscape | NA | NA | landscape metric | x |
Before using landscapemetrics and calculating landscape metrics in general, it makes sense to check your landscape.
If your landscape has some properties that restrict the calculation or interpretation of landscape metrics, that should be detected with check_landscape
:
# import raster # for local file: rast("pathtoyourraster/raster.asc") # ... or any other raster file type, geotiff, ... # Check your landscape check_landscape(landscape) # because CRS is unknown, not clear check_landscape(podlasie_ccilc) # wrong units check_landscape(augusta_nlcd) # everything is ok
The requirements to calculate meaningful landscape metrics are:
If you are sure that your landscape is suitable for the calculation of landscape metrics, landscapemetrics makes this quite easy:
# import raster # for local file: rast("pathtoyourraster/raster.asc") # ... or any other raster file type, geotiff, ... # Calculate e.g. perimeter of all patches lsm_p_perim(landscape)
Every function in landscapemetrics accept data as its first argument, which makes piping a natural workflow. A possible use case is that you would load your spatial data, calculate some landscape metrics and then use the resulting tibble in further analyses.
# all patch IDs of class 2 with an ENN > 2.5 subsample_patches <- landscape |> lsm_p_enn() |> dplyr::filter(class == 2 & value > 2.5) |> dplyr::pull(id) # show results subsample_patches
To list all available metrics, just use the list_lsm()
function.
Here, you can specify, for example, a level or type of metrics.
# list all available metrics list_lsm() # list only aggregation metrics at landscape level and just return function name list_lsm(level = "landscape", type = "aggregation metric", simplify = TRUE) # you can also combine arguments and only return the function names list_lsm(level = c("patch", "landscape"), type = "core area metric", simplify = TRUE)
Every function returns a tibble
, thus combining the metrics that were selected for your research question is straightforward:
# bind results from different metric functions patch_metrics <- dplyr::bind_rows( lsm_p_cai(landscape), lsm_p_circle(landscape), lsm_p_enn(landscape) ) # look at the results patch_metrics
All metrics are abbreviated in the result tibble
.
Therefore, we provide a tibble
containing the full metric names, as well as the class of each metric (lsm_abbreviations_names
).
Using e.g. the left_join()
function of the dplyr package one could join a result tibble
and the abbreviations tibble
.
# bind results from different metric functions patch_metrics <- dplyr::bind_rows( lsm_p_cai(landscape), lsm_p_circle(landscape), lsm_p_enn(landscape) ) # look at the results patch_metrics_full_names <- dplyr::left_join(x = patch_metrics, y = lsm_abbreviations_names, by = "metric") patch_metrics_full_names
Additionally, we provide a wrapper where the desired metrics can be specified as a vector of strings.
Because all metrics regardless of the level return an identical tibble
, different levels can be mixed.
It is also possible to calculate all available metrics at a certain level using, e.g., level = "patch"
.
Additionally, similar to list_lsm()
you can also specify, e.g., a certain group of metrics.
Of course, you can also include the full names and information of all metrics using full_name = TRUE
.
# calculate certain metrics calculate_lsm(landscape, what = c("lsm_c_pland", "lsm_l_ta", "lsm_l_te")) # calculate all aggregation metrics on patch and landscape level calculate_lsm(landscape, type = "aggregation metric", level = c("patch", "landscape")) # show full information of all metrics calculate_lsm(landscape, what = c("lsm_c_pland", "lsm_l_ta", "lsm_l_te"), full_name = TRUE)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.