rgisr-sfr-rasterfurrr

Parallel GIS with `future`


I noticed that when loading furrr after raster, I am warned that values is masked:

The following object is masked from ‘package:raster’:

    values

I'm not 100% convinced that is the issue, but I am struggling to run raster::extract on sf objects

library(raster)
library(sf)
p <- shapefile(system.file("external/lux.shp", package="raster"))
s <- as(p, "sf")
r <- raster(p, ncol=100, nrow=100)
values(r) <- 1:ncell(r)

I can extract just fine

raster::extract(r, s)

but when I parallelize (version 1) I get an error:

library(furrr)
plan(strategy = "multiprocess", workers =2)

future_map(1:2, function(extr){
  raster::extract(r,s)
})

one weird thing is that it seems to work ok with sp type objects (version 2)

u<-as(s, "Spatial")

future_map(1:2, function(extr){
  raster::extract(r,u)
})

Besides just switching between sf and SpatialPointsDataFrame, how can I get the "version 1" code working?


Solution

  • The workers aren't loading the sf package. Use .options = furrr_options(packages = "sf").

    Reproducing the issue:

    future_map(1:2, function(x){
      raster::extract(r,s)
    })
    #> Error in as(from, "Spatial"): no method or default for coercing "sf" to "Spatial"
    

    Solution:

    future_map(1:2, .options = furrr_options(packages = "sf"), function(x){
      raster::extract(r,s)
    })