簡體   English   中英

在 R 中使用循環重新采樣和匹配柵格堆棧

[英]Resample and matching raster stack using loop in R

我的目標是將生物多樣性數據與土地覆蓋信息(柵格和矢量)結合起來。 但是,我需要將每個柵格(預測變量)的分辨率、范圍、CRS 和維度與我的生物多樣性數據(答案變量)相匹配。 我已經成功地單獨完成了,但是有六個光柵。 雖然,當我嘗試對柵格堆棧進行循環時。 我有一些錯誤。

library(terra)
library(raster)
#Create a raster stack with land cover predictors:
CDI_stack<-raster::stack(list.files(path = dir_Proj1, pattern='.tif', full.names=T))
#Convert to cylindrical equal area projection
equalareaproj<-"+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
crs(CDI_stack, warn=FALSE)<-equalareaproj
#Raster with standard dimension, resolution, extention and CRS
standard<-terra::subset(study_area, 2) 
#Loop for the raster stack
for(i in 1:length(CDI_stack@layers)){
  #Creating a single raster with each layer to maintain values
  CDI_layer<-terra::rast(terra::subset(CDI_stack, i)) 
  #Matching a raster extention individually
  CDI_layer<-ext(standard) 
  #Cropping it with standard raster to reduce matching error
  raster::crop(CDI_layer[i],standard) 
  #Resample resolution 
  terra::resample(CDI_layer[i], standard, method= "near", threads= T) 
  #Write the raster:
  return(writeRaster(Resampled_layer, 
                     filename=paste0("~/Land use/Chronic_Anthropogenic_Disturbance_Surface/", CDI_layer[i]),
                     format="GTiff", overwrite=TRUE))
  }

我發現了這些錯誤:

Error in h(simpleError(msg, call)) : 
 error evaluating argument 'x' in method selection for function 'crop': 'this S4 class is not subsettable
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘crop’ for signature ‘"numeric"’

我想知道使用光柵堆棧是否有任何並發症,或者我是否錯誤地執行了任何代碼步驟。 我希望在代碼或 class object 的使用中找到更正。

拜托了,希望大家多多支持。 謝謝你。 G。

當您逐行運行代碼時,應該很容易找出問題所在; 包括 for 循環內部(將i設置為 1 或發生錯誤時的任何值)。

你會看到這失敗了:

CDI_layer <- ext(standard) 
raster::crop(CDI_layer[i],standard) 

因為CDI_layer[i]是一個單一的數字。

還有其他一些尷尬的事情。 特別是,只使用“terra”,不要同時使用“raster”,以免混淆。

感謝您的建議,Hijimanns 先生。 我發現錯誤太多了。 我更喜歡使用柵格堆棧中的列表而不是堆棧,它在循環中工作得更好。 此外,我使用矢量來裁剪柵格,它保留了柵格值(避免返回 NA)。

#Create a list from the stack with land cover predictors:
CDI_list<-terra::as.list(CDI_stack)  

rm(study_area,CDI_stack)

#Create a list to store the results
results <- list()

#Loop for each SpatRaster from the list
for(i in 1:length(CDI_list)) {
  r<-rast(CDI_list[[i]]) # create a raster for each layer
  ext(r) <-ext(standard)  # redefine extension for each layer
  #Crop rasters using the vector to avoid 'NA' values
  rc <- terra::crop(r, standard_vec)
  #Resample rasters following standard parameters
  rc <- terra::resample(rc, standard, method= "bilinear", threads= T)
  #Rewrite the list layers with the result     
  results[[i]] <- rc 
}

#Check the values
results[[4]]
#Rasterize the list to save it as a data frame
resampled<-rast(results)
df<-as.data.frame(resampled)
summary(df)
#Save the data frame in the project directory
data.table::fwrite(df, "~/Land use/DATASETS/resampled.csv")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM