r-spatialecology/landscapemetrics

Error from sample_lsm: 'subscript out of bounds'

Closed this issue · 5 comments

Hello,

I am getting the above error when using sample_lsm to call lsm_c_ed, lsm_c_para_mn, and lsm_c_cohesion. For example:

Load land cover raster

lc2000 <- raster('LC2000_ORIGINAL.img')

Set up sample locations (points)

ghow <- read.csv('GHOW.csv', row.names = 1)
buow <- read.csv('BUOW.csv', row.names = 1)
owl <- rbind(buow, ghow)
IDED <- rowSums(owl[,6:17])
RICH <- rowSums(owl[,6:17] !=0)
owl <- cbind(owl[,1:5], IDED, RICH, owl[,6:17])
buow <- subset(owl, OWL == 'BUOW')
buow <- subset(buow, IDED >= 10)
ghow <- subset(owl, OWL == 'GHOW')
ghow <- subset(ghow, IDED >= 30)
owl <- rbind(buow, ghow)
proj <- projection(lc2000)
owl <- SpatialPointsDataFrame(owl[,2:3], owl, proj4string = CRS('+proj=longlat +datum=WGS84'))
owl <- spTransform(owl, CRS(proj))

Calculate edge density within 500m of each sample location (i.e., owl)

ed1km <- sample_lsm(lc2000, owl.sp, shape='square', size=500, what='lsm_c_ed')

Error in neighbor_matrix[2:ncol(neighbor_matrix), 1] : subscript out of bounds

drive-download-20190415T204715Z-001.zip

Any help with this error would be greatly appreciated! Also, congratulations on developing this package, it is AWESOME!

Cheers,
Leanne Heisler

Originally posted by @heislele in #17 (comment)

Hey Leanne,

Glad you like the package :)

I tried to reproduce your error, however, it seems like the raster file got somehow damaged while saving ("Failure during raster IO"). Could you please try to write/save the raster and upload the file again.

Thanks a lot.

Oh sorry about that!

Turns out the raster I loaded the first time was too big, so I included a subset of 3 samples and revised the code below:

samples <- readOGR(dsn = 'C:/Users/leann/GDrive/RESEARCH', layer = 'TEST_SAMPLES')
lc <- raster('TEST_RASTER.tif')
proj <- projection(lc)
samples <- spTransform(samples, CRS(proj))
ed1km <- sample_lsm(lc, samples, shape='square', size=500, what='lsm_c_ed')

TEST_RASTER.zip

Cheers!
Leanne Heisler

The error occurs because your raster values are not discrete classes. You can use the check_landscape() function to check if your raster is suitable for landscapemetrics.

The result shows that the values are non-integers (not allowed) and 1153 different classes exist (highly unlikely for discrete land-cover classes). I just quickly "floored" your values and everything works now. I don't know if this acutally makes sense for your data or another classification to discrete classes makes more sense.

Btw...Most of your seven sample points are located exactly ontop of each other and consequently you only have three different sample locations. Again, no idea about your data, but maybe you also want to make sure this is correct :).

Hope this helps,
Max

library(landscapemetrics)
library(raster)
library(sp)

# import sample points
samples <- shapefile("C:/Users/Maximilian/Downloads/TEST_RASTER/TEST_SAMPLES.shp")

# import raster
lc <- raster("C:/Users/Maximilian/Downloads/TEST_RASTER/TEST_RASTER.tif")

# check landscape
check_landscape(lc)
#> > Caution: Land-cover classes must be decoded as integer values.
#> > Caution: More than 30 land cover-classes - Please check if discrete land-cover classes are present.
#>   layer       crs units       class n_classes OK
#> 1     1 projected     m non-integer      1153  x

# only integer values
lc[] <- floor(lc[])

# everything is okay now
check_landscape(lc)
#>   layer       crs units   class n_classes OK
#> 1     1 projected     m integer         6  v

# get CRS
proj <- projection(lc)

# re-project samples
samples <- spTransform(samples, CRS(proj))

# now it should work
ed1km <- sample_lsm(landscape = lc, y = samples, 
                    shape = "square", size = 250, what = "lsm_c_ed")

ed1km
#> # A tibble: 42 x 8
#>    layer level class    id metric value plot_id percentage_inside
#>    <int> <chr> <int> <int> <chr>  <dbl>   <int>             <dbl>
#>  1     1 class     1    NA ed     37.8        1              92.2
#>  2     1 class     2    NA ed     43.0        1              92.2
#>  3     1 class     3    NA ed     44.3        1              92.2
#>  4     1 class     4    NA ed     48.2        1              92.2
#>  5     1 class     5    NA ed     40.4        1              92.2
#>  6     1 class     6    NA ed      7.81       1              92.2
#>  7     1 class     1    NA ed     37.8        2              92.2
#>  8     1 class     2    NA ed     43.0        2              92.2
#>  9     1 class     3    NA ed     44.3        2              92.2
#> 10     1 class     4    NA ed     48.2        2              92.2
#> # ... with 32 more rows

Created on 2019-04-17 by the reprex package (v0.2.1)

That was definitely the problem, thank you for your help, it is greatly appreciated!
Cheers!
Leanne Heisler

Great!

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