Episode III: Basic operations illutrating the transition from rgeos/rgdal to sf/terra
As you probably learned from the first episode of our Insight Series Spatial data in R, some R packages that were widely used for spatial data such as rgeos, rgdal, and maptools are not maintained anymore. The main reason is that the maintainer Roger Bivand retired. For R version >= 4.4.0, we need to use new packages for spatial data such as sf,terra, and stars.
In this episode, I will summarise a few key points about how this change may impact your work, mainly taking information from the webpage provided by Edzer Pebesma and Roger Bivand (link here: R-spatial evolution). I will then compare approaches using the old vs new packages to do basic operations on spatial data. This will be hopefully beneficial for those who used rgeos, rgdal, and maptools and want their code to be readable by others or for new users to get the basics of analysing spatial data in R.
In addition to the fact that the packages rgeos, rgdal, and maptools are not maintained anymore, other packages that are common for spatial data analysis such as raster and sp have dependencies with rgeos or rgdal. What does that mean? By using raster or rgdal to do some operations with spatial data in R, they may call a function from another package. Dependencies are common and usually not an issue but calling function from other packages assumes that the called package exists. If a package calls another package that you cannot obtain, such as rgeos or rgdal then you are in trouble 🙁.
Here we compare how to carry out basic spatial operations in R using the previous approach based on rgeos/rgdal/sp and the current approach based on sf/terra. We will focus on some spatial objects without going into much details on their structure. If you want get a good introduction of spatial data in R from Paula Moraga (👉 Ch2 - spatial data in R). If you are not familiar with spatial data at all, you should perhaps look read more chapters of her book. Here we will focus on two common spatial objects to illustrate the comparison between the use of old vs new R packages for spatial data.
Transition with vector objects
First, let's focus on "vector" objects. They may represent points, lines, or polygons. A shapefile is a vector object and therefore can be used to represent any type of vector object. It is composed of multiple files, including the geometry (.shp file) and a few more files which may include optionally a dataframe attached to it (.dbf) file. If a dataframe is attached, the spatial object contains layers which can be further analysed into maps with various colours to distinguish values for example. In the table below, we denote shp the R object generated by loading the shapefile 'shapefile.shp' in R. The other terms used in the table below should be hopefully clear.
Operation
Loading a shapefile (shapefile.shp) into the shp object
rgeos/rgdal/sp code ⛔
sf code ✅
Saving the spatial object shp into a shapefile (shapefile.shp)
Transform shapefile's projection to UTM 33
Intersection of polygons
Union of polygons
Create a 1,000-unit buffer around a geometry
Compute area of polygons
One way operation ➡️: spatial polygon to an sf object
Convert a spatial polygon or spatial polygon dataframe into an sf object
sf code
One way operation 🔙: sf object back to a spatial polygon object
Convert an sf object named 'sfshp' into a spatial polygon object
rgeos code
Transition with raster objects
Let's now look at "raster" objects. They are images composed of cells or pixels of equal size. If you take a picture from your phone it will likely be stored into a .jpg, .png, or .tif file, the latter is a common format used to store raster data. Picture can contain millions of pixels. A 1M-pixel image contains 1 million cells, which can be structured into 1000 (rows) x 1000 (columns) if the image is a square.
The package terra has replaced the package raster. Here we compare how to carry out basic spatial operations in R using the previous approach based on raster/rgeos/rgdal/sp and the current approach based on terra. In the table below, we denote shp the R object generated by loading the shapefile 'shapefile.shp' in R.The object rs is generated by loading the raster file 'rasterfile.tif' in R. The other terms used in the table below should be hopefully clear.
Operation
Loading a shapefile (shapefile.shp) into the shp object
raster/rgdal/sp code ⛔
terra code ✅
Saving the shp object into the shapefile.shp file
Reproject the polygon poly into UTM 33
Loading the raster file (rasterfile.tif) into the raster object rs
Saving the raster object rs into a raster file (rs.tif)
Cropping a raster object rs based on polygon poly
Reproject the raster object rs into UTM 33
Compute the mean of values from the raster object rs
Note that different packages may call a function using the same name. Both packages raster and terra use the function writeRaster() to save a raster file. Annoying, isn't it? To ensure that you call the desired package, use the double column symbol :: in front of the function you are applying. For example, to save a raster file using terra use terra::writeRaster().
In the next episode we will introduce some case studies with more advances features from sf and terra to do operations with spatial data. Stay tune!