Overlay and reclassification of rasters

In this section, we will introduce two basic operations involving rasters: performing mathematical operations between overlapping rasters and reclassifying the values of a raster into new aggregated categories.

Raster algebra and overlay operations

In many cases, when we have two or more overlapping rasters, we would like to apply a certain function on each pair, triplet, and so on of overlapping pixels in those rasters. As a result, we will get a new raster, where the value of each pixel is the result of the latter function on the respective pixels in the input rasters. Such operations are also referred to as raster algebra, usually when using straightforward arithmetic notation (such as r+s, where r and s are rasters) or overlay operations.

There are numerous functions that can be used in raster algebra expressions, including arithmetic operators (such as +, -, *, and /), logical operators (such as >, >=, <, <=, ==, and !), and several simple functions (such as min, max, and sum). Numeric values can also be combined with rasters (as long as the first object in the expression is a raster), in which case the numeric values are recycled as if to form a raster where all the values are equal to the given numeric value. For example, if r is a raster, r+1 would yield a new raster, where 1 is recycled and added to each of the values in raster r. Similarly, the expression r*2 would yield a raster where each value is multiplied by 2. A single band raster can also be combined with a multiband raster, in which case the single band raster is recycled.

For example, the following expression will give us a new raster with the minimal NDVI values observed in each pixel over the 280 layers of raster r, excluding NA values:

> min_ndvi = min(r, na.rm = TRUE)

Note

In cases where we would like to apply a certain function on the values of the raster (rather than performing an overlay operation), we need to apply the respective function on the vector of raster values, rather than on the raster object itself.

For example, to find out the minimum value among all cells in the first band of raster r, we will have to use the following expression:

> min(r[[1]][], na.rm = TRUE)
[1] 0.007

Applying the min function on the raster itself, with min(r[[1]],na.rm=TRUE), is an overlay operation. It is not what we want to do in this case since an overlay would give us a new raster where each cell contains the minimal observed value among all bands of the input raster. The result will be r[[1]] itself since there is only one band to choose from.

Using the range function on the multiband raster r would give us a new raster with two bands (since the function returns a vector of length 2), one with the minimal values observed in each pixel and the other with the maximal values observed in each pixel:

> range_ndvi = range(r, na.rm = TRUE)

We can examine the range_ndvi result by plotting it with the levelplot function (this time, showing contours with contour=TRUE):

> levelplot(range_ndvi, par.settings = RdBuTheme, contour = TRUE)

The following screenshot shows the graphical output:

Raster algebra and overlay operations

The resulting graphical output shows that NDVI minima are more spatially uniform than NDVI maxima.

Though slightly less straightforward than the raster algebra notation, the overlay and calc functions provide a more flexible way to perform overlay operations on rasters. Their main advantage is that we can supply any function (built-in or user-defined) to define the overlay operation, rather than choosing from the predefined set of functions that are applicable using the raster algebra notation. The main difference between calc and overlay is that calc is intended for overlay operations involving the bands of a single (multiband) raster, while overlay is intended to be used with several separate raster objects, although in many cases these functions are interchangeable (since we can always stack individual rasters into a single multiband raster). In both cases, we supply the overlay function as an argument to the fun parameter. The only requirement for the function we supply is that it should accept a vector and return a vector of a fixed length.

For example, we can write a function called prop_na that takes a numeric vector and returns the proportion of the NA values that vector contains (from 0, if the vector has no NA values, to 1, if the vector is entirely composed of the NA values). The following code section defines the prop_na function and demonstrates that it works as expected using a simple example:

> prop_na = function(x) length(x[is.na(x)]) / length(x)
> prop_na(c(10,3,NA,2))
[1] 0.25

We can now utilize calc to perform an overlay operation on all layers in r with the function prop_na, as follows:

> prop_na_r = calc(r, fun = prop_na)

The resulting object prop_na_r is a single band raster, where the value of each pixel reflects the proportion of missing values among the bands of the multiband raster r.

As another example, we will calculate an NDVI image based on our Landsat image l_00. NDVI is defined as the difference between NIR and red reflectances (which in Landsat correspond to bands 4 and 3, respectively) divided by their sum. We can perform the calculation by first defining a function called ndvi that, given a vector, calculates the NDVI based on its fourth and third elements (assuming they correspond to NIR and red, respectively).

