In R raster calculations, involving one or more raster layers, can be conducted in 3 different ways:
...
Below examples of raster calculations are shown below. These examples are taken from the “Effects of Cyclone Yasi on Green Cover at Mission Beach” tutorial. Putting the code snippets in context, by looking at a broader section of the R script, could be benefitial. Boxes with grey background contain code snippets, and boxes with white background containt code (text) outputs.
Single-Layer Raster Calculations
...
Example 1: Re-scale SPGC for the winter of year 2010
.
Div |
---|
style | background-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
SPGC.StudyArea.2010q3.rl = SPGC.StudyArea.2010q3.rl * 100 / 255
SPGC.StudyArea.2010q3.rl
|
.
Div |
---|
style | background-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
## class : RasterLayer
## dimensions : 295, 206, 60770 (nrow, ncol, ncell)
## resolution : 30, 30 (x, y)
## extent : 1484025, 1490205, -1996985, -1988135 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs
## data source : in memory
## names : SPGC_2010q3
## values : 39.21569, 77.2549 (min, max)
|
.
.
Example 2: Re-scale SPGC for the winter of year 2011
...
Div |
---|
style | background-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
SPGC.StudyArea.2011q3.rl = SPGC.StudyArea.2011q3.rl * 100 / 255
SPGC.StudyArea.2011q3.rl
|
.
Div |
---|
style | background-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
## class : RasterLayer
## dimensions : 295, 206, 60770 (nrow, ncol, ncell)
## resolution : 30, 30 (x, y)
## extent : 1484025, 1490205, -1996985, -1988135 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs
## data source : in memory
## names : SPGC_2011q3
## values : 39.21569, 76.86275 (min, max)
|
.
.
.
Multiple-Layer Raster Calculations
.
Example 1: Compute the change in Raw (in %) Seasonal Persistent Ground Cover (SPGC) fraction between the winters of 2011 and 2010
.
Div |
---|
style | background-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
SPGC.StudyArea.Diffq3.rl = SPGC.StudyArea.2011q3.rl - SPGC.StudyArea.2010q3.rl
SPGC.StudyArea.Diffq3.rl
|
.
Div |
---|
style | background-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
## class : RasterLayer
## dimensions : 295, 206, 60770 (nrow, ncol, ncell)
## resolution : 30, 30 (x, y)
## extent : 1484025, 1490205, -1996985, -1988135 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs
## data source : in memory
## names : layer
## values : -7.843137, 7.058824 (min, max)
|
.
.
Example 2: Compute the change in Standardised (in Standard Deviations) Seasonal Persistent Ground Cover (SPGC) fraction between the winters of 2011 and 2010
.
Method 1: Compute using Raster Algebra
.
Div |
---|
style | background-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
SPGC.StudyArea.StdDiffq3.rl = ((SPGC.StudyArea.2011q3.rl - cellStats(SPGC.StudyArea.2011q3.rl,mean)) / cellStats(SPGC.StudyArea.2011q3.rl,sd)) -
((SPGC.StudyArea.2010q3.rl - cellStats(SPGC.StudyArea.2010q3.rl,mean)) / cellStats(SPGC.StudyArea.2010q3.rl,sd))
SPGC.StudyArea.StdDiffq3.rl
|
.
Div |
---|
style | background-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
## class : RasterLayer
## dimensions : 295, 206, 60770 (nrow, ncol, ncell)
## resolution : 30, 30 (x, y)
## extent : 1484025, 1490205, -1996985, -1988135 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs
## data source : in memory
## names : layer
## values : -3.245601, 2.273073 (min, max)
|
.
.
Method 2: Compute using Higher Level Functions from the ‘raster’ package
.
Div |
---|
style | background-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
calc.DiffStdRaster.f = function(rla, r1b)
{
diff.std.rl = ((r1b - cellStats(r1b, mean)) / cellStats(r1b, sd)) - ((rla - cellStats(rla, mean)) / cellStats(rla, sd))
return(diff.std.rl)
}
SPGC.StudyArea.StdDiffq3.rl.m2 = calc.DiffStdRaster.f(SPGC.StudyArea.2010q3.rl, SPGC.StudyArea.2011q3.rl)
SPGC.StudyArea.StdDiffq3.rl.m2
|
.
Div |
---|
style | background-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
## class : RasterLayer
## dimensions : 295, 206, 60770 (nrow, ncol, ncell)
## resolution : 30, 30 (x, y)
## extent : 1484025, 1490205, -1996985, -1988135 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs
## data source : in memory
## names : layer
## values : -3.245601, 2.273073 (min, max)
|
.
.
Method 3: Compute using ‘normImage’ function in ‘RStoolbox’ package
.
Div |
---|
style | background-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
SPGC.StudyArea.StdDiffq3.rl.m3 = normImage(SPGC.StudyArea.2011q3.rl) - normImage(SPGC.StudyArea.2010q3.rl)
SPGC.StudyArea.StdDiffq3.rl.m3
|
.
Div |
---|
style | background-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
## class : RasterLayer
## dimensions : 295, 206, 60770 (nrow, ncol, ncell)
## resolution : 30, 30 (x, y)
## extent : 1484025, 1490205, -1996985, -1988135 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs
## data source : in memory
## names : layer
## values : -3.245601, 2.273073 (min, max)
|
.
.
Compare Raster resulting from the different Standarisation Methods
.
Div |
---|
style | background-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
all.equal(SPGC.StudyArea.StdDiffq3.rl, SPGC.StudyArea.StdDiffq3.rl.m2)
|
.
Div |
---|
style | background-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
## [1] TRUE
|
.
.
Div |
---|
style | background-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
all.equal(SPGC.StudyArea.StdDiffq3.rl, SPGC.StudyArea.StdDiffq3.rl.m3)
|
.
Div |
---|
style | background-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
## [1] TRUE
|
.
.
Remove unwanted objects
.
Div |
---|
style | background-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem; |
---|
|
rm(SPGC.StudyArea.StdDiffq3.rl.m2, SPGC.StudyArea.StdDiffq3.rl.m3)
|
.
.
.