Search code examples
rtarget

Load raster data with {terra} in {targets} pipeline


I'm trying to load a raster in a targets pipeline with the terra package.

The _targets.R file looks like this:

# Created by use_targets().
# Follow the comments below to fill in this target script.
# Then follow the manual to check and run the pipeline:
#   https://books.ropensci.org/targets/walkthrough.html#inspect-the-pipeline # nolint

# Load packages required to define the pipeline:
library(targets)
# library(tarchetypes) # Load other packages as needed. # nolint

# Set target options:
tar_option_set(
  packages = c(
    "tidyverse",
    "terra",
  ), # packages that your targets need to run
  format = "rds" # default storage format
  # Set other options as needed.
)

# tar_make_clustermq() configuration (okay to leave alone):
options(
  clustermq.scheduler = "multiprocess"
)

# tar_make_future() configuration (okay to leave alone):
# Install packages {{future}}, {{future.callr}}, and {{future.batchtools}} to allow use_targets() to configure tar_make_future() options.

# Run the R scripts in the R/ folder with your custom functions:
tar_source()
# source("other_functions.R") # Source other scripts as needed. # nolint

# Replace the target list below with your own:
list(
  ## Load all data in R/load_data.R
  tar_target(esg_rast_file, "data/ESGs/ESGs.tif", format="file"),
  tar_target(esg_rast_data, terra::rast(esg_rast_file))
)

My output looks like this:

r$> library(targets)

r$> tar_make()
• start target esg_rast_file
• built target esg_rast_file [6.42 seconds]
• start target esg_rast_data
• built target esg_rast_data [0.05 seconds]
• end pipeline [7.96 seconds]
 
r$> tar_load(esg_rast_data)
NULL value passed as symbol address
r$> esg_rast_data
class       : SpatRaster
Error in x@ptr$nrow() : external pointer is not valid

Is this what is supposed to happen? Am I doing something wrong? I can't seem to find anything online about these errors. I'm also having trouble loading sf .shp data as well. Seems like I am missing something.


Solution

  • You can use tar_terra_rast() from the geotargets package.

    From the README:

    library(targets)
    
    tar_dir({ # tar_dir() runs code from a temporary directory.
      tar_script({
        library(geotargets)
    
        get_elev <- function() {
          terra::rast(system.file("ex", "elev.tif", package = "terra"))
        }
    
        list(
          tar_terra_rast(
            terra_rast_example,
            get_elev()
          )
        )
      })
    
      tar_make()
      x <- tar_read(terra_rast_example)
      x
    })
    #> ▶ dispatched target terra_rast_example
    #> ● completed target terra_rast_example [0.011 seconds, 7.992 kilobytes]
    #> ▶ ended pipeline [0.085 seconds]
    #> class       : SpatRaster 
    #> dimensions  : 90, 95, 1  (nrow, ncol, nlyr)
    #> resolution  : 0.008333333, 0.008333333  (x, y)
    #> extent      : 5.741667, 6.533333, 49.44167, 50.19167  (xmin, xmax, ymin, ymax)
    #> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
    #> source      : terra_rast_example 
    #> name        : elevation 
    #> min value   :       141 
    #> max value   :       547