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

Receive 'length' error when using sample_lsm with polygons #310

Closed tmowens34 closed 6 months ago

tmowens34 commented 7 months ago

Hello,

I'm using package version 2.0.0. and keep receiving the following error when using sample_lsm(): Error in numeric(NA) : invalid 'length' argument.

My code looks like this:

EVT20 = raster("LC20_EVT_220rcl_11142023.tif")

g18 = st_read("PTT18_MCPs_redo_NAD83.shp")

Simple feature collection with 36 features and 1 field

Geometry type: POLYGON

Dimension: XY

Bounding box: xmin: -1758059 ymin: 2370639 xmax: -1684142 ymax: 2553837

Projected CRS: NAD83(2011) / Conus Albers

g18_metrics = sample_lsm(EVT20, g18, what = c("lsm_c_clumpy", "lsm_c_iji"), plot_id = g18$ID)

Error in numeric(NA) : invalid 'length'

The raster and shapefile have overlapping extents in the same datum. I've tried remaking the polygon file, subsetting to run fewer polygons, and even tried running one polygon at a time, and I'm still receiving the same error.

Any help is greatly appreciated!

mhesselbarth commented 7 months ago

Hey,

Your code looks like it should run. Do you have any MULTIPOLYGONS in your data? I remember we changed some of the behaviour there recently.

If you could actually share your data with us (or a sub-sample) so we could reproduce the error, we could look into this further.

tmowens34 commented 7 months ago

Hi!

The issue was the calculation of the clumpy index. If I asked for just area and iji everything ran smoothly. I had to adjust my input rasters and calculate the clumpy index separately. My vector file did not contain any MULTIPOLYGONS.

Thanks for getting back to me! Terrah

mhesselbarth commented 7 months ago

Hey Terrah,

Glad it's working for you now. Is there any chance you could still share some of the data with us? It sounds like you found a workaround, which is great, but something is still not working properly. You could also email the data to me and I won't share it with anyone else.

mhesselbarth commented 7 months ago

Okay, I think I found the bug. Was related to classes for which clumpy can not be calculated and how the corresponding NA is created. Should be fixed in https://github.com/r-spatialecology/landscapemetrics/commit/f714a61e517372ff6c76c07191c1b5806bc59d5a.

mhesselbarth commented 6 months ago

Will close this for now, but please re-open if needed.