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] (ORCID: <https://orcid.org/0000-0002-3611-7281>), 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: | 2025-06-15 07:33:42 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 withnlm_mpd()
fromNLMR
and classified withutil_classify()
fromlandscapetools
.
Sciaini M, Fritsch M, Scherer C, Simpkins CE. NLMR and landscapetools: An integrated environmentfor 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 withnlm_mpd()
fromNLMR
.
Sciaini M, Fritsch M, Scherer C, Simpkins CE. NLMR and landscapetools: An integrated environmentfor 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 theeffect 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 theraster) |
A torus is an infinite surface where the top joins the bottom, andthe left side meets the right side. See https://en.wikipedia.org/wiki/Torusfor a full mathematical description.
In this function, the torus effect is achieved by adding the specifiednumber of rows of the top of the raster to the bottom (and vice versa) andthe specified number of rows of the right of the raster to the left (andvice versa)
raster. Original raster padded by r cells with torus effect (seeDetails)
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 metriccalculation |
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 Shannonevenness. Open a new issue (https://github.com/laurajanegraham/grainchanger/issues)to request additional diversity metrics.
Ifclass(x) == "winmove"
, a smoothed raster with the diversitymetric calculated within the specified moving window
Ifclass(x) == "numeric"
, a single value representing the diversity metric inx
McGarigal, K. and Marks, B.J., 1995. FRAGSTATS: spatial pattern analysisprogram 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 datadata(cat_ls)# convert data to object of class winmovecat_ls <- new("winmove", cat_ls)# calculate Shannon diversity in a rectangular window of dimension 5d <- shdi(cat_ls, d = 5, type = "rectangle", lc_class = 1:4)# convert data to object of class numericcat_ls <- raster::values(cat_ls)# calculate Shannon evennessd <- shei(cat_ls, lc_class = 1:4)
# load required datadata(cat_ls)# convert data to object of class winmovecat_ls<- new("winmove", cat_ls)# calculate Shannon diversity in a rectangular window of dimension 5d<- shdi(cat_ls, d=5, type="rectangle", lc_class=1:4)# convert data to object of class numericcat_ls<- raster::values(cat_ls)# calculate Shannon evennessd<- 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 withnlm_mpd()
and converted to sf.
Sciaini M, Fritsch M, Scherer C, Simpkins CE. NLMR and landscapetools: An integrated environmentfor 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 tomean
for objects of classwinmove
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 movingwindow |
... | 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 specifiedmoving window
# load required datadata(cont_ls)# convert data to object of class winmovecont_ls <- new("winmove", cont_ls)# aggregate using a circular window with radius 3d <- mean(cont_ls, d = 3, type = "circle")
# load required datadata(cont_ls)# convert data to object of class winmovecont_ls<- new("winmove", cont_ls)# aggregate using a circular window with radius 3d<- mean(cont_ls, d=3, type="circle")
Calculate the value for a given function for each cell in a larger resolutiongrid.
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 takemultiple numbers, and return a single number. For example mean, modal, minor max. It should also accept a na.rm argument (or ignore it, e.g. as oneof 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. Functionscurrently included are:
shdi
- Shannon diversity, requires the additional argumentlc_class
(vector or scalar)
shei
- Shannon evenness, requires the additional argumentlc_class
(vector or scalar)
prop
- Proportion, requires the additional argumentlc_class
(scalar)
var_range
- Range (max - min)
Note thatnomove_agg
can be run in parallel usingplan(multiprocess)
from thefuture
package.
Raster (if input is Raster) or numeric vector (if input is sp or sfobject) containing values calculated for each coarser cell
# load required datadata(g_sf)data(cont_ls)data(cat_ls)# aggregate using meand <- nomove_agg(g_sf, cont_ls, mean)# aggregate using Shannon evennessd <- nomove_agg(g_sf, cont_ls, shei, lc_class = 1:4)
# load required datadata(g_sf)data(cont_ls)data(cat_ls)# aggregate using meand<- nomove_agg(g_sf, cont_ls, mean)# aggregate using Shannon evennessd<- 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 withsf::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 forcalculating land-cover or soil type proportions. Should be used with a categoricalraster
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 |
Ifclass(x) == "winmove"
, a smoothed raster with the proportion ofcells of the given class calculated within the specified moving window
Ifclass(x) == "numeric"
, a single value representing the proportion of valuesof a given class inx
# load required datadata(cat_ls)# convert data to object of class winmovecat_ls <- new("winmove", cat_ls)# aggregate using a rectangular window with dimension 5 for class 3d <- prop(cat_ls, d = 5, type = "rectangle", lc_class = 3)# convert data to object of class numericcat_ls <- raster::values(cat_ls)d <- prop(cat_ls, lc_class = 2)
# load required datadata(cat_ls)# convert data to object of class winmovecat_ls<- new("winmove", cat_ls)# aggregate using a rectangular window with dimension 5 for class 3d<- prop(cat_ls, d=5, type="rectangle", lc_class=3)# convert data to object of class numericcat_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 |
Ifclass(x) == "winmove"
, a smoothed raster with the size of the range of values calculated within the specifiedmoving window
Ifclass(x) == "numeric"
, a single value representing the size of the range of values inx
# load required datadata(cat_ls)data(cont_ls)# convert data to object of class winmovecat_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 numericcont_ls <- raster::values(cont_ls)d <- range(cont_ls)
# load required datadata(cat_ls)data(cont_ls)# convert data to object of class winmovecat_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 numericcont_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 inbuiltgrainchanger functions, the function should take multiple numbers, and return asingle number. For example |
... | further arguments passed to or from other methods |
grainchanger
has several built-in functions. Functions currentlyincluded are:
wm_shei
- Shannon evenness, requires theadditional argumentlc_class
(vector or scalar)
wm_prop
-Proportion, requires the additional argumentlc_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 datadata(cat_ls)data(cont_ls)# calculate the moving window meand <- winmove(cont_ls, 5, "rectangle", mean)# calculate the moving window Shannon evennessd <- winmove(cat_ls, 5, "rectangle", shei, lc_class = 1:4)
# load required datadata(cat_ls)data(cont_ls)# calculate the moving window meand<- winmove(cont_ls,5,"rectangle", mean)# calculate the moving window Shannon evennessd<- winmove(cat_ls,5,"rectangle", shei, lc_class=1:4)
Calculate the mean moving window value for a given radius, shape and functionfor 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 windowfunction |
fine_dat | Raster* object. The fine grain data (predictor / covariatedata) to aggregate |
d | numeric. If |
type | character. The shape of the moving window |
win_fun | character. The function to apply to the moving window. Thefunction |
agg_fun | character. The function by which to aggregate. By default thisis set to |
is_grid | logical. Use |
quiet | logical. If |
... | further arguments passed to or from other methods |
grainchanger
has several built-in functions. Functionscurrently included are:
shdi
- Shannon diversity, requires the additional argumentlc_class
(vector or scalar)
shei
- Shannon evenness, requires the additional argumentlc_class
(vector or scalar)
prop
- Proportion, requires the additional argumentlc_class
(scalar)
var_range
- Range (max - min)
Note thatwinmove_agg
can be run in parallel usingplan(multiprocess)
from thefuture
package.
Numeric vector containing moving window values calculated for eachgrid cell
## Not run: # load required datadata(g_sf)data(cont_ls)data(cat_ls)# aggregate using meand <- winmove_agg(g_sf, cont_ls, 5, "rectangle", mean)# aggregate using Shannon evennessd <- winmove_agg(g_sf, cat_ls, 5, "rectangle", shei, lc_class = 1:4)## End(Not run)
## Not run:# load required datadata(g_sf)data(cont_ls)data(cat_ls)# aggregate using meand<- winmove_agg(g_sf, cont_ls,5,"rectangle", mean)# aggregate using Shannon evennessd<- 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). Objectswill need to be set to this class in order to be used with the inbuiltwinmove
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 ofExtent-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 datadata(cat_ls)# set \code{cat_ls} to object of class winmovenew("winmove", cat_ls)
# load required datadata(cat_ls)# set \code{cat_ls} to object of class winmovenew("winmove", cat_ls)