Before you start
In this Chapter, we introduce the stars
package (Pebesma 2020) for raster data handling. It can be particularly useful for those who use spatiotemporal raster data often (like daily PRISM and Daymet data) because it brings a framework that provides a consistent treatment of raster data with temporal dimensions. Specifically, stars
objects can have a time dimension in addition to the spatial 2D dimensions (longitude and latitude), where the time dimension can take Date
values.77 This can be handy for several reasons as you will see below (e.g., filtering the data by date).
Another advantage of the stars
package is its compatibility with sf
objects as the lead developer of the two packages is the same person. Therefore, unlike the terra
package approach, we do not need any tedious conversions between sf
and SpatVector
. The stars
package also allows dplyr
-like data operations using functions like filter()
, mutate()
(see section 7.6).
In Chapters 4 and 5, we used the raster
and terra
packages to handle raster data and interact raster data with vector data. If you do not feel any inconvenience with the approach, you do not need to read on. Also, note that the stars
package was not written to replace either raster
or terra
packages. Here is a good summary of how raster
functions map to stars
functions. As you can see, there are many functions that are available to the raster
packages that cannot be implemented by the stars
package. However, I must say the functionality of the stars
package is rather complete at least for most economists, and it is definitely possible to use just the stars
package for all the raster data work in most cases.78
Finally, this book does not cover the use of stars_proxy
for big data that does not fit in your memory, which may be useful for some of you. This provides an introduction to stars_proxy
for those interested. This book also does not cover irregular raster cells (e.g., curvelinear grids). Interested readers are referred to here.
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 and put them 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 thepacman::p_load()
function.
if (!require("pacman")) install.packages("pacman")
::p_load(
pacman# spatiotemporal data handling
stars, # vector data handling
sf, # data wrangling
tidyverse, # handle raster data
cubelyr, # make maps
tmap, # make maps
mapview, # fast raster data extraction
exactextractr, # handle dates
lubridate, # download PRISM data
prism )
- Run the following code to define the theme for map:
theme_set(theme_bw())
<- theme(
theme_for_map axis.ticks = element_blank(),
axis.text= element_blank(),
axis.line = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_line(color='transparent'),
panel.grid.minor = element_line(color='transparent'),
panel.background = element_blank(),
plot.background = element_rect(fill = "transparent",color='transparent')
)