raster
package so if you’re planning on working with lots of raster data, be sure to review this chapter to learn about other tools stored in the package.R
indexing which you should be familiar with by now.The main tool we’ll be working with in this tutorial is the raster
package, so go ahead and install/load that. You can work with rasters using the sp
package by loading the raster as a SpatialGridDataFrames
, but the raster
package is much better, so we’ll focus on that!
library(raster)
Raindrops on roses and rasters of land use… these are a few of my favorite things. Rasters are fantastic tools to store massive amounts of massively cool data. Think of a raster as a matrix (NO NOT THAT MATRIX)… or a grid of numbers. Each pixel in the raster has number associated with it that tells us something about the data of interest, i.e.
Let me pause here and state that you are some of the luckiest people to ever live because you have insanely easy access to incredible, beautiful visualizations of our precious planet.. in near real time, all the time, anywhere. Take a second to ponder that. Then go download Google Earth and start playing.
The data stored in rasters can be continuous (precipitation, elevation, reflectance) or categorical (land use category). In this tutorial we’ll work with both. For a raster to be a raster, we need a few key pieces of information:
When you’re working with raster data, especially satellite imagery, think carefully about trade-offs between resolution in space and time. Some datasets are available daily at a low spatial resolution; others are available monthly at a high spatial resolution; and others are high spatial and temporal resolution, but massively large and hard for you to work with on your laptop.
raster
packageThe raster
package includes several raster classes:
RasterLayer
contains a single-layer raster. This object contains the number of columns and rows in the raster, the spatial extent, pixel values, and the projection information (stored in CRS
format)RasterBricks
, and RasterStacks
are great for multiband data (i.e. multiple spectral bands, observations through time). The main difference is that a RasterBrick
can be linked to only one (though multi-layer) file. RasterStacks
can be made with multiple files (band from one file merged with a band from another file) - though these objects have to have the same extent and resolution.You’ve already learned how to make a raster from scratch, so we won’t cover that here. Instead, we’ll focus on learning how to load and manipulate the most commonly used types of raster data. There are tons of raster extensions out there: .grd
, .asc
, .sdat
, .rst
, .nc
, .tif
, .envi
, .bil
, .img
, .hdf
. I’ve encountered all of these at some point… and the raster
package can handle just about all of them. You can find the full list in the raster
package documentation. And if you still feel confused about the different ways of storing raster data in the raster
package, check this out. And finally, if you’re one of the unlucky ones whose data comes in HDF4
or HDF5
data (ahem NASA, get your stuff together!), read this. You’ll definitely want the HDFView
tool.
We’ll get to bricks later in the tutorial. For now, let’s work with a single layer of my current favorite raster dataset, the CropScape dataset.. This is a categorical raster that covers most of the country from 2000 to present at a 30 meter - YES T-H-I-R-T-Y meter - resolution. Since the dataset was built by the folks at the U.S. Department of Agriculture, there are more detailed categorizations for agricultural lands than for ecosystems.1
I went to the CropScape website and downloaded data for Cache Valley in 2016. To do this, click on the little USA map at the top of the page, select County
, find Cache, then click Submit
. The map should zoom to Cache Valley. Next, click the little folder icon with a green arrow on it. Select 2016
, Submit
and the data should download to your computer. You’ll have to unzip the files and move both files to a local data folder. So I did all that, and can load my raster with the easiest function ever:
cache <- raster("./data/CDL_2016_49005.tif")
plot(cache)