Welcome to Spatial Data in R! This first set of tutorials (in three parts) is designed to provide an introduction to the two types of spatial data you will encounter in R: vector data and rasters. By the end of this tutorial, you should have a good sense of how R thinks about spatial data, and how to import and export spatial datasets you may get from other programs or sources.
Rasters are much more compact that vectors. Because of their regular structure the coordinates do not need to be recorded for each pixel or cell in the rectangular extent. A raster has a CRS, an origin, a distance or cell size in each direction, a dimension in terms of numbers of cells, and an array of values. If necessary, the coordinates for any cell can be computed.
Note that the sp
library used for vector data does have some basic tools for manipulating raster data. However, the sp
library has largely been replaced by the raster
library we will use here, and anything one can do with the sp
library can also be done with the raster
library.
A raster dataset has three primary components:
It’s relatively easy to start a raster object by just defining the grid:
basic_raster <- raster(ncol = 5, nrow = 10, xmn = 0, xmx = 5, ymn = 0, ymx = 10)
basic_raster
class : RasterLayer
dimensions : 10, 5, 50 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 0, 5, 0, 10 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
However, note that this raster has a grid, but no data:
hasValues(basic_raster)
[1] FALSE
We add data to the raster object using the values
function:
values(basic_raster) <- 1:50 # Note 50 is the total number of cells in the grid.
plot(basic_raster)
Note even though a grid is a 2-dimensional object, raster
looks for data that is one-dimensional, then assigns the values in the DataFrame by (a) starting in the top left cell, then (b) moving across the row from left to right, and finally (c) moving down a row and repeating the whole process. Thus each column must be the length of the total number of cells.
To define a projection, we use the same proj4 strings as vector data, but without the intermediate step of creating a CRS object:
projection(basic_raster) <- "+init=EPSG:4326"
The raster
library can also read many file types on it’s own. For example, let’s load SF Elevation data.
raster_from_file <- raster("RGIS1_Data/sanfrancisconorth.dem")
plot(raster_from_file)
raster
objectsraster
has a number of functions that can be either used to look at a feature, or to change a feature depending on whether you try to set a value. For example:
res(basic_raster)
Change resolution: res(basic_raster) <- c(x value,y value)
ncol(basic_raster)
Change number of columns: ncol(basic_raster) <- value
However, be careful with these commands – if you change the number of columns or rows, that will necessarily change the resolution and vice versa! Moreover, if you change the dimensions, any values associated with the data will be erased.
R_Workshop
folder and it contains the materials you downloaded and unzipped earlier.raster_from_file <- raster("RGIS1_Data/sanfrancisconorth.dem")
plot(raster_from_file)
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.