我至少已经使用以下函数一年来通过sf多边形剪切栅格,没有任何问题,但自从更新到R 4.0.3和最近的rgdal更新后,我开始遇到麻烦。当我第一次运行clip时,会出现一个错误,指出x$.self$finalize())(),然后函数失败。在我再次运行它或重新加载raster包后,它似乎可以工作。 这个问题之前已经被提到过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))
运行剪辑时:
> DCN <- clip(DCN, shoreline)
Error in (function (x) : attempt to apply non-function
Called from: (function (x)
x$.self$finalize())(<environment>)
Browse[1]> DCNt <- t(as.matrix(DCN))
Error during wrapup: no method for coercing this S4 class to a vector
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>
更为奇怪的是,当我只运行等效命令时,会返回错误,但结果计算出来了,现在剪贴板可以正常工作。这很繁琐,我需要能够从整个脚本中获取源代码!
> test <- crop(mask(DCN, shoreline), shoreline)
Error in x$.self$finalize() : attempt to apply non-function
Error in x$.self$finalize() : attempt to apply non-function
Error in (function (x) : attempt to apply non-function
> DCN <- clip(DCN, shoreline)
>
这个示例中使用的shapefile和raster可以在这里找到: https://drive.google.com/drive/folders/1D_y_9vAA69CsA2nwJTw_hpfdjgZfqdRm?usp=sharing
供参考:
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] fitdistrplus_1.1-1 survival_3.2-7 MASS_7.3-53
[5] reticulate_1.18 stars_0.4-3 abind_1.4-5 sf_0.9-6
[9] EBImage_4.31.0 raster_3.4-5 sp_1.4-4
loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 locfit_1.5-9.4 lattice_0.20-41 deldir_0.1-29
[5] FNN_1.1.3 fftwtools_0.9-9 png_0.1-7 class_7.3-17
[9] zoo_1.8-8 gtools_3.8.2 digest_0.6.27 gstat_2.0-6
[13] R6_2.4.1 tiff_0.1-5 R.matlab_3.6.2 pracma_2.2.9
[17] coda_0.19-4 e1071_1.7-4 spam_2.5-1 pillar_1.4.6
[21] rlang_0.4.8 spdep_1.1-5 rstudioapi_0.11 gdata_2.18.0
[25] gmodels_2.18.1 R.utils_2.10.1 R.oo_1.24.0 Matrix_1.2-18
[29] splines_4.0.3 rgdal_1.5-18 htmlwidgets_1.5.2 igraph_1.2.6
[33] RCurl_1.98-1.2 compiler_4.0.3 pkgconfig_2.0.3 BiocGenerics_0.35.4
[37] htmltools_0.5.0 tidyselect_1.1.0 tibble_3.0.4 expm_0.999-5
[41] intervals_0.15.2 codetools_0.2-16 spacetime_1.2-3 crayon_1.3.4
[45] dplyr_1.0.2 rappdirs_0.3.1 bitops_1.0-6 R.methodsS3_1.8.1
[49] grid_4.0.3 jsonlite_1.7.1 nlme_3.1-150 spData_0.3.8
[53] lwgeom_0.2-5 lifecycle_0.2.0 DBI_1.1.0 magrittr_1.5
[57] units_0.6-7 KernSmooth_2.23-17 LearnBayes_2.15.1 ellipsis_0.3.1
[61] xts_0.12.1 generics_0.0.2 vctrs_0.3.4 boot_1.3-25
[65] tools_4.0.3 glue_1.4.2 purrr_0.3.4 maps_3.3.0
[69] fields_11.6 jpeg_0.1-8.1 parallel_4.0.3 classInt_0.4-3
[73] dotCall64_1.0-0
return(mask(crop(x, shape), shape))
应该更高效(先裁剪)。 - Robert Hijmans