Why does rasterToPoints generate an error on first call but not second?

筅森魡賤 提交于 2021-02-08 07:32:20

问题


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:

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

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().

Main questions:
1. Why does this produce an error at all?
2. Why does it only display the error on the first run through the loop?

Reproducible example:

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"))
}

Any help would be greatly appreciated!


回答1:


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.

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


来源:https://stackoverflow.com/questions/61598340/why-does-rastertopoints-generate-an-error-on-first-call-but-not-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!