r-spatialecology / landscapemetrics

Landscape Metrics for Categorical Map Patterns 🗺️ in R
https://r-spatialecology.github.io/landscapemetrics
GNU General Public License v3.0
227 stars 43 forks source link

sample_lsm Issue #323

Closed daviskec closed 1 month ago

daviskec commented 1 month ago

Hello,

I'm trying to apply the sample_lsm to calculate a bunch of metrics within a 300m buffer of my sample sites. I'm implementing the function as follows: class_ed_buffer <- sample_lsm(landscape = test_raster, y = sites, shape = "circle", size = 300, all_classes = TRUE, return_raster = FALSE, what = "lsm_c_ed", directions = 8, verbose = TRUE, progress = TRUE) When I run this, I get the following output/error: Progress nlayers: 1 / 1Error: [crop] extents do not overlap This error appears to be associated with terra::crop, but the only info I find online suggests that it is due to a non-matching crs. However, I have made sure that my crs matches between the sf object (sites) and the raster (test_raster), and when I plot them both on the same map, they definitely do overlap.

I downloaded thesample_lsm.R file from GitHub to see where exactly terra::crop was implemented and why I might be getting that error. I did find that when I got to line 173: landscape_mask <- terra::crop(x = landscape, y = y[current_plot,], mask = TRUE) I got an error that said crop does not work on objects of class list, which is not the same error I was getting when using the entire sample_lsm function. However, if I changed line 173 to say x=landscape[[1]] then it does work. The landscape appears to be converted into a list in one of the first lines of the function (line 58): landscape <- landscape_as_list(landscape).

I can't easily provide reproducible data for the error, but if needed I could set up a dropbox with the files. However, the datasets I'm working with are quite large - I'm using an HPC with a lot of memory to be able to manipulate them.

Nowosad commented 1 month ago

Hi @daviskec -- yes, sharing the files would be the best if you cannot reproduce this error on some smaller data.

daviskec commented 1 month ago

Hi @Nowosad - I uploaded the tif and site location data I was working with here, along with my code: https://drive.google.com/drive/folders/10Ihl4IV3D-GNHWI1zH_3MXreJX67f3R_?usp=sharing

Nowosad commented 1 month ago

Thanks, @daviskec.

I looked at your data and found the issue -- the point 336 has wrong coordinates (notice 8 instead of 7values for the y-axis). If you remove (or correct) this point, the calculations should be fine.

> p = sites_geom[336, ]
> st_bbox(p)
    xmin     ymin     xmax     ymax 
  516679 49749433   516679 49749433 
> st_bbox(test_raster)
   xmin    ymin    xmax    ymax 
 499980 4690200  609780 5000040 
mhesselbarth commented 1 month ago

Will close for now. Please re-open if needed