> ndvi = function(x) (x[4] - x[3]) / (x[4] + x[3])

Then, we can apply the function on the Landsat multiband raster l_00 with calc:

> ndvi_00 = calc(l_00, fun = ndvi)

We will display the result using levelplot. Note that when plotting a single layer, an average profile of mean values is shown for rows and columns along plot margins (this behavior can be disabled by specifying margin=FALSE):

> levelplot(ndvi_00, par.settings = RdBuTheme, contour = FALSE)

The following graphical output is produced:

Raster algebra and overlay operations

The Landsat image was taken during the dry season, and the area has a semiarid climate; thus, the green vegetation cover is scarce except in a few spots. For example, the two larger white/light blue patches, roughly in the middle of the preceding screenshot, are planted pine (mainly Aleppo pine) forests named Lahav and Kramim. Aleppo pine is an evergreen tree species; thus, the forested area retains relatively high NDVI values even during the dry season. We are going to return to these two forests in other examples later.

When applying logical operators on rasters, we get rasters with logical values (TRUE or FALSE). However, the same way as we have seen regarding logical vectors (Chapter 2, Working with Vectors and Time Series), when we apply an arithmetic operation on a logical raster, the logical values are converted to numeric values (1 or 0) before the operation is carried out. For example, if we would like to find out how many missing values the first layer of raster r contains, we can use the following expression:

> sum(is.na(r[[1]])[])
[1] 721

Note that with is.na(r[[1]]), we first created a logical raster, where the values are TRUE in pixels with NA in r[[1]], or FALSE otherwise. Then, with [], we obtained the vector of values from this logical raster. Finally, with sum, we found out how many TRUE values the vector contains. The answer is 721 NA values in the first layer of r.

Logical rasters can also be used to select a subset of the values of another overlapping raster and assign new values to the resulting subset. For example, if we wanted to fill the missing values in r[[1]], say with the mean of all non-missing values, we could do so as follows:

> temp = r[[1]]
> temp[is.na(temp)] = mean(temp[], na.rm = TRUE)

Note that we first assigned the first band of r to a new object named temp so that we will not alter the values of the original raster object. Then, we created a logical raster named is.na(temp) to subset the NA values in temp and assign the mean value of all the other cells to that subset. Plotting the new raster temp alongside the original one r[[1]] indeed shows that the missing values—appearing white in the left panel of the following screenshot (and transparent in the Google Earth visualization as seen in the previous screenshot)—were uniformly filled:

> levelplot(stack(r[[1]], temp),
+ par.settings = RdBuTheme, contour = FALSE)

The resulting graphical output is shown in the following screenshot:

Raster algebra and overlay operations

Reclassifying raster values

Reducing the amount of information a raster has for easier interpretation often involves reclassifying the values of the raster from a continuous scale into a set of discrete categories. In fact, we have already seen one method that we can use to do this in the previous section— the assignment to subsets of raster values defined by a condition. For example, we can reclassify the ndvi_00 raster by assigning 0 to all cells where NDVI≤0.2 and 1 to all cells where NDVI>0.2, as follows:

> l_rec = ndvi_00
> l_rec[l_rec <= 0.2] = 0
> l_rec[l_rec > 0.2] = 1

The following expression that uses plot will show an image of l_rec:

> plot(l_rec)

This following screenshot shows the graphical output:

Reclassifying raster values

In fact, there is a specialized function for reclassification in the raster package called reclassify. The function accepts a raster (which we would like to reclassify) and a numeric vector or matrix (specifying the rules of reclassification). For example, if we supply a numeric vector, it should be composed of triplets of numeric values specifying the classification rules: from-, to-, new value, from-, to-, new value, and so on. It is often convenient to use the special value Inf (which stands for infinity) to specify unlimited from- or to- edges for a given range. For example, to specify the ≤0.2 range, we will say from -Inf to 0.2. Therefore, for example, we could create the l_rec raster with the following expression, instead of the three expressions shown in the previous example:

> l_rec = reclassify(ndvi_00, c(-Inf, 0.2, 0, 0.2, Inf, 1))

Here, in plain language, we request the values from -∞ to 0.2 (closed on the right by default, thus including 0.2, as in ≤0.2) to be converted to 0, and the values from 0.2 to ∞ to be converted to 1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.120.57