Spatial Data Analysis with R
Society for Conservation GIS, July 2021

Session 1 Review

Highlights From Last Time

Good Coding Practices

RStudio is a great IDE

Do your work in scripts (or Notebooks)

We almost always save values to variables:

pop_2010 <- 3200 * 0.03

You can see the data type of a variable with class():

class(pop_2010)


Packages

packages are the key to productivity with R

packages mainly provide additional functions (and sometimes data)

key packages for geospatial data include sf and raster

the tidyverse is a family of packages that use piping syntax

install packages from the ‘Packages’ pane in RStudio, or install.packages()

load packages into memory with library()

to prevent clashes with duplicate function names, specify the package using the pkgname:: prefix (e.g., dplyr::filter()), or indicate your preferences using the conflicted package


Importing and Plotting Vector Data

sf data frames are essentially regular data frames with a ‘geometry’ column

import GIS data with: st_read(source, layer)

where source can be a:

  • a folder (e.g, containing Shapefiles, geodatabase)
  • file (e.g., geojson file, KML file)
  • database connection string (e.g., “PG:dbname=postgis”)

layer is a named layer

view layers in a source with st_layers()

code to import Shapefiles, KMLs, GeoJSON, Geodatabase, GeoPackage

st_as_sf() to convert plain data frames into sf data frames

plot() works with sf objects

  • to plot just the outline try: plot(ca_counties %>% st_geometry())
  • to alter symbology by an attribute field use plot(ca_counties["col_name"])

tmap makes nicer maps

tm_shape(yose_bnd_ll) +
  tm_polygons() +    
tm_shape(yose_hp_ll) +
  tm_symbols() +  
...
  


Importing and Plotting Raster Data

raster() and brick() to import raster files

crop() to crop a raster

Code Recipes

Group Photo!

Download Today’s Notebooks

Make sure your working directory is the workshop folder, then:

zip_url <- "https://github.com/ucanr-igis/rspatial_data/raw/master/scgis21_nb_sect02.zip"
temp_fn <- tempfile()
download.file(zip_url, destfile=temp_fn, mode="wb")
unzip(temp_fn, exdir = ".")
unlink(temp_fn)

Look in the ‘Files’ pane to make sure they’re there!

Workshop Evaluation

Don’t forget the complete the workshop evaluation at the end of today!



Next: Manipulating sf Objects