draw_ring: Ring matrix for moving window

View source: R/draw_ring.R

draw_ringR Documentation

Ring matrix for moving window

Description

This function was written to produce rings to be used as moving windows in the function focal(). Such rings are required for instance to calculate the topographic position index (TPI) at variable scales according to Weiss (2001, http://www.jennessent.com/downloads/tpi-poster-tnc_18x22.pdf). Values of inner and outer annulus have to be provided as integers and will correspond to the respective radial length in pixel (cell) number.

Usage

draw_ring(inner, outer, squared = FALSE)

Arguments

inner

Inner annulus in pixel (cell) number.

outer

Outer annulus in pixel (cell) number.

squared

Logical value, whether the ring should be squared or round.

Value

A square matrix of dimensions outer*2 + 1 containing values 0 and 1 (1 for cells inside of the ring). Remember that the function focal() will calculate by default the sum of the cells inside the ring when provided a matrix as window. Thus you may change the values 1 by their relative weight in order to get the mean value as output (i.e. ring <- ring/sum(ring)).

Author(s)

Miguel Alvarez kamapu78@gmail.com

See Also

focal().

Examples

## Two different rings
A <- draw_ring(3, 6)
B <- draw_ring(7, 10)

A
B

## For circles, set inner=0
C <- draw_ring(0, 100)

## For squared rings, set squared=TRUE
D <- draw_ring(0, 50, squared = TRUE)

## Now see them in plot
library(terra)

par(mfrow = c(2, 2))
plot(rast(A))
plot(rast(B))
plot(rast(C))
plot(rast(D))

kamapu/spatialist documentation built on Jan. 10, 2025, 6:59 p.m.