我已经使用以下函数通过 sf 多边形剪辑栅格至少一年了,没有任何问题,但自从更新到 R 4.0.3 和最近的 rgdal 更新以来,我开始遇到麻烦。当我第一次运行 Clip 时,出现错误,指出 x$.self$finalize())() 并且函数失败。在我再次运行它或重新加载光栅包后,它似乎可以工作。这是之前通过 raster 和 sp 提出的。在这种情况下,问题似乎相关,请参见下文:我留下了通常在脚本中加载的附加库,以防它们与问题相关。
library(raster)
library(EBImage)
library(sf)
library(stars)
library(reticulate)
library(fitdistrplus)
measure <- import("skimage.measure")
clip<-function(x,shape) {
# Arguments:
# x: Input raster
# shape: polygon (in same projection)
# Returns:
# clipped raster
#######
return(crop(mask(x, shape), shape))
}
## polygon w/ single feature
shoreline <- st_read("shoreline_FULL.shp")
## Load channel network mask
DCN <- raster("2000_07_channel_clean.tif")
DCNt <- t(as.matrix(DCN))
Run Code Online (Sandbox Code Playgroud)
然后运行剪辑时:
> DCN <- clip(DCN, shoreline)
Error in (function (x) …Run Code Online (Sandbox Code Playgroud)