Title: | Moving-Window and Direct Data Aggregation |
---|---|
Description: | Data aggregation via moving window or direct methods. Aggregate a fine-resolution raster to a grid. The moving window method smooths the surface using a specified function within a moving window of a specified size and shape prior to aggregation. The direct method simply aggregates to the grid using the specified function. |
Authors: | Laura Graham [aut, cre] , Felix Eigenbrod [ctb] (Input on initial conceptual development), Marco Sciaini [ctb] (Input on package development and structure) |
Maintainer: | Laura Graham <[email protected]> |
License: | GPL-3 |
Version: | 0.3.2 |
Built: | 2024-11-27 03:55:11 UTC |
Source: | https://github.com/ropensci/grainchanger |
An example map to show functionality on categorical surfaces.
cat_ls
cat_ls
A raster layer object.
Generated with nlm_mpd()
from NLMR
and classified with util_classify()
from landscapetools
.
Sciaini M, Fritsch M, Scherer C, Simpkins CE. NLMR and landscapetools: An integrated environment for simulating and modifying neutral landscape models in R. Methods in Ecology and Evolution. 2018; 00:1-9. https://doi.org/10.1111/2041-210X.13076
Marco Sciaini and Matthias Fritsch (2018). landscapetools: Landscape Utility Toolbox. R package version 0.4.0. https://CRAN.R-project.org/package=landscapetools
An example map to show functionality on continuous surfaces.
cont_ls
cont_ls
A raster layer object.
Generated with nlm_mpd()
from NLMR
.
Sciaini M, Fritsch M, Scherer C, Simpkins CE. NLMR and landscapetools: An integrated environment for simulating and modifying neutral landscape models in R. Methods in Ecology and Evolution. 2018; 00:1-9. https://doi.org/10.1111/2041-210X.13076
This function pads a raster by a specified number of cells, creating the effect of a torus. This function is intended for use on simulated landscapes, in order to avoid edge effects
create_torus(dat, dpad)
create_torus(dat, dpad)
dat |
The raster dataset to pad |
dpad |
The amount by which to pad the raster (in the same units as the raster) |
A torus is an infinite surface where the top joins the bottom, and the left side meets the right side. See https://en.wikipedia.org/wiki/Torus for a full mathematical description.
In this function, the torus effect is achieved by adding the specified number of rows of the top of the raster to the bottom (and vice versa) and the specified number of rows of the right of the raster to the left (and vice versa)
raster. Original raster padded by r cells with torus effect (see Details)
data(cat_ls) d <- create_torus(dat = cat_ls, dpad = 5)
data(cat_ls) d <- create_torus(dat = cat_ls, dpad = 5)
A range of functions to calculate well known landcover diversity metrics
## S3 method for class 'winmove' shdi(x, lc_class, d, type, ...) ## S3 method for class 'numeric' shdi(x, lc_class, ...) ## S3 method for class 'winmove' shei(x, lc_class, d, type, ...) ## S3 method for class 'numeric' shei(x, lc_class, ...)
## S3 method for class 'winmove' shdi(x, lc_class, d, type, ...) ## S3 method for class 'numeric' shdi(x, lc_class, ...) ## S3 method for class 'winmove' shei(x, lc_class, d, type, ...) ## S3 method for class 'numeric' shei(x, lc_class, ...)
x |
numeric, winmove. The data over which to calculate the diversity metrics |
lc_class |
numeric. The class values to include in the diversity metric calculation |
d |
numeric. If |
type |
character. The shape of the moving window |
... |
further arguments passed to or from other methods |
Currently provided diversity metrics are Shannon diversity and Shannon evenness. Open a new issue (https://github.com/laurajanegraham/grainchanger/issues) to request additional diversity metrics.
If class(x) == "winmove"
, a smoothed raster with the diversity
metric calculated within the specified moving window
If class(x) == "numeric"
, a single value representing the diversity metric in
x
McGarigal, K. and Marks, B.J., 1995. FRAGSTATS: spatial pattern analysis program for quantifying landscape structure. Gen. Tech. Rep. PNW-GTR-351. Portland, OR: US Department of Agriculture, Forest Service, Pacific Northwest Research Station. 122 p, 351.
# load required data data(cat_ls) # convert data to object of class winmove cat_ls <- new("winmove", cat_ls) # calculate Shannon diversity in a rectangular window of dimension 5 d <- shdi(cat_ls, d = 5, type = "rectangle", lc_class = 1:4) # convert data to object of class numeric cat_ls <- raster::values(cat_ls) # calculate Shannon evenness d <- shei(cat_ls, lc_class = 1:4)
# load required data data(cat_ls) # convert data to object of class winmove cat_ls <- new("winmove", cat_ls) # calculate Shannon diversity in a rectangular window of dimension 5 d <- shdi(cat_ls, d = 5, type = "rectangle", lc_class = 1:4) # convert data to object of class numeric cat_ls <- raster::values(cat_ls) # calculate Shannon evenness d <- shei(cat_ls, lc_class = 1:4)
An example grid to show functionality when aggregating using an sf object.
g_sf
g_sf
An sf object.
Generated with nlm_mpd()
and converted to sf.
Sciaini M, Fritsch M, Scherer C, Simpkins CE. NLMR and landscapetools: An integrated environment for simulating and modifying neutral landscape models in R. Methods in Ecology and Evolution. 2018; 00:1-9. https://doi.org/10.1111/2041-210X.13076
An extension to mean
for objects of class winmove
mean(x, ...) ## S3 method for class 'winmove' mean(x, d, type, ...)
mean(x, ...) ## S3 method for class 'winmove' mean(x, d, type, ...)
x |
RasterLayer. The data over which to calculate the mean value within a moving window |
... |
further arguments passed to or from other methods |
d |
numeric. If |
type |
character. The shape of the moving window |
RasterLayer. A smoothed raster with the mean calculated within the specified moving window
# load required data data(cont_ls) # convert data to object of class winmove cont_ls <- new("winmove", cont_ls) # aggregate using a circular window with radius 3 d <- mean(cont_ls, d = 3, type = "circle")
# load required data data(cont_ls) # convert data to object of class winmove cont_ls <- new("winmove", cont_ls) # aggregate using a circular window with radius 3 d <- mean(cont_ls, d = 3, type = "circle")
Calculate the value for a given function for each cell in a larger resolution grid.
nomove_agg(coarse_dat, fine_dat, agg_fun, is_grid = TRUE, quiet = FALSE, ...)
nomove_agg(coarse_dat, fine_dat, agg_fun, is_grid = TRUE, quiet = FALSE, ...)
coarse_dat |
sf, Raster* or Spatial* object. The coarse grain data (response data) across which to calculate the aggregated function |
fine_dat |
Raster* object. Raster* object. The fine grain data (predictor / covariate data) to aggregate |
agg_fun |
function The function to apply. The function fun should take multiple numbers, and return a single number. For example mean, modal, min or max. It should also accept a na.rm argument (or ignore it, e.g. as one of the 'dots' arguments. For example, length will fail, but function(x, ...)na.omit(length(x)) works. See Details |
is_grid |
logical. Use |
quiet |
logical. If |
... |
further arguments passed to or from other methods |
grainchanger
has several built-in functions. Functions
currently included are:
shdi
- Shannon diversity, requires the additional argument lc_class
(vector or scalar)
shei
- Shannon evenness, requires the additional argument lc_class
(vector or scalar)
prop
- Proportion, requires the additional argument lc_class
(scalar)
var_range
- Range (max - min)
Note that nomove_agg
can be run in parallel using plan(multiprocess)
from the future
package.
Raster (if input is Raster) or numeric vector (if input is sp or sf object) containing values calculated for each coarser cell
# load required data data(g_sf) data(cont_ls) data(cat_ls) # aggregate using mean d <- nomove_agg(g_sf, cont_ls, mean) # aggregate using Shannon evenness d <- nomove_agg(g_sf, cont_ls, shei, lc_class = 1:4)
# load required data data(g_sf) data(cont_ls) data(cat_ls) # aggregate using mean d <- nomove_agg(g_sf, cont_ls, mean) # aggregate using Shannon evenness d <- nomove_agg(g_sf, cont_ls, shei, lc_class = 1:4)
An example non-gridded coarse data to show functionality when aggregating using an sf object.
poly_sf
poly_sf
An sf object.
Generated with sf::st_make_grid(sf::st_as_sfc(sf::st_bbox(cont_ls)), cellsize = 13, square = FALSE)
Calculate the proportion of a given value present within a raster. Useful for calculating land-cover or soil type proportions. Should be used with a categorical raster
prop(x, lc_class, ...) ## S3 method for class 'winmove' prop(x, lc_class, d, type, ...) ## S3 method for class 'numeric' prop(x, lc_class, ...)
prop(x, lc_class, ...) ## S3 method for class 'winmove' prop(x, lc_class, d, type, ...) ## S3 method for class 'numeric' prop(x, lc_class, ...)
x |
numeric, winmove. The data over which to calculate the proportion |
lc_class |
numeric. The class value to calculate the proportion of |
... |
further arguments passed to or from other methods |
d |
numeric. If |
type |
character. The shape of the moving window |
If class(x) == "winmove"
, a smoothed raster with the proportion of
cells of the given class calculated within the specified moving window
If class(x) == "numeric"
, a single value representing the proportion of values
of a given class in x
# load required data data(cat_ls) # convert data to object of class winmove cat_ls <- new("winmove", cat_ls) # aggregate using a rectangular window with dimension 5 for class 3 d <- prop(cat_ls, d = 5, type = "rectangle", lc_class = 3) # convert data to object of class numeric cat_ls <- raster::values(cat_ls) d <- prop(cat_ls, lc_class = 2)
# load required data data(cat_ls) # convert data to object of class winmove cat_ls <- new("winmove", cat_ls) # aggregate using a rectangular window with dimension 5 for class 3 d <- prop(cat_ls, d = 5, type = "rectangle", lc_class = 3) # convert data to object of class numeric cat_ls <- raster::values(cat_ls) d <- prop(cat_ls, lc_class = 2)
Calculates the difference between the maximum and minimum value
var_range(x, ...) ## S3 method for class 'winmove' var_range(x, d, type, na.rm = TRUE, ...) ## S3 method for class 'numeric' var_range(x, na.rm = TRUE, ...)
var_range(x, ...) ## S3 method for class 'winmove' var_range(x, d, type, na.rm = TRUE, ...) ## S3 method for class 'numeric' var_range(x, na.rm = TRUE, ...)
x |
RasterLayer. The data over which to calculate the range size |
... |
further arguments passed to or from other methods |
d |
numeric. If |
type |
character. The shape of the moving window |
na.rm |
logical. indicates whether |
If class(x) == "winmove"
, a smoothed raster with the size of the range of values calculated within the specified
moving window
If class(x) == "numeric"
, a single value representing the size of the range of values in x
# load required data data(cat_ls) data(cont_ls) # convert data to object of class winmove cat_ls <- new("winmove", cat_ls) # aggregate using a rectangular window with dimensions c(2,3) d <- range(cont_ls, d = c(2,3), type = "rectangle") # convert data to object of class numeric cont_ls <- raster::values(cont_ls) d <- range(cont_ls)
# load required data data(cat_ls) data(cont_ls) # convert data to object of class winmove cat_ls <- new("winmove", cat_ls) # aggregate using a rectangular window with dimensions c(2,3) d <- range(cont_ls, d = c(2,3), type = "rectangle") # convert data to object of class numeric cont_ls <- raster::values(cont_ls) d <- range(cont_ls)
Smooth a raster surface using a moving window with a given function, radius and shape.
winmove(fine_dat, d, type = c("circle", "rectangle"), win_fun, ...)
winmove(fine_dat, d, type = c("circle", "rectangle"), win_fun, ...)
fine_dat |
The raster dataset on which to calculate the moving window function |
d |
numeric. If |
type |
The shape of the moving window |
win_fun |
function. The function to apply. If not choosing one of the inbuilt
grainchanger functions, the function should take multiple numbers, and return a
single number. For example |
... |
further arguments passed to or from other methods |
grainchanger
has several built-in functions. Functions currently
included are:
wm_shei
- Shannon evenness, requires the
additional argument lc_class
(vector or scalar)
wm_prop
-
Proportion, requires the additional argument lc_class
(scalar)
wm_classes
- Unique number of classes in a categorical landscape
var_range
- Range (max - min)
RasterLayer. A smoothed raster with the moving window values calculated
# load required data data(cat_ls) data(cont_ls) # calculate the moving window mean d <- winmove(cont_ls, 5, "rectangle", mean) # calculate the moving window Shannon evenness d <- winmove(cat_ls, 5, "rectangle", shei, lc_class = 1:4)
# load required data data(cat_ls) data(cont_ls) # calculate the moving window mean d <- winmove(cont_ls, 5, "rectangle", mean) # calculate the moving window Shannon evenness d <- winmove(cat_ls, 5, "rectangle", shei, lc_class = 1:4)
Calculate the mean moving window value for a given radius, shape and function for each cell in a larger resolution grid.
winmove_agg( coarse_dat, fine_dat, d, type = c("circle", "rectangle"), win_fun, agg_fun = mean, is_grid = TRUE, quiet = FALSE, ... )
winmove_agg( coarse_dat, fine_dat, d, type = c("circle", "rectangle"), win_fun, agg_fun = mean, is_grid = TRUE, quiet = FALSE, ... )
coarse_dat |
sf, Raster* or Spatial* object. The coarse grain data (response data) across which to calculate the aggregated moving window function |
fine_dat |
Raster* object. The fine grain data (predictor / covariate data) to aggregate |
d |
numeric. If |
type |
character. The shape of the moving window |
win_fun |
character. The function to apply to the moving window. The
function |
agg_fun |
character. The function by which to aggregate. By default this
is set to |
is_grid |
logical. Use |
quiet |
logical. If |
... |
further arguments passed to or from other methods |
grainchanger
has several built-in functions. Functions
currently included are:
shdi
- Shannon diversity, requires the additional argument lc_class
(vector or scalar)
shei
- Shannon evenness, requires the additional argument lc_class
(vector or scalar)
prop
- Proportion, requires the additional argument lc_class
(scalar)
var_range
- Range (max - min)
Note that winmove_agg
can be run in parallel using plan(multiprocess)
from the future
package.
Numeric vector containing moving window values calculated for each grid cell
## Not run: # load required data data(g_sf) data(cont_ls) data(cat_ls) # aggregate using mean d <- winmove_agg(g_sf, cont_ls, 5, "rectangle", mean) # aggregate using Shannon evenness d <- winmove_agg(g_sf, cat_ls, 5, "rectangle", shei, lc_class = 1:4) ## End(Not run)
## Not run: # load required data data(g_sf) data(cont_ls) data(cat_ls) # aggregate using mean d <- winmove_agg(g_sf, cont_ls, 5, "rectangle", mean) # aggregate using Shannon evenness d <- winmove_agg(g_sf, cat_ls, 5, "rectangle", shei, lc_class = 1:4) ## End(Not run)
An S4 class for use with winmove functions (extends RasterLayer). Objects
will need to be set to this class in order to be used with the inbuilt winmove
functions (e.g. mean
, prop
, var_range
, shdi
, shei
)
Slots for RasterLayer and RasterBrick objects
title
:Character
file
:Object of class ".RasterFile"
data
:Object of class ".SingleLayerData"
or ".MultipleLayerData"
history
:To record processing history, not yet in use
legend
:Object of class .RasterLegend
, Default legend. Should store preferences for plotting. Not yet implemented except that it stores the color table of images, if available
extent
:Object of Extent-class
ncols
:Integer
nrows
:Integer
crs
:Object of class "CRS"
, i.e. the coordinate reference system. In Spatial* objects this slot is called 'proj4string'
# load required data data(cat_ls) # set \code{cat_ls} to object of class winmove new("winmove", cat_ls)
# load required data data(cat_ls) # set \code{cat_ls} to object of class winmove new("winmove", cat_ls)