terra package looks designed to replace an old favourite
It is made by a similar team. The
terra documentation states “can do
more, is simpler to use, and it is faster.”
So should you make the switch to
terra? I’ll answer that here.
terra is simpler and faster than
raster and will be easy for
raster users to learn. Compatibility with other packages can
be an issue, but conversion back to
raster objects is easy. Verdict:
make the switch.
There are a few important considerations when changing packages:
How long will it take me to learn the new syntax?
How much help is available online?
Is it faster than what I used to use?
Will it be compatible with other packages I use?
I will test each in turn.
We’ll use this data from one of my courses.
First, let’s take a look at some basic syntax and compare it with raster
You can read in data much the same way, with the command
library(terra) r <- rast("data-for-course/spatial-data/MeanAVHRRSST.grd") plot(r)
ext(r) ## SpatExtent : 82.5, 181.25, -72.25, -9.75 (xmin, xmax, ymin, ymax)
Now let’s crop and reproject it:
#create an extent object ext2 <- ext(r) #constrain it in x direction ext2 <- 120 ext2 <- 170 r2 <- crop(r, ext2) r3 <- project(r2, "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0") plot(r3)
So much of the syntax is familiar (or identical), if slightly different. It took me about 10 minutes to translate what I know from raster to terra syntax.
Note there are some important caveats with terra when it comes to
cluster computations and saving data see
?terra for more information.
It’s early days yet. But the terra package documentation is outstanding, as good as it was for raster. This was one reason raster became so popular.
?terra provides a very helpful description, a menu of functions and at
the very end a translation of function names from raster to terra (many
are the same)
So users will be once again grateful to Robert Hijmans and the authorship team for the effort tney put into package documentation
There are a few courses/ blogs online if you google it and some limited posts on stackexchange sites.
No vignette with the package as yet.
So the verdict is that the documentation of the package and functions is excellent. Currently, there is limited existing documentation of troubleshooting errors and bugs online. So you might have to ask yourself. But online content will grow as the package becomes more popular.
I take the author’s word that its faster, but let’s see how much faster:
library(microbenchmark) r_raster <- raster::raster("data-for-course/spatial-data/MeanAVHRRSST.grd") robin_proj <- "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0" tout <- microbenchmark( project(r, robin_proj), raster::projectRaster(r_raster, crs = robin_proj), times = 10 ) tout ## Unit: milliseconds ## expr median ## project(r, robin_proj) 76.3 ## raster::projectRaster(r_raster, crs = robin_proj) 529.6
So something like 7 times faster for the computationally demanding task of reprojecting a raster.
The answer here obviously depends on what packages you want to use. A
key one for me is tmap for mapping. This now works with
But at the time of first writing this post my version of
tmap was out of date
terra wasn’t compatible with
tmap. So if you package isn’t compatible,
the next question, how onerous is it to convert a
terra raster to a
Here’s a demonstration (with my out of date version of
library(tmap) r_raster <- raster::raster(r) tm_shape(r_raster) + tm_raster()
The multi-tool function
raster() does the job, so I’ll take that for now.
terra looks set to replace
raster. It is faster and just as easy to
raster. Making the switch to
terra isn’t as hard as it may
seem, its use will seem very familiar to
There are probably common errors and bugs with particular data types for
the R community to find and there isn’t help online for those yet. There
will be challenges in compatibility with other packages. But conversion
raster objects is easy.
There are also new features in
terra, to handle vector data and manage
very large datasets. So plenty more to explore.