Overview

The first generation of r spatial packages (raster, sp, rgdal, rgeos) have gone the way of the dodo, and the second-generation packages (terra, sf, and stars) are here to stay. If you haven’t made the transition from raster to terra, now is the time. And the good news is, it’s probably easier than you think. This compares the two packages, provides direct function and syntax translations, and links to many resources to answer any further questions you might have.

Additionally, because both raster an terra are so large, this reader focuses on things that have changed between raster and terra. If you don’t see something mentioned here, that doesn’t mean it doesn’t exist in terra. It probably just didn’t change between the two packages.

Learning Objectives

  • Get a sense for the functionality of the terra package as a whole
  • Be able to translate existing code from deprecated packages to terra syntax
  • Know where to find more information about terra.

This reader will NOT give you a comprehensive tour of terra.

It is too short for that. However, it will provide links to places you can find more documentation.

Prerequisites

  • Familiarity with the raster and sp packages

What we will cover

  • Package overview
  • New classes and object creation
  • Method name changes
  • Spatial relationships
  • Calculations
  • Plotting
  • What’s not in terra

Setup

library(microbenchmark) # for speed testing

#new packages
library(terra) 
library(geodata)
library(tidyterra)
library(sf)

#old packages
library(raster)
library(rgdal)

#formatting
library(data.table)
library(kableExtra)
library(scales)
library(RColorBrewer)

temp_pal = colorRampPalette(brewer.pal(9, 'YlOrRd'))(50)
precip_pal = colorRampPalette(brewer.pal(9, 'Blues'))(50)

geodata_path('data/')

cv = function(x, na.rm=TRUE) sd(x, na.rm=na.rm)/mean(x, na.rm=na.rm)

options(warn=-1)

elev_fn = system.file("ex/elev.tif", package="terra")
lux_fn = system.file("ex/lux.shp", package="terra")

elev = rast(elev_fn)
r_elev = raster(elev)

lux = vect(lux_fn)
sp_lux = shapefile(lux_fn)

temp = worldclim_country(country='LUX', var='tavg')
precip = worldclim_country(country='LUX', var='prec')

names(temp) = month.name
names(precip) = month.name