1. The raster package
In the last chapter we focused on point, polygon, and line data, but data frames are also not ideal for storing raster data.
2. Data frames aren't great for storing spatial data
Remember the house price predictions from chapter 1. They were raster data stored in a data frame.
The problems are the same: there is no way to store information about the coordinate reference system, and it's inefficient in terms of storage since we end up storing the midpoint of every cell, rather than the origin, and the step sizes in each direction.
Displaying raster data in a data frame using ggplot2's geom_tile can also be inefficient. We end up drawing thousands of little rectangles, and depending on the number, that can be very time consuming.
3. A better structure for raster data
A much more intuitive structure for raster data is a matrix, where the cells of the matrix correspond to the cells of our spatial grid.
Then we just need to add the additional information for grid itself and the coordinate system. Special objects for raster data generally exploit this intuition and allow you manipulate rasters as you would a matrix.
Sometimes there will be multiple rasters all with the same grid, but each measuring something different. It's useful to think of these as multiple layers of raster data, and they are sometimes referred to as multi-layer or multi-band rasters. Intuitively, it's useful to think of these as 3D arrays, where the layers provide the third dimension.
4. The raster package
The sp package also provides some classes for raster data: SpatialGrid and SpatialPixels, and their data frame extensions. However, we will instead use the classes provided by the raster package.
We prefer the raster package, because it has easier import of raster files, doesn't read large rasters into memory and provides more functionality for manipulating rasters.
Like sp, the raster package also uses S4 to define its classes and methods, so you'll see some similarity when examining the objects. The raster package also acknowledges that you are probably working with both raster and sp objects and where appropriate will provide the same functionality under the same function name, so you have less to remember.
5. raster provides print methods for sp objects
One thing that can be a little confusing is that raster provides some methods, including print, for sp objects. That means you may see different output based on which packages you have loaded. For example, take the countries_spdf object from the last chapter. If we only have sp loaded, printing the object, that is just typing the name, results in a lot, and I mean pages and pages, of uninformative output.
6. raster provides print methods for sp objects
Now, if we load the raster package, and print it again, we get a succinct summary! The object itself hasn't changed, but when we ask to print the object the method is being drawn from raster rather than sp.
Once again, rather than telling you all the details of the raster objects,
7. Let's practice!
I'm going to give you a few to explore over the next few exercises.