buffer,GRaster-method | R Documentation |
Buffers can be constructed for GRaster
s or GVector
s. For rasters, the buffer()
function creates a buffer around non-NA
cells. The output will be a raster. For vectors, the buffer()
and st_buffer()
functions create a vector polygon larger or smaller than the focal vector.
## S4 method for signature 'GRaster'
buffer(
x,
width,
unit = "meters",
method = "Euclidean",
background = 0,
lowMemory = FALSE
)
## S4 method for signature 'GVector'
buffer(x, width, capstyle = "round", dissolve = TRUE)
## S4 method for signature 'GVector'
st_buffer(x, dist, endCapStyle = "round", dissolve = FALSE)
x |
A |
width |
Numeric: For rasters – Maximum distance cells must be from focal cells to be within the buffer. For rasters, if the buffering unit is For vectors, distance from the object to place the buffer. Negative values create "inside" buffers. Units are in the same units as the current coordinate reference system (e.g., degrees for WGS84 or NAD83, often meters for projected systems). |
unit |
Character: Rasters – Indicates the units of * `"cells"`: Units are numbers of cells.
|
method |
Character: Rasters – Only used if
Partial matching is used and case is ignored. |
background |
Numeric: Rasters – Value to assign to cells that are not |
lowMemory |
Logical: Rasters – Only used if buffering a raster and |
capstyle , endCapStyle |
Character: Vectors – Style for ending the "cap" of buffers around lines. Valid options include |
dissolve |
Logical ( |
dist |
Vectors – Same as |
Note that in some cases, topologically incorrect vectors can be created when buffering. This can arise when buffers intersect to create intersections that technically belong to two or more geometries. This issue can be resolved by dissolving borders between buffered geometries using dissolve = TRUE
, but as of now, there is no fix if you do not want to dissolve geometries. A workaround would be to create a different GVector
for each geometry, and then buffer each individually :(.
terra::buffer()
, sf::st_buffer()
, and modules r.buffer
, r.grow
, and v.buffer
in GRASS
if (grassStarted()) {
# Setup
library(sf)
library(terra)
# Elevation raster, rivers vector
madElev <- fastData("madElev")
madRivers <- fastData("madRivers")
# Convert a SpatRaster to a GRaster, and sf to a GVector
elev <- fast(madElev)
rivers <- fast(madRivers)
### Buffer a raster by a given distance:
buffByDist <- buffer(elev, width = 2000) # 2000-m buffer
plot(buffByDist, legend = FALSE)
plot(madElev, add = TRUE)
### Buffer a raster by a given number of cells:
buffByCells <- buffer(elev, width = 20.01, unit = "cells") # 20-cell buffer
plot(buffByCells, legend = FALSE)
plot(madElev, add = TRUE)
### Buffer a vector:
buffRivers <- buffer(rivers, width = 2000, dissolve = TRUE) # 2000-m buffer
plot(buffRivers)
plot(st_geometry(madRivers), col = "blue", add = TRUE)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.