Movatterモバイル変換


[0]ホーム

URL:


Package 'grainchanger'

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

Help Index


Example categorical raster (fine_dat)

Description

An example map to show functionality on categorical surfaces.

Usage

cat_ls

Format

A raster layer object.

Details

Generated withnlm_mpd() fromNLMR and classified withutil_classify() fromlandscapetools.

Source

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


Example continuous raster (fine_dat)

Description

An example map to show functionality on continuous surfaces.

Usage

cont_ls

Format

A raster layer object.

Details

Generated withnlm_mpd() fromNLMR.

Source

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


Pad a raster by a specified radius

Description

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

Usage

create_torus(dat, dpad)

Arguments

dat

The raster dataset to pad

dpad

The amount by which to pad the raster (in the same units as theraster)

Details

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)

Value

raster. Original raster padded by r cells with torus effect (seeDetails)

Examples

data(cat_ls)d<- create_torus(dat= cat_ls, dpad=5)

Diversity metrics

Description

A range of functions to calculate well known landcover diversity metrics

Usage

## 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,...)

Arguments

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. Iftype=circle, the radius of the circle (in units of theCRS). Iftype=rectangle the dimension of the rectangle (one or two numbers)

type

character. The shape of the moving window

...

further arguments passed to or from other methods

Details

Currently provided diversity metrics are Shannon diversity and Shannonevenness. Open a new issue (https://github.com/laurajanegraham/grainchanger/issues)to request additional diversity metrics.

Value

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

References

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.

Examples

# 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)

Example grid (coarse_dat)

Description

An example grid to show functionality when aggregating using an sf object.

Usage

g_sf

Format

An sf object.

Details

Generated withnlm_mpd() and converted to sf.

Source

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


Arithmetic mean

Description

An extension tomean for objects of classwinmove

Usage

mean(x,...)## S3 method for class 'winmove'mean(x, d, type,...)

Arguments

x

RasterLayer. The data over which to calculate the mean value within a movingwindow

...

further arguments passed to or from other methods

d

numeric. Iftype=circle, the radius of the circle (in units of theCRS). Iftype=rectangle the dimension of the rectangle (one or two numbers)

type

character. The shape of the moving window

Value

RasterLayer. A smoothed raster with the mean calculated within the specifiedmoving window

Examples

# 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")

Direct data aggregation

Description

Calculate the value for a given function for each cell in a larger resolutiongrid.

Usage

nomove_agg(coarse_dat, fine_dat, agg_fun, is_grid=TRUE, quiet=FALSE,...)

Arguments

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. UseTRUE (default) ifg contains onlyrectangular cells (i.e. a grid). Ifg is any other polygon file,this should be set to false

quiet

logical. IfFALSE (default) andis_grid == TRUEthe user gets a warning that the aggregation assumes all cells arerectangular

...

further arguments passed to or from other methods

Details

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.

Value

Raster (if input is Raster) or numeric vector (if input is sp or sfobject) containing values calculated for each coarser cell

Examples

# 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)

Example polygon (coarse_dat)

Description

An example non-gridded coarse data to show functionality when aggregating using an sf object.

Usage

poly_sf

Format

An sf object.

Details

Generated withsf::st_make_grid(sf::st_as_sfc(sf::st_bbox(cont_ls)), cellsize = 13, square = FALSE)


Calculate proportion of a given value

Description

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

Usage

prop(x, lc_class,...)## S3 method for class 'winmove'prop(x, lc_class, d, type,...)## S3 method for class 'numeric'prop(x, lc_class,...)

Arguments

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. Iftype=circle, the radius of the circle (in units of theCRS). Iftype=rectangle the dimension of the rectangle (one or two numbers)

type

character. The shape of the moving window

Value

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

Examples

# 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)

Size of range of values

Description

Calculates the difference between the maximum and minimum value

Usage

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,...)

Arguments

x

RasterLayer. The data over which to calculate the range size

...

further arguments passed to or from other methods

d

numeric. Iftype=circle, the radius of the circle (in units of theCRS). Iftype=rectangle the dimension of the rectangle (one or two numbers)

type

character. The shape of the moving window

na.rm

logical. indicates whetherNA values should be stripped before thecomputation proceeds.na.rm = TRUE is the default

Value

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

Examples

# 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)

Create moving window surface

Description

Smooth a raster surface using a moving window with a given function, radius and shape.

Usage

winmove(fine_dat, d, type= c("circle","rectangle"), win_fun,...)

Arguments

fine_dat

The raster dataset on which to calculate the moving window function

d

numeric. Iftype=circle, the radius of the circle (in units of theCRS). Iftype=rectangle the dimension of the rectangle (one or two numbers).

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 examplemean,modal,min ormax. It should also accept ana.rmargument (or ignore it, e.g. as one of the 'dots' arguments. For example, length willfail, butfunction(x, ...){na.omit(length(x))} works. See Details

...

further arguments passed to or from other methods

Details

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)

Value

RasterLayer. A smoothed raster with the moving window values calculated

Examples

# 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)

Moving-window data aggregation

Description

Calculate the mean moving window value for a given radius, shape and functionfor each cell in a larger resolution grid.

Usage

winmove_agg(  coarse_dat,  fine_dat,  d,  type= c("circle","rectangle"),  win_fun,  agg_fun= mean,  is_grid=TRUE,  quiet=FALSE,...)

Arguments

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. Iftype=circle, the radius of the circle (in unitsof the CRS). Iftype=rectangle the dimension of the rectangle (oneor two numbers).

type

character. The shape of the moving window

win_fun

character. The function to apply to the moving window. Thefunctionwin_fun should take multiple numbers, and return a single number. Forexamplemean,modal,min ormax. It should also accept ana.rm argument (orignore it, e.g. as one of the 'dots' arguments. For example,length willfail, butfunction(x, ...){na.omit(length(x))} works. See Details

agg_fun

character. The function by which to aggregate. By default thisis set tomean

is_grid

logical. UseTRUE (default) ifg contains onlyrectangular cells (i.e. a grid). Ifg is any other polygon file,this should be set to false

quiet

logical. IfFALSE (default) andis_grid == TRUEthe user gets a warning that the aggregation assumes all cells arerectangular

...

further arguments passed to or from other methods

Details

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.

Value

Numeric vector containing moving window values calculated for eachgrid cell

Examples

## 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)

Description

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 inbuiltwinmovefunctions (e.g.mean,prop,var_range,shdi,shei)

Slots

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'

Examples

# load required datadata(cat_ls)# set \code{cat_ls} to object of class winmovenew("winmove", cat_ls)


[8]ページ先頭

©2009-2025 Movatter.jp