lpi calculation
Closed this issue · 3 comments
Hi there! I am trying to calculate LPI for defined squares (cellsize=1500 grids). I tried to understand lpi calculation by using sample data.
Here are the codes that I used.
my_raster = augusta_nlcd
my_grid = st_sf(geom = st_make_grid(augusta_nlcd, cellsize = 1500))
my_metric = sample_lsm(my_raster, as(my_grid_, "Spatial"), metric = "lpi")
plot(lpi)
Error 1:
"Error in plot.window(...) : sonlu 'ylim' değerleri gerekli
In addition: Warning messages:
1: In data.matrix(x) : NAs introduced by coercion
2: In data.matrix(x) : NAs introduced by coercion
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf"
my_grid= bind_cols(my_grid, my_metric)
Error 2:
Error: Argument 2 must be length 126, not 1439
Hey,
The largest patch index is simply the area of the largest patch in relation to the overall landscape either separated by class or of the total landscape (watch out: in the case of sample_lsm()
in relation to the sample buffer area).
The sample_lsm()
returns a data.frame with the corresponding results. Because you selected metric = lpi
the index is calculated on class and landscape level. This is why you get too many rows. In case you want only the landscape level, you need to further specify the level.
Then, it should be possible to combine the grid and the resulting data.frame.
Does that answer your question?
library(dplyr)
library(landscapemetrics)
library(raster)
library(sf)
my_grid <- sf::st_sf(geom = st_make_grid(augusta_nlcd, cellsize = 1500))
my_metric <- sample_lsm(landscape = augusta_nlcd,
y = as(my_grid, "Spatial"),
metric = "lpi",
level = "landscape",
return_raster = TRUE)
#> Warning: The 'perecentage_inside' is below 90% for at least one buffer.
my_metric
#> # A tibble: 126 x 9
#> layer level class id metric value plot_id percentage_insi…
#> <int> <chr> <int> <int> <chr> <dbl> <int> <dbl>
#> 1 1 land… NA NA lpi 36.4 1 100
#> 2 1 land… NA NA lpi 8.84 2 100
#> 3 1 land… NA NA lpi 15.2 3 100
#> 4 1 land… NA NA lpi 19.4 4 100
#> 5 1 land… NA NA lpi 21 5 100
#> 6 1 land… NA NA lpi 22.1 6 100
#> 7 1 land… NA NA lpi 13.9 7 100
#> 8 1 land… NA NA lpi 20.5 8 100
#> 9 1 land… NA NA lpi 25.8 9 100
#> 10 1 land… NA NA lpi 15.8 10 100
#> # … with 116 more rows, and 1 more variable: raster_sample_plots <list>
# plot(augusta_nlcd)
# plot(my_grid, add = TRUE)
#
# plot(my_metric$raster_sample_plots[[1]])
result <- dplyr::bind_cols(my_grid, dplyr::select(my_metric, -raster_sample_plots))
result
#> Simple feature collection with 126 features and 8 fields
#> geometry type: POLYGON
#> dimension: XY
#> bbox: xmin: 1249635 ymin: 1246815 xmax: 1270635 ymax: 1260315
#> epsg (SRID): NA
#> proj4string: +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs
#> First 10 features:
#> geom layer level class id metric value plot_id
#> 1 POLYGON ((1249635 1246815, ... 1 landscape NA NA lpi 36.36 1
#> 2 POLYGON ((1251135 1246815, ... 1 landscape NA NA lpi 8.84 2
#> 3 POLYGON ((1252635 1246815, ... 1 landscape NA NA lpi 15.20 3
#> 4 POLYGON ((1254135 1246815, ... 1 landscape NA NA lpi 19.40 4
#> 5 POLYGON ((1255635 1246815, ... 1 landscape NA NA lpi 21.00 5
#> 6 POLYGON ((1257135 1246815, ... 1 landscape NA NA lpi 22.08 6
#> 7 POLYGON ((1258635 1246815, ... 1 landscape NA NA lpi 13.88 7
#> 8 POLYGON ((1260135 1246815, ... 1 landscape NA NA lpi 20.48 8
#> 9 POLYGON ((1261635 1246815, ... 1 landscape NA NA lpi 25.84 9
#> 10 POLYGON ((1263135 1246815, ... 1 landscape NA NA lpi 15.84 10
#> percentage_inside
#> 1 100
#> 2 100
#> 3 100
#> 4 100
#> 5 100
#> 6 100
#> 7 100
#> 8 100
#> 9 100
#> 10 100
Created on 2019-12-21 by the reprex package (v0.3.0)
Hi Hessel,
The codes worked! Thanks a lot. That's a great contribution to my research.
Best,
Derya
Hey Derya,
That's great!
In case you don't need the actual raster within each sample plot, you can, of course, set return_raster = FALSE
. I just set it to true for checking/demonstrating which area is clipped.
I will close the issue, but please feel free to re-open if necessaire.
Cheers,
Max