为什么rasterToPoints在第一次调用时产生错误,而第二次没有?

问题描述 投票:0回答:1

我有一些代码,循环处理一个研究ID的列表(ids),并将它们变成独立的多边形空间点。在第一次执行循环时,它会产生以下错误。

在(函数(x)中出错:试图应用非函数(x))

这是raster::rasterToPoints函数的结果。我看了这个函数的帮助部分的例子,传递 fun=NULL 似乎是一个可以接受的方法(过滤掉所有 NA 值)。所有的值都等于1,所以我试着传递一个简单的函数,比如function(x){x==1}。当这个方法不奏效时,我也尝试过抑制错误信息,但使用try()或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"))
}

任何帮助将是非常感激的!

r spatial r-raster
1个回答
1
投票

这些是 错误信息但不是 错误 严格意义上说,脚本继续运行,结果不受影响。我所知道的是,这与垃圾收集有关(从memery中移除不再使用的对象),这使得我们很难确定是什么原因造成的(下面你可以看到一个稍加修改的例子,表明了另一个罪魁祸首),以及为什么它并不总是发生在同一个地方。

这些消息最终源于使用raster::rasterize(因为那使用的是 raster 包的Rcpp模块)。) 我在几个使用了 Rcpp模块. 这里是在讨论 terra 包裹 https:/github.comrspatialterraissues30。

我不知道这是我这边编程不好,还是其他原因。所以,感谢你创造这个例子。希望有更懂行的人可以指点一下。

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
© www.soinside.com 2019 - 2024. All rights reserved.