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

1

3

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!

Heiduc answered 4/5, 2020 at 17:40 Comment(0)
M
3

These are error messages, but not errors in the strict sense as the script continues to run, and the results are not affected. They are related to garbage collection (removal from memory 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.


Edit (Oct 2022)

These annoying messages

Error in x$.self$finalize() : attempt to apply non-function
Error in (function (x)  : attempt to apply non-function

Will disappear with the next release of Rcpp, which is planned for Jan 2023. You can also install the development version of Rcpp like this:

install.packages("Rcpp", repos="https://rcppcore.github.io/drat")
Mb answered 5/5, 2020 at 21:26 Comment(3)
Hi Robert. Ths is news to me. If you can minimize the example down to least of spatial code and linking requirement I can attack it from the Rcpp side. We have been using Rcpp Modules for many years in many contexts -- without such messages AFAIK.Archibaldo
I have never seen it in packages without a module (or in raster before I added the module); and I have seen it in several packages with a module, also in simpler non-spatial packages such as Rquefts and Rwofost. But I have not been able to create a reproducible example with these packages. I will keep looking for one ---- that may take a while. I added another example to the github/terra issue referenced above, but I understand that you would like something simpler. I will let you know when I find one. Thank you.Mb
Awesome! Will help as I can, but the whole geo stack is a wee bit foreign to me.Archibaldo

© 2022 - 2024 — McMap. All rights reserved.