In this chapter we describe how to access and explore attributes of remote sensing images with R. We also show how to plot (make maps).

We will primarily use a spatial subset of Landsat 8 scene collected on June 14, 2017. The subset covers the area between Concord and Stockton, in California, USA.

All Landsat image scenes have a unique product ID and metadata. You can find the information on Landsat sensor, satellite, location on Earth (WRS path, WRS row) and acquisition date from the product ID. For example, the product identifier of the data we will use is ‘LC08_044034_20170614’. Based on [this guide](https://landsat.usgs.gov/landsat-collections#Prod IDs), you can see that the Sensor-Satellite is OLI/TIRS combined Landsat 8, WRS Path 44, WRS Row 34 and collected on June 14, 2017. Landsat scenes are most commonly delivered as zipped file, which contains separate files for the reflectance values at each bandwidth (wavelength).

We will start by exploring and visualizing the data (See [chapter 1] for data downloading instructions if you have not already done so).

View image properties

Create RasterLayer objects for single Landsat layers (bands)

## Loading required package: sp
# Blue
b2 <- raster('data/rs/LC08_044034_20170614_B2.tif')
# Green
b3 <- raster('data/rs/LC08_044034_20170614_B3.tif')
# Red
b4 <- raster('data/rs/LC08_044034_20170614_B4.tif')
# Near Infrared (NIR)
b5 <- raster('data/rs/LC08_044034_20170614_B5.tif')

Print the variables to check. E.g.

## class       : RasterLayer 
## dimensions  : 1245, 1497, 1863765  (nrow, ncol, ncell)
## resolution  : 30, 30  (x, y)
## extent      : 594090, 639000, 4190190, 4227540  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=10 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
## data source : c:\github\rspatial\rspatial-web\source\rs\_R\data\rs\LC08_044034_20170614_B2.tif 
## names       : LC08_044034_20170614_B2 
## values      : 0.0748399, 0.7177562  (min, max)

You can see the spatial resolution, extent, number of layers, coordinate reference system and more.

Image information and statistics

The below shows how you can access various properties from a Raster* object (this is the same for any raster data set).

# coordinate reference system (CRS)
## CRS arguments:
##  +proj=utm +zone=10 +datum=WGS84 +units=m +no_defs +ellps=WGS84
## +towgs84=0,0,0
# Number of rows, columns, or cells
## [1] 1863765
## [1] 1245 1497    1
# spatial resolution
## [1] 30 30
# Number of bands
## [1] 1
# Do the bands have the same extent, number of rows and columns, projection, resolution, and origin 
## [1] TRUE

You can create a RasterStack (an object with multiple layers) from the existing RasterLayer (single band) objects.

landsatRGB <- stack(b4, b3, b2)
landsatFCC <- stack(b5, b4, b3)
# Check the properties of the RasterStack
## class       : RasterStack 
## dimensions  : 1245, 1497, 1863765, 3  (nrow, ncol, ncell, nlayers)
## resolution  : 30, 30  (x, y)
## extent      : 594090, 639000, 4190190, 4227540  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=10 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
## names       : LC08_044034_20170614_B4, LC08_044034_20170614_B3, LC08_044034_20170614_B2 
## min values  :              0.02084067,              0.04259216,              0.07483990 
## max values  :               0.7861769,               0.6924697,               0.7177562

Notice the order of the layers. These are suitable for plotting different color composites. You can learn more about color composites in remote sensing here and also in the section below.

You can also create the RasterStack using the filenames.

# first create a list of raster layers to use
filenames <- paste0('data/rs/LC08_044034_20170614_B', 1:11, ".tif")
##  [1] "data/rs/LC08_044034_20170614_B1.tif" 
##  [2] "data/rs/LC08_044034_20170614_B2.tif" 
##  [3] "data/rs/LC08_044034_20170614_B3.tif" 
##  [4] "data/rs/LC08_044034_20170614_B4.tif" 
##  [5] "data/rs/LC08_044034_20170614_B5.tif" 
##  [6] "data/rs/LC08_044034_20170614_B6.tif" 
##  [7] "data/rs/LC08_044034_20170614_B7.tif" 
##  [8] "data/rs/LC08_044034_20170614_B8.tif" 
##  [9] "data/rs/LC08_044034_20170614_B9.tif" 
## [10] "data/rs/LC08_044034_20170614_B10.tif"
## [11] "data/rs/LC08_044034_20170614_B11.tif"
landsat <- stack(filenames)
## class       : RasterStack 
## dimensions  : 1245, 1497, 1863765, 11  (nrow, ncol, ncell, nlayers)
## resolution  : 30, 30  (x, y)
## extent      : 594090, 639000, 4190190, 4227540  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=10 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
## names       : LC08_044034_20170614_B1, LC08_044034_20170614_B2, LC08_044034_20170614_B3, LC08_044034_20170614_B4, LC08_044034_20170614_B5, LC08_044034_20170614_B6, LC08_044034_20170614_B7, LC08_044034_20170614_B8, LC08_044034_20170614_B9, LC08_044034_20170614_B10, LC08_044034_20170614_B11 
## min values  :            9.641791e-02,            7.483990e-02,            4.259216e-02,            2.084067e-02,            8.457669e-04,           -7.872183e-03,           -5.052945e-03,            3.931751e-02,           -4.337332e-04,             2.897978e+02,             2.885000e+02 
## max values  :              0.73462820,              0.71775615,              0.69246972,              0.78617686,              1.01243150,              1.04320455,              1.11793602,              0.82673049,              0.03547901,             322.43139648,             317.99530029

Above we created a RasterStack with 11 layers. The layers represent reflection intensity in the following wavelengths: Ultra Blue, Blue, Green, Red, Near Infrared (NIR), Shortwave Infrared (SWIR) 1, Shortwave Infrared (SWIR) 2, Panchromatic, Cirrus, Thermal Infrared (TIRS) 1, Thermal Infrared (TIRS) 2. We won’t use the last four layers and we will learn how to remove those in following sections.

Visualize single and multi-band imagery

You can plot individual layers of a RasterStack of a multi-spectral image.

par(mfrow = c(2,2))
plot(b2, main = "Blue", col = gray(0:100 / 100))
plot(b3, main = "Green", col = gray(0:100 / 100))
plot(b4, main = "Red", col = gray(0:100 / 100))
plot(b5, main = "NIR", col = gray(0:100 / 100))

plot of chunk rs2multi

Check the legends. They represent the range of values in each layer, and they range from 0 and 1. Notice the difference in shading and range of legends between the different bands. This is because different Earth surface features reflect the incident solar radiation differently. Each layer represent how much incident solar radiation is reflected for that particular wavelength. For example, vegetation reflects more energy in NIR than other wavelengths and thus appears brighter in NIR wavelength. However water absorbs most of the incident energy in NIR regions and appears dark.

However we did not get much information from these grey-scale plots, but these are often combined to create more interesting plots. To combine three bands, we can use plotRGB. To make a “true/natural color” image (that is, something that looks like a normal photograph), we need to select the bands that we want to render in the red, green and blue regions. For this Landsat image, r = 3 (red), g = 2(green), b = 1(blue) will plot the true color composite (vegetation in green, water blue etc). You can also supply additional arguments to plotRGB to improve the visualization (e.g. a linear stretch of the values, using strecth = "lin").

plotRGB(landsatRGB, axes = TRUE, stretch = "lin", main = "Landsat True Color Composite")

plot of chunk truecolor

This RGB-plot has lot more information than the previous ones, although it has been created using the same bands. Another popular image visualization method in remote sensing is known “false color” image where red, green and blue bands are replaced by other bands. Selecting r = NIR, g = red, b = green will plot a “false color”” composite. This representation is popular as it makes it easy to see the vegetation (in red).

par(mfrow = c(1,2))
plotRGB(landsatRGB, r=1, g=2, b=3, axes=TRUE, stretch="lin", main="Landsat True Color Composite")
plotRGB(landsatFCC, r=1, g=2, b=3, axes=TRUE, stretch="lin", main="Landsat False Color Composite")

plot of chunk rs2plotrgb

Note: Always check for package documentation (help(plotRGB)) for other arguments that can be added (like scale) to improve or modify the image.

Exercise 1 Use the RasterStack landsat to create a true and false color composite (hint remember the position of the bands in the stack).

Subset and rename spectral bands

You can select specific layers (bands) using subset function, or via indexing.

# select first 3 bands only
landsatsub1 <- subset(landsat, 1:3)
# same
landsatsub2 <- landsat[[1:3]]
# Number of bands in orginal and new data
## [1] 11
## [1] 3
## [1] 3

As mentioned above, we have no use here for the last four bands inlandsat. You can remove those using

landsat <- subset(landsat, 1:7)

Set the names of the bands using the following:

## [1] "LC08_044034_20170614_B1" "LC08_044034_20170614_B2"
## [3] "LC08_044034_20170614_B3" "LC08_044034_20170614_B4"
## [5] "LC08_044034_20170614_B5" "LC08_044034_20170614_B6"
## [7] "LC08_044034_20170614_B7"
names(landsat) <- c('ultra-blue', 'blue', 'green', 'red', 'NIR', 'SWIR1', 'SWIR2')
## [1] "ultra.blue" "blue"       "green"      "red"        "NIR"       
## [6] "SWIR1"      "SWIR2"

Spatial subset or crop

Spatial subsetting can be used to limit analysis to a geographic subset of the image. Spatial subsets can be created with the crop function, using an extent object, or another spatial object from which an Extent can be extracted/created,.

# Using extent
## class       : Extent 
## xmin        : 594090 
## xmax        : 639000 
## ymin        : 4190190 
## ymax        : 4227540
e <- extent(624387, 635752, 4200047, 4210939)
# crop landsat by the extent
landsatcrop <- crop(landsat, e)

Exercise 2 Interactive selection from the image is also possible. Use drawExtent and drawPoly to select an area of interest. Note: drawing will not work for plots generated within the markdown document. Please run the plot and draw commands from the RStudio console.

Exercise 3 Use the RasterStack landsatcrop to create a true and false color composite (hint remember the position of the bands in the stack).

Saving results to disk

At this stage we may want to save the raster to disk using the function writeRaster. Multiple file types are supported. We will use the commonly used GeoTiff format. While the layer order is preserved, layer names are unfortunately lost in the GeoTiff format.

writeRaster(landsatcrop,filename = "cropped-landsat.tif", overwrite = TRUE)

To keep the layer names, you can used the ‘raster-grd’ format:

writeRaster(landsatcrop, filename = "cropped-landsat.grd", overwrite = TRUE)

The disadvantage of this format is that not many other programs can read the data, in contrast to the GeoTiff format.

Note: Check for package documentation (help(writeRaster)) for additional helpful arguments that can be added.

Relation between bands

A scatterplot matrix can be helpful in exploring relationships between raster layers. This can be done with the pairs() function of the raster package.

Plot of reflection in the ultra-blue wavelength against reflection in the blue wavelength.

pairs(landsatcrop[[1:2]], main = "Ultra-blue versus Blue")

plot of chunk rs2pairs1

Plot of reflection in the red wavelength against reflection in the NIR wavelength.

pairs(landsatcrop[[4:5]], main = "Red versus NIR")

plot of chunk rs2pairs2

The first plot reveals high correlations between the blue wavelength regions. Because of the high correlation, we can just use one of the Blue bands without losing much information.

This distribution of points in second plot (between NIR and Red) is unique due to its triangular shape. Vegetation reflects very highly in the NIR range than red and creates the upper corner close to NIR (y) axis. Water absorbs energy from all the bands and occupies the location close to origin. The furthest corner is created due to highly reflecting surface features like bright soil or concrete.

Extract raster values

Often we want to get the values of raster cells for specific geographic locations or area. The extract function is used to get raster values at the locations of other spatial data. You can use points, lines, polygons or an Extent (rectangle) object. You can also use cell numbers to extract values. When using points, extract returns the values of a Raster* object for the cells in which a set of points fall.

# load the polygons with land use land cover information
samp <- readRDS('data/rs/samples.rds')
# generate 300 point samples from the polygons; 
ptsamp <- spsample(samp, 300, type = 'random')
# add the class information to the point samples from polygons
ptsamp$class <- over(ptsamp, samp)$class
# extract values with points
df <- extract(landsat, ptsamp)
# To see some of the reflectance values
##      ultra.blue       blue      green        red        NIR      SWIR1
## [1,]  0.1321355 0.12714760 0.13668963 0.18778288 0.32700983 0.32612070
## [2,]  0.1312463 0.11255263 0.09034571 0.07063273 0.05029085 0.03272483
## [3,]  0.1441715 0.13044395 0.11760557 0.09802271 0.06252200 0.04478250
## [4,]  0.1071744 0.08353616 0.06870264 0.04374155 0.33820003 0.15445083
## [5,]  0.1361041 0.12053324 0.10838883 0.08735298 0.05046434 0.03636816
## [6,]  0.1405064 0.13085598 0.13005358 0.15913510 0.23809543 0.28190205
##           SWIR2
## [1,] 0.19528639
## [2,] 0.02715142
## [3,] 0.03638984
## [4,] 0.06989539
## [5,] 0.02873453
## [6,] 0.19485267

Spectral profiles

A plot of the spectrum (all bands) for pixels representing a certain earth surface features (e.g. water) is known as a spectral profile. Such profiles demonstrate the differences in spectral properties of various earth surface features and constitute the basis for image analysis. Spectral values can be extracted from any multispectral data set using extract function. In the above example, we extracted values of Landsat data for the samples. These samples include: cropland, water, fallow, built and open. First we compute the mean reflectance values for each class and each band.

ms <- aggregate(df, list(ptsamp$class), mean)
# instead of the first column, we use rownames 
rownames(ms) <- ms[,1]
ms <- ms[,-1]
##          ultra.blue       blue      green        red        NIR      SWIR1
## built     0.1827408 0.17526883 0.17737833 0.19132566 0.24418340 0.24180086
## cropland  0.1119747 0.08943998 0.08467470 0.05247608 0.48268723 0.16277651
## fallow    0.1326074 0.11636857 0.10289262 0.11015497 0.17115589 0.22603256
## open      0.1380836 0.13650621 0.15114874 0.20417131 0.34231158 0.35222516
## water     0.1335097 0.11646439 0.09942139 0.07877746 0.04838245 0.03302258
##               SWIR2
## built    0.19806620
## cropland 0.07137964
## fallow   0.19276600
## open     0.20968959
## water    0.02671183

Now we will plot the mean spectra of these features.

# Create a vector of color for the land cover classes for use in plotting
mycolor <- c('darkred', 'yellow', 'burlywood', 'cyan', 'blue')
#transform ms from a data.frame to a matrix
ms <- as.matrix(ms)
# First create an empty plot
plot(0, ylim=c(0,0.6), xlim = c(1,7), type='n', xlab="Bands", ylab = "Reflectance")
# add the different classes
for (i in 1:nrow(ms)){
  lines(ms[i,], type = "l", lwd = 3, lty = 1, col = mycolor[i])
# Title
title(main="Spectral Profile from Landsat", font.main = 2)
# Legend
legend("topleft", rownames(ms), 
       cex=0.8, col=mycolor, lty = 1, lwd =3, bty = "n")

plot of chunk rs2spect

The spectral profile shows (dis)similarity in the reflectance of different features on the earth’s surface (or above it). Unsurprisingly, the spectral signatures of ‘crop’ and ‘vegetation’ are similar. ‘Water’ shows relatively low reflection in all wavelengths, and ‘built’, ‘fallow’ and ‘open’ have relatively high reflectance in the longer wavelengts.