Summary and Setup

This lesson is an introduction to working with raster and vector data in R for researchers in urbanism or related fields. After a brief introduction to R, it focuses on vector and raster data and their use in practical GIS operations on urban datasets.

The content of the workshop was adapted from the three lessons of Data Carpentry Geospatial workshop, as follows:

  • Stachelek, J., & Marwaha, K. (2023). datacarpentry/organization-geospatial: Data Carpentry: Introduction to Geospatial Concepts 2023-05 (2023.05) [Computer software]. Zenodo. https://doi.org/10.5281/zenodo.7897047

  • Stachelek, J., Njambi, S., Mahoney, M., sheila, Thomaz, A. T., Rasel, A. A., Pérez-Suárez, D., Koontz, E., Molloy, J., Jablonski, J., Fung, J., Leinweber, K., Sisk, M., Detomasi, R., Stevens, S. L., Stewart, S., Chen, S., & Ng, W. (2023). datacarpentry/r-intro-geospatial: Introduction to R for Geospatial Data 2023-05 (2023.05) [Computer software]. Zenodo. https://doi.org/10.5281/zenodo.7897052

  • Stachelek, J., Becker, E. A., Asberry, D., Strimas-Mackey, M., Rasel, A. A., Li, A., Avery, R., kcarini, Marwaha, K., mneilson-usgs, Sparks, A. H., bart1, Knudsen, C. B., Kerchner, D., Vanichkina, D. P., Pérez-Suárez, Jablonski, J., Liou, M., Morandeira, N., & Williams, R. (2023). datacarpentry/r-raster-vector-geospatial: Data Carpentry: Introduction to Geospatial Raster and Vector Data with R, February 2023 (v2023.02.06) [Computer software]. Zenodo. https://doi.org/10.5281/zenodo.7612769

Overview

This workshop is designed to be run on your local machine. First, you will need to download the data we use in the workshop. Then, you need to set up your machine with the software required to analyse and process geospatial data. We provide installation instructions below for all components.

Data

Download the data zip file and unzip it to your Desktop. The file is 67.5 MB.

Software

Software Install Manual Available for Description
GDAL Link Link Linux, MacOS, Windows Geospatial model for reading and writing a variety of formats
GEOS Link Link Linux, MacOS, Windows Geometry models and operations
PROJ.4 Link Link Linux, MacOS, Windows Coordinate reference system transformations
R Link Link Linux, MacOS, Windows Software environment for statistical and scientific computing
RStudio Link Linux, MacOS, Windows GUI for R
UDUNITS Link Link Linux, MacOS, Windows Unit conversions

We provide quick instructions below for installing the various software needed for this workshop. At points, they assume familiarity with the command line and with installation in general. As there are different operating systems and many different versions of operating systems and environments, these may not work on your computer. If an installation doesn’t work for you, please refer to the installation instructions for that software listed in the table above.

If you have previously installed these software on your computer, please make sure to update your installations. Using the latest versions of R, RStudio, R packages and external dependencies indicated in the setup instructions will ensure that the software we use behaves the same way on all computers during the workshop.

GDAL, GEOS, and PROJ.4

The installation of the geospatial libraries GDAL, GEOS, and PROJ.4 varies significantly based on operating system. These are all dependencies for sf, the R package that we will be using for spatial data operations throughout this workshop.

To install the geospatial libraries, install the latest version RTools

Install the geospatial libraries individually using homebrew. Open Terminal.app and run the following commands:

BASH

$ brew tap osgeo/osgeo4mac && brew tap --repair
$ brew install proj
$ brew install geos
$ brew install gdal

Steps for installing the geospatial libraries will vary based on which form of Linux you are using. These instructions are adapted from the sf package’s README.

For Ubuntu:

BASH

$ sudo add-apt-repository ppa:ubuntugis
$ sudo apt-get update
$ sudo apt-get install libgdal-dev libgeos-dev libproj-dev

For Fedora:

BASH

$ sudo dnf install gdal-devel proj-devel geos-devel

For Arch:

BASH

$ pacman -S gdal proj geos

For Debian: The rocker geospatial Dockerfiles may be helpful. Ubuntu Dockerfiles are found here. These may be helpful to get an idea of the commands needed to install the necessary dependencies.

UDUNITS

Linux users will have to install UDUNITS separately. Like the geospatial libraries discussed above, this is a dependency for the R package sf. Due to conflicts, it does not install properly on Linux machines when installed as part of the sf installation process. It is therefore necessary to install it using the command line ahead of time.

Steps for installing the geospatial will vary based on which form of Linux you are using. These instructions are adapted from the sf package’s README.

For Ubuntu:

BASH

$ sudo apt-get install libudunits2-dev

For Fedora:

BASH

$ sudo dnf install udunits2-devel

For Arch:

BASH

$ pacaur/yaourt/whatever -S udunits

For Debian:

BASH

$ sudo apt-get install -y libudunits2-dev

R

Participants who do not already have R installed should download and install it.

To install R, Windows users should select “Download R for Windows” from RStudio and CRAN’s cloud download page, which will automatically detect a CRAN mirror for you to use. Select the base subdirectory after choosing the Windows download page. A .exe executable file containing the necessary components of base R can be downloaded by clicking on “Download R for Windows”.

To install R, macOS users should select “Download R for macOS” from RStudio and CRAN’s cloud download page, which will automatically detect a CRAN mirror for you to use. A .pkg file containing the necessary components of base R can be downloaded by clicking on the first available link (this will be the most recent), which will read R-4.x.x-arm64.pkg for Apple silicon (M1/M2) Macs and R-4.x.x-x86_64.pkg for older Intel Macs.

To install R, Linux users should select “Download R for Linux” from RStudio and CRAN’s cloud download page, which will automatically detect a CRAN mirror for you to use. Instructions for a number of different Linux operating systems are available.

RStudio

RStudio is a GUI for using R that is available for Windows, macOS, and various Linux operating systems. It can be downloaded here. You will need the free Desktop version for your computer. In order to address issues with ggplot2, learners and instructors should run a recent version of RStudio (v1.2 or greater).

R Packages

The following R packages are used in the various geospatial lessons.

  • tidyverse - this is a collection of packages for data science that contains the dplyr and ggplot2 packages we will use throughout the lessons.
  • terra
  • sf

To install these packages in RStudio, do the following:
1. Open RStudio by double-clicking the RStudio application icon. You should see something like this:

RStudio layout

2. Type the following into the console and hit enter.

R

install.packages(c("tidyverse", "terra", "sf"))

You should see a status message starting with:

OUTPUT

trying URL 'https://cran.rstudio.com/bin/macosx/big-sur-arm64/contrib/4.3/tidyverse_2.0.0.tgz'
Content type 'application/x-gzip' length 428470 bytes (418 KB)
==================================================
downloaded 418 KB

trying URL 'https://cran.rstudio.com/bin/macosx/big-sur-arm64/contrib/4.3/terra_1.7-55.tgz'
Content type 'application/x-gzip' length 97482563 bytes (93.0 MB)
==================================================
downloaded 93.0 MB

trying URL 'https://cran.rstudio.com/bin/macosx/big-sur-arm64/contrib/4.3/sf_1.0-14.tgz'
Content type 'application/x-gzip' length 86848420 bytes (82.8 MB)
==================================================
downloaded 82.8 MB

When the installation is complete, you will see a status message like:

OUTPUT

The downloaded binary packages are in
/var/folders/hk/0c_kwgjs1zlgczjlszxgq348281j_b/T//RtmpTmRDrL/downloaded_packages

You are now ready for the workshop!