This lesson is still being designed and assembled (Pre-Alpha version)

# Day 3: Introduction to Raster Data

## Overview

Teaching: min
Exercises: 3 min
Questions
• What is the average of the minimum and maximum values of your raster and what patterns do you observe?

Objectives
• Import and explore multiple rasters and develop initial geospatial insights. Use raster math to to calculate different summary statistics.

• Load in and explore the rasters, describe the patterns you observe. Perform raster math.

# Outline

• Intro + Minute questions (10-15 min.)
• Raster Capstone (30-40 min.):
• Use the overlay() function to perform raster math
• Outro + Final Questions (10-15 min.)

In this capstone lesson, we use the following capstone data:

• A raster of precipitation in Wisconsin
• A raster of maximum temperature in Wisconsin
• A raster of minimum temperature in Wisconsin

If you are using your own data, please identify two (3) rasters with continuous values that are unprojected. You may use the same 2 rasters from the previous section.

## 1) Load, summarize, and visualize

Load in the precipitation raster for Wisconsin and explore the raster. Plot the raster and describe the patterns you observe.

## Solution

library(raster)
library(ggplot2)
library(rgdal)
Error: package or namespace load failed for 'rgdal' in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/home/runner/work/_temp/Library/rgdal/libs/rgdal.so':
libgdal.so.26: cannot open shared object file: No such file or directory
precip <- raster("../data/precip_annual_wi.tif")
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create RasterLayer object from this file; perhaps you need to install rgdal first
summary(precip)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
7.00   29.38   36.60   34.89   42.77   67.00
precip_df <- as.data.frame(precip, xy=TRUE)
ggplot() +
geom_raster(data=precip_df,
aes(x=x, y=y,fill=precip_annual_wi))+
scale_fill_viridis_c() +
coord_quickmap()

Annual precipitation is highest near the Illinois border and near Iron county. It is lowest near Douglas and Washburn county.

## 2) Raster math using overlay(): calculating the difference

Load in the maximum and minimum temperature rasters for Wisconsin. Subtract the minimum temperature from the maximum temperature using the overlay() function and plot your results. Note what you observe.

## Solution

mintemp <- raster("../data/mintemp_monthcold_wi.tif")
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create RasterLayer object from this file; perhaps you need to install rgdal first
maxtemp <- raster("../data/maxtemp_monthwarm_wi.tif")
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create RasterLayer object from this file; perhaps you need to install rgdal first
diff <- overlay(maxtemp, mintemp,
fun = function(r1, r2) { return( r1 - r2) })
Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'overlay': object 'maxtemp' not found
diff_df <- as.data.frame(diff, xy=TRUE)
Error in as.data.frame.default(diff, xy = TRUE): cannot coerce class '"function"' to a data.frame
ggplot() +
geom_raster(data = diff_df,
aes(x = x, y = y, fill = layer)) +
scale_fill_gradientn(name = "Temperature Difference", colors = terrain.colors(10)) +
coord_quickmap()

Near Lake Michigan, the difference between maximum and minimum temperature was lowest, again possibly due to the lake effect. The largest differences in maximum and minimum temperatures was near the Minnesota border.

## 3) Raster math using overlay(): calculating the average

Use the overlay() function again to calculate the average temperature, using the maximum and minimum temperature rasters. Plot your results and note what you observe.

## Solution

avg <- overlay(maxtemp, mintemp,
fun = function(r1, r2) { return( (r1 + r2)/2) })
Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'overlay': object 'maxtemp' not found
#alternative:
#avg <- overlay(maxtemp, mintemp, fun = mean)

avg_df <- as.data.frame(avg, xy=TRUE)
Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'as.data.frame': object 'avg' not found
ggplot() +
geom_raster(data = avg_df,
aes(x = x, y = y, fill = layer)) +
scale_fill_gradientn(name = "Temperature Average", colors = terrain.colors(10)) +
coord_quickmap()

The average temperatures are higher toward the Illinois border and lower up north.

## Key Points

• Always explore your data and note initial geospatial insights.

• The overlay() function can be used to calculate different summary statistics for your raster.

• With the overlay() function, you can begin to develop your skills in writing functions.