r-spatialecology/landscapemetrics

Error: Input must be a vector, not a <RasterStack> object.

Closed this issue · 5 comments

I need some help here, it's unclear to me what I'm doing wrong.

We have a rasterStack object that we are trying to run landscape metrics on. The check_landscape function comes out clear.

This is the error that I'm getting when trying to run landscape metrics on a rasterStack:

Error: Input must be a vector, not a object.

rlang::last_error()
<error/vctrs_error_scalar_type>
Input must be a vector, not a object.
Backtrace:

  1. base::$<-(...)
  2. vctrs:::stop_scalar_type(...)
  3. vctrs:::stop_vctrs(msg, "vctrs_error_scalar_type", actual = x)
    Run rlang::last_trace() to see the full context.

Would appreciate your help asap.

@priyapatel1 can you show us the code you are using?

Hi there, here is the code we used:

## Use this to install the package 
#install.packages("landscapemetrics")
#install.packages("Rcpp")
#install.packages("rgdal")

# Load Libraries
library(landscapemetrics)
library(raster)
library(Rcpp)
library(rgdal)
## Set this to where the files are saved. 
setwd("C:/Users/Bryant/Documents/RESEARCH/SoE")

## List of .tif files 
files <- list.files(pattern="\\.tif$")

df_class <- NULL

for (file in files) {
  print(file)
  landtest <- stack(file)
  classtest <- landscapemetrics::check_landscape(landtest)
  print(classtest$n_classes)
  class_level <- calculate_lsm(
    landtest,
    level = "class",
    metric = NULL,
    directions = 8,
    count_boundary = FALSE,
    consider_boundary = FALSE,
    edge_depth = 1,
    cell_center = FALSE,
    classes_max = 9,
    # run with 9 after
    neighbourhood = 8,
    ordered = TRUE,
    verbose = TRUE,
    progress = FALSE
  )

  class_level$filename <- file
  class_level$nclasses <- classtest$n_classes
  df_class <- rbind(class_level, df_class)

}

write.csv(df_class,file = "class_level_perband.csv")

We have a series of multi-band rasters that we need to loop through. Let me know if you want a copy of the .tif as well.

The code looks good to me. If you can share the .tif (or at least one or two where it fails) that would be great

@priyapatel1, I was unable to reproduce your problem on our test data. Feel free to reopen the issue if the problem still exist and (if possible) try to create a small reproducible example.

library(landscapemetrics)
library(tibble)
df_class <- NULL
classtest <- landscapemetrics::check_landscape(landscape)
#> Warning: Caution: Coordinate reference system not metric - Units of results
#> based on cellsizes and/or distances may be incorrect.
class_level <- calculate_lsm(
  landscape,
  level = "class",
  metric = NULL,
  directions = 8,
  count_boundary = FALSE,
  consider_boundary = FALSE,
  edge_depth = 1,
  cell_center = FALSE,
  classes_max = 9,
  # run with 9 after
  neighbourhood = 8,
  ordered = TRUE,
  verbose = TRUE,
  progress = FALSE
)
#> Warning: Please use 'check_landscape()' to ensure the input data is valid.
#> Warning: Class 1: PAFRAC = NA for class with < 10 patches
#> Warning: Class 3: PAFRAC = NA for class with < 10 patches
class_level$filename <- "my_file"
class_level$nclasses <- classtest$n_classes
df_class <- rbind(class_level, df_class)

Created on 2022-10-07 with reprex v2.0.2