Resources

Goals and Outcomes

  • Summarize the functionality of modern R packages for handling spatial data.
  • Discuss the retirement of rgdal, rgeos, and maptools and its effect on the rest of the spatial data ecosystem in R.
  • Provide additional resources for handling spatial data in R

sf

The sf R package (pebesma2018sf?) implements an R-based approach to handling spatial data based on simple features, a set of standards that specify how spatial objects are represented by computers. The simple features standards are widely implemented in spatial databases like GIS and GDAL. They have a geometry that describes their location on Earth using a coordinate reference system. Simple features objects in R are called sf objects and build upon R’s standard data frame by adding geometry. sf objects have two main components:

  1. A data.frame with feature attributes
  2. A list-column with geometries for each feature

You can operate on sf objects just as you would a operate on a data.frame, and there are many tidyverse operations (e.g., select(), filter()) that work directly with sf objects. All sf functions that operate on spatial data are prefixed by st_, which refers to spatial type. The prefix makes them easy to find via tab (or command-line) completion. For example, to inspect the sf function’s geometry as well-known text, run st_geometry().

The sf R package is the modern approach to manipulating spatial data in R using simple features and is designed to completely supersede sp. For more of sf, visit their website here or this overview here.

terra

The terra R package (hijmans2023terra?) is used to create, read, manipulate, and write raster data. Raster data is a spatial data structure that divides a region into rectangles called “cells” or “pixels” that can store one or more values fore each cell. Raster data structures are often called “grids”, a contrast with the “vector” data used to represent points, lines, and polygons as in the sf package. One advantage of the terra package and raster data structures is that it can handle large data sets on disk that are too large to be loaded into memory.

Raster objects in terra are typically created or read in using the rast() function. Raster objects in terra support common algebraic operations (e.g., +, *), logical operations (e.g., >=, ==), and functionals (e.g., round, log) on raster cells. There are several options for modifying raster objects: crop, which subsets a larger terra object; trim, which removes outer rows and columns with NA values; extend, which adds outer rows and columns with NA values, merge, which merges two or more raster objects into a single object; aggregate and disagg for changing the resolution (cell size) of a raster object; wrap for transforming to a new coordinate reference system; and more. There are also options for computing distances, summary statistics, prediction, and visualization.

The terra R package is the modern approach to manipulating raster data in R and is designed to completely supersede raster``. For more ofterra`, visit their website here.

Other Useful Spatial Data Packages

  • stars for handling spatio-temporal vector and raster data (link here)
  • leaflet for an R package that accesses leaflet, a population JavaScript library for interactive maps (link here)
  • tmap for generating thematic maps in R for (link here)
  • mapview for a different approach to interacting with the leaflet Javascript library (link here)
  • ggplot2 for general graphics in R (link here), with a focus on geom_sf() (link here)

Retirement of rgdal, rgeos, and maptools; Plan for sp

The rgdal, rgeos, and maptools R packages won’t be available on CRAN after October, 2023. Preparation for these retirements has been worked on for quite some time and is one of the motivating factors in pursuing modern alternatives like sf and terra. The sp package will still be available from CRAN, but it won’t be actively maintained, and sf should be used instead. To learn more about these retirements and understand how you may be affected, visit this link here.

R Spatial Resources

R Vector Processing / Simple Features Resources

R Raster Resources

R Mapping Resources

Web Services in R

General R Resources

Previous R Spatial Workshops