为什么 rasterToPoints 在第一次调用时产生错误而不是第二次调用? [英] Why does rasterToPoints generate an error on first call but not second?

查看:149
本文介绍了为什么 rasterToPoints 在第一次调用时产生错误而不是第二次调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以遍历研究 ID 列表 (ids) 并将它们转换为单独的多边形/空间点.在第一次执行循环时,它会产生以下错误:

I have some code that loops over a list of study IDs (ids) and turns them into separate polygons/spatial points. On the first execution of the loop it produces the following error:

(函数(x)中的错误:尝试应用非函数

Error in (function (x) : attempt to apply non-function

这是来自 raster::rasterToPoints 函数.我已经查看了此函数的帮助部分中的示例,并且传递 fun=NULL 似乎是一种可接受的方法(过滤掉所有 NA 值).无论如何,所有值都等于 1,所以我尝试传递一个像它建议的简单函数,例如 function(x){x==1}.当这不起作用时,我还尝试仅抑制错误消息,但使用 try() 或 tryCatch() 没有任何运气.

This is from the raster::rasterToPoints function. I've looked at the examples in the help section for this function and passing fun=NULL seems to be an acceptable method (filters out all NA values). All the values are equal to 1 anyways so I tried passing a simple function like it suggests such as function(x){x==1}. When this didn't work, I also tried to just suppress the error message but without any luck using try() or tryCatch().

主要问题:
<强>1.为什么这会产生错误?
<强>2.为什么它只在循环第一次运行时显示错误?

可重现的例子:

library(ggplot2)
library(raster)
library(sf)
library(dplyr)

pacific <- map_data("world2")
pac_mod <- pacific
coordinates(pac_mod) <- ~long+lat
proj4string(pac_mod) <- CRS("+init=epsg:4326")
pac_mod2 <- spTransform(pac_mod, CRS("+init=epsg:4326"))
pac_rast <- raster(pac_mod2, resolution=0.5)
values(pac_rast) <- 1

all_diet_density_samples <- data.frame(
  lat_min = c(35, 35),
  lat_max = c(65, 65),
  lon_min = c(140, 180), 
  lon_max = c(180, 235),
  sample_replicates = c(38, 278), 
  id= c(1,2)
)
ids <- all_diet_density_samples$id
for (idnum in ids){
  poly1 = all_diet_density_samples[idnum,]
  pol = st_sfc(st_polygon(list(cbind(c(poly1$lon_min, poly1$lon_min, poly1$lon_max, poly1$lon_max, poly1$lon_min), c(poly1$lat_min, poly1$lat_max, poly1$lat_max, poly1$lat_min, poly1$lat_min)))))
  pol_sf = st_as_sf(pol)
  x <- rasterize(pol_sf, pac_rast)
  df1 <- raster::rasterToPoints(x, fun=NULL, spatial=FALSE) #ERROR HERE
  df2 <- as.data.frame(df1)
  density_poly <- all_diet_density_samples %>% filter(id == idnum) %>% pull(sample_replicates)
  df2$density <- density_poly
  write.csv(df2, paste0("pol_", idnum, ".csv"))
}

任何帮助将不胜感激!

推荐答案

这些是错误消息,但不是严格意义上的错误,因为脚本继续运行,并且结果不受影响.我所知道的是,这与垃圾收集(从不再使用的对象的内存中删除)有关,这使得查明导致它的原因变得棘手(在下面您可以看到一个稍微修改的示例,该示例暗示了另一个罪魁祸首),并且为什么它并不总是发生在同一地点.

These are error messages, but not errors in the strict sense as the script continues to run, and the results are not affected. All I know is that this is related to garbage collection (removal from memery of objects that are no longer in use) and this makes it tricky to pinpoint what causes it (below you can see a slightly modified example that suggests another culprit), and why it does not always happen at the same spot.

消息最终源于使用 raster::rasterize(就像使用 raster 包的 Rcpp 模块一样).我在几个使用 Rcpp 模块的 R 包中看到了这些消息.这是在 terrahttps://github 的上下文中的讨论.com/rspatial/terra/issues/30

The messages ultimately stem from using raster::rasterize (as that used the raster package's Rcpp module). I see these messages in several R packages that use Rcpp modules. Here is discussion in the context of the terra package https://github.com/rspatial/terra/issues/30

我不知道这是我这边的一些糟糕的编程,还是别的什么.感谢您创建此示例.希望有更博学的人能插话.

I do not know if this is some bad programming on my side, or something else. So thank you for creating this example. Hopefully someone more knowledgeable can chime in.

library(ggplot2)
library(raster)
library(sf)
library(dplyr)

pac_mod <- map_data("world2")
coordinates(pac_mod) <- ~long+lat
proj4string(pac_mod) <- CRS("+init=epsg:4326")
pac_mod2 <- spTransform(pac_mod, CRS("+init=epsg:4326"))
pac_rast <- raster(pac_mod2, resolution=0.5)
values(pac_rast) <- 1

all_diet_density_samples <- data.frame( lat_min = c(35, 35), lat_max = c(65, 65),
  lon_min = c(140, 180), lon_max = c(180, 235),  sample_replicates = c(38, 278), id= c(1,2))
ids <- all_diet_density_samples$id

for (idnum in ids){
  poly1 = all_diet_density_samples[idnum,]
  pol = st_sfc(st_polygon(list(cbind(c(poly1$lon_min, poly1$lon_min, poly1$lon_max, poly1$lon_max, poly1$lon_min), c(poly1$lat_min, poly1$lat_max, poly1$lat_max, poly1$lat_min, poly1$lat_min)))))
  pol_sf = st_as_sf(pol)
  x <- rasterize(pol_sf, pac_rast)
  df1 <- raster::rasterToPoints(x, fun=NULL, spatial=FALSE) #ERROR HERE
}

df2 <- as.data.frame(df1)
#Error in x$.self$finalize() : attempt to apply non-function
#Error in x$.self$finalize() : attempt to apply non-function
#Error in x$.self$finalize() : attempt to apply non-function

这篇关于为什么 rasterToPoints 在第一次调用时产生错误而不是第二次调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