distance,GRaster,missing-method | R Documentation |
This function produces a raster or a matrix of geographic distances, depending on the input:
Case 1: Argument x
is a GRaster
and y
is missing: By default, this function replaces values in all NA
cells with the distance between them and their closest non-NA
cell. Alternatively, all non-NA
cells can have their values replaced by the distance to NA
cells. You can also specify which cells (by value) have their values replaced by distance to other cells.
Case 2: Argument x
is a GRaster
and y
is a GVector
: All cells in the raster have their value replaced by the distance to the nearest features in the GVector
. Alternatively, calculate the distance from any cell covered by a vector object and the nearest cell not covered by a vector object. Note that the vector is rasterized first.
Case 3: Argument x
is a GVector
and y
is a GVector
: A matrix of pairwise distances between all features in one versus the other GVector
is returned.
Case 4: Argument x
is a GVector
and y
is missing: A matrix of pairwise distances between all features in the GVector
is returned.
## S4 method for signature 'GRaster,missing'
distance(
x,
y,
target = NA,
fillNA = TRUE,
unit = "meters",
method = ifelse(is.lonlat(x), "geodesic", "Euclidean"),
minDist = NULL,
maxDist = NULL
)
## S4 method for signature 'GRaster,GVector'
distance(
x,
y,
fillNA = TRUE,
thick = TRUE,
unit = "meters",
method = ifelse(is.lonlat(x), "geodesic", "Euclidean"),
minDist = NULL,
maxDist = NULL
)
## S4 method for signature 'GVector,GVector'
distance(x, y, unit = "meters", minDist = NULL, maxDist = NULL)
## S4 method for signature 'GVector,missing'
distance(x, y, unit = "meters", minDist = NULL, maxDist = NULL)
x |
A |
y |
Either missing, or a |
target |
Numeric: Only applicable for case 1, when |
fillNA |
Logical: Determines which raster cells to fill with distances.
|
unit |
Character: Units of the output. Any of:
Partial matching is used and case is ignored. |
method |
Character: The type of distance to calculate. Partial matching is used and capitalization is ignored. Possible values include:
|
minDist , maxDist |
Either |
thick |
Logical: Only applicable for case 2, when |
If x
is a GRaster
, then the output is a GRaster
. If x
is a GVector
, then the output is a numeric vector.
terra::distance()
; GRASS modules r.grow.distance
and v.distance
if (grassStarted()) {
# Setup
library(sf)
library(terra)
# Elevation raster, rivers vector, locations of Dypsis plants
madElev <- fastData("madElev")
madRivers <- fastData("madRivers")
madDypsis <- fastData("madDypsis")
# Convert a SpatRaster to a GRaster, and sf to a GVector
elev <- fast(madElev)
rivers <- fast(madRivers)
dypsis <- fast(madDypsis)
### case 1: GRaster by itself
# Distance between NA cells and nearest non-NA cells
naDist <- distance(elev)
names(naDist) <- "NA Distance"
plot(naDist)
# Distance between non-NA cells and nearest NA cells
nonNaDist <- distance(elev, fillNA = FALSE)
names(nonNaDist) <- "non-NA Distance"
plot(nonNaDist)
# Distance between cells with an elevation of 3 and any other cell that != 3
distFocal3 <- distance(elev, target = 3)
names(distFocal3) <- "Distance from 3"
plot(distFocal3)
# Distance between any cell and cells with a value of 3
distTo3 <- distance(elev, fillNA = FALSE, target = 3)
names(distTo3) <- "Distance to 3"
plot(distTo3)
### Case 2: GRaster and GVector
distToVect <- distance(elev, rivers)
plot(distToVect)
plot(rivers, add = TRUE)
### Case 3: GVector vs GVector
plot(rivers)
plot(dypsis, add = TRUE)
distToRivers <- distance(dypsis, rivers, unit = "yd")
distToPlants <- distance(rivers, dypsis)
distToRivers
distToPlants
### Case 4: GVector vs itself
distToItself <- distance(dypsis)
distToItself
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.