r-spatialecology/landscapemetrics

Sample_lsm function - "y" argument

Closed this issue · 4 comments

Hello everyone,
I am completely new to R, so if this question appears annoying please, forgive me.
I would like to calculate some landscape metrics (mainly PLAND metrics) in buffers around several sample points, using the function "sample_lsm()". I have loaded my sample points in R as shapefile, with the function "shapefile" of the raster package. In this way, I have obtained a SpatialPointsDataFrame, but the function "sample_lsm()" only accept "2-column matrix with coordinates, SpatialPoints, SpatialLines, SpatialPolygons, sf points or sf polygons" as "y" arguments. How can I solve this problem?
Thank you all in advance.

Here attached what I have done up to now.

library(raster)
library(gdalUtilities)
library(sf)

Corine <- raster("U2018_CLC2018_V2020_20u1.tif")
Corine
Tus_boudaries <- st_read("ISTAT_Toscana_3035old.shp")
Corine_cropped <- st_crop(Corine, Tus_boudaries)
my_shape <- shapefile("ISTAT_Toscana_3035old.shp")
r2 <- crop(Corine, extent(my_shape))
r2
my_raster_clip <- mask(x = r2, mask = my_shape)
plot(my_raster_clip)

my_raster_clip

library(landscapemetrics)
check_landscape(my_raster_clip)

shapefile("Sample_points_3035old.shp")
points <- shapefile("Sample_points_3035old.shp")
class(points)

sample_lsm(list("my_raster_clip"),
           y = "points",
           size = 100,
           what = lsm_c_pland(my_raster_clip, directions = 8),
           shape = "circle")

From RStudio Console:

> class(points)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
> sample_lsm(list("my_raster_clip"),
+            y = "points",
+            size = 100,
+            what = lsm_c_pland(my_raster_clip, directions = 8),
+            shape = "circle")
Error: 'y' must be a matrix, SpatialPoints, SpatialLines, SpatialPolygons, POINT or MULTIPOINT.

Hi @Floreanita -- my guess is that you are using too many ". Can you try to run:

sample_lsm(my_raster_clip, y = points, size = 100, what = "lsm_c_pland", directions = 8, shape = "circle")

?

Hi @Nowosad!
Thank you so much for your kind, quick and useful reply!
Now it seems to work!

I have received this two warning messages:
Warning messages:
1: The 'percentage_inside' is below 90% for at least one buffer.
2: Please use 'check_landscape()' to ensure the input data is valid.
But I think I could ignore them...

Thank you very much!

You are welcome.

The first warning might be important, but depending on your analysis. It's more about how you interpret the results. At least one sample buffer has some area outside the area that has data.