Before you start

In this chapter, we will learn how to use the raster and terra package to handle raster data. The raster package has been (and I must say still is) the most popular and commonly used package for raster data handling. However, we are in the period of transitioning from the raster package to the terra package. The terra package has been under active development to replace the raster package (see the most up-to-date version of the package here). terra is written in C++ and thus is faster than the raster package in many raster data operations. The raster and terra packages share the same function name for many of the raster operations. Key differences will be discussed and will become clear later.

For economists, raster data extraction for vector data will be by far the most common use case of raster data and also the most time-consuming part of the whole raster data handling experience. Therefore, we will introduce only the essential knowledge of raster data operation required to effectively implement the task of extracting values, which will be covered extensively in Chapter 5. For example, we do not cover raster arithmetic, focal operations, or aggregation. Those who are interested in a fuller treatment of the raster or terra package are referred to Spatial Data Science with R and “terra” or Chapters 3, 4, and 5 of Geocomputation with R, respectively.

Even though the terra package is a replacement of the raster package and it has been out on CRAN for more than a year, we still learn the raster object classes defined by the raster package and how to switch between the raster and terra object classes. This is because other useful packages for us economists were written to work with the raster object classes and have still not been adapted to support terra object classes at the moment.

Finally, you might benefit from learning the stars package for raster data operations (covered in Chapter 7), particularly if you often work with raster data with the temporal dimension (e.g., PRISM, Daymet). It provides a data model that makes working with raster data with temporal dimensions easier. It also allows you to apply dplyr verbs for data wrangling.

Direction for replication

Datasets

All the datasets that you need to import are available here. In this chapter, the path to files is set relative to my own working directory (which is hidden). To run the codes without having to mess with paths to the files, follow these steps:

  • set a folder (any folder) as the working directory using setwd()
  • create a folder called “Data” inside the folder designated as the working directory (if you have created a “Data” folder previously, skip this step)
  • download the pertinent datasets from here
  • place all the files in the downloaded folder in the “Data” folder

Packages

Run the following code to install or load (if already installed) the pacman package, and then install or load (if already installed) the listed package inside the pacman::p_load() function.

if (!require("pacman")) install.packages("pacman")
pacman::p_load(
  terra, # handle raster data
  raster, # handle raster data
  cdlTools, # download CDL data
  mapview, # create interactive maps
  dplyr, # data wrangling
  sf # vector data handling
)