Using an example from Geocomputations in R, I'm trying to import a DEM (from RSAGA package) from a 2-element file (header and list of elevations).
It works with the Raster package, but I'm having trouble with the proper syntax for Terra. I get an error indicating unused arguments in the code below. Could someone please check the Terra syntax and advise what the correct code would be?
# example from Geocomputations in R (v.1)
library(raster)
library(terra)
data("landslides", package = "RSAGA")
# this works:
demRaster <- raster::raster(dem$data,
crs = dem$header$proj4string,
xmn = dem$header$xllcorner,
xmx = dem$header$xllcorner + dem$header$ncols * dem$header$cellsize,
ymn = dem$header$yllcorner,
ymx = dem$header$yllcorner + dem$header$nrows * dem$header$cellsize)
# unused arguments error:
demTerra <- terra::rast(dem$data,
nrows = dem$header$nrows,
ncols = dem$header$ncols,
nlyrs = 1,
xmin = dem$header$xllcorner,
xmax = dem$header$xllcorner + dem$header$ncols * dem$header$cellsize,
ymin = dem$header$yllcorner,
ymax = dem$header$yllcorner + dem$header$nrows * dem$header$cellsize,
crs = dem$header$proj4string)
Best Answer
I had the same issue some time ago and fortunately this is quite easy to fix.
Since
dem$data
is a matrix,terra::rast()
does only accept a few number of arguments:According to
?rast
, you are only able to pass the following attributes:So basically, all you need to do is to create an object of type SpatExtent from your xmin, xmax, ymin, ymax coordinates and pass this along with the crs: