`My goal is to calculate landscape variables, so I work with the "lconnect" and "landscape metrics" packages To do this, I have 30 ". tif" raster files that are composed of 7 to 12 different landscape classes. But to apply "lconnect" package, I converted my ".tif" files to shapefile and vector ". gpkg". But when I run the command, I get this error : "Erreur dans stats::hclust(distance, "single") : il faut n >= 2 objets pour une classification". When I look at the function command on a particular file, it works for 1/3 of my document. So when I put it in a loop, it instantly displays this error.
So, I checked the number of polygons I have for each file ". gpkg" and it shows me 7 to 12 polygons. I also tried with ".shp" files and ".gpkg" files and both don't work when it is in a loop.
I don’t understand where my mistake comes from, probably from the conversion?
Thanks `
setwd("C:/Users/user/OneDrive/Documents/Files")
install.packages(c("lconnect","landscapemetrics","raster","sp","sf","terra","gdalUtils","dplyr","rgdal","whitebox"))
x <- c("lconnect","landscape","raster","gdalUtils","sp","sf","terra","dplyr","rgdal","whitebox")
lapply(x, require, character.only = TRUE)
options(stringsAsFactors = FALSE)
path0= "C:/Users/user/OneDrive/Documents/Files"
path_in=paste0(path0,"/data_in")
path_out=paste0(path0,"/output")
list_all_tif <- list.files(path_in, pattern = "\\.tif$", full.names = TRUE)
rast_in=paste0(path_in,"/1.tif")
for (rast_in in list_all_tif) {
lulc <- rast(rast_in)
polygons = as.polygons(lulc,dissolve=T,na.rm=T)
plot(polygons)
vect_gpkg <- gsub(".tif$", "_vect.gpkg", basename(rast_in))
polygons_out <- file.path(path_out, vect_gpkg)
writeVector(polygons,polygons_out,overwrite=T)
cat("Number of polygons for", vect_gpkg, ":", nrow(polygons), "\n")
}
for (rast_in in list_all_tif) {
lulc <- raster(rast_in)
polygones <- rasterToPolygons(lulc, dissolve = TRUE)
vect_shp<- gsub(".tif$", "_vect.shp", basename(rast_in))
writeOGR(polygones, dsn = ".", layer = vect_shp, driver = "ESRI Shapefile")
}
polyg_in=paste0(path_out,"/1_vect.gpkg")
list_shapefile <-list.files(path_out, pattern = "\\_vect.gpkg$", full.names = TRUE)
for (polyg_in in list_shapefile) {
landscape <- upload_land(polyg_in, bound_path = NULL,habitat = 1, max_dist = 500)
metrics <- con_metric(landscape, metric = c('SLC', 'CCP', 'IIC', 'LCP', 'CPL', 'ECS'))
metrics <- data.frame(as.list(metrics))
}
for (rast_in in list_all_tif) {
lulc <- raster(rast_in)
metrics2 <- calculate_lsm(lulc, level = NULL, what = c('lsm_c_np', 'lsm_c_area_mn', 'lsm_c_area_sd', 'lsm_c_division', 'lsm_c_mesh', 'lsm_c_np', 'lsm_c_pd', 'lsm_c_lpi'), directions = 8)
metrics2[, 7] <- paste0(metrics2$metric, "_", metrics2$class)
df2 <- data.frame(t(metrics2[, 6:7]))
names(df2) <- df2[2, ]
df2 <- df2[-2, ]
metrics3<- calculate_lsm(lulc,level=NULL,what=c("lsm_l_sidi","lsm_l_siei","lsm_l_shdi","lsm_l_shei"),directions = 8)
df3 <- data.frame(t(metrics3[,5:6]))
names(df3) <- df3[1, ]
df3 <- df3[-1, ]
write.table(c(metrics,df2,df3), paste0(path0,"/output","_connectivity.txt") , append = FALSE, sep = " ", dec = ".", row.names=FALSE, col.names = TRUE)
}