flowPath,GRaster-method | R Documentation |
This function finds the least-cost pathway from a set of starting points to the lowest cells accessible from them while, in each step, traversing "down" slope gradients. It is intended to depict the path a drop of water would take when flowing across a landscape. For a single starting point, the defaults settings will produce a raster with cells with values of 1 along the path. All other cells will be set to NA
.
## S4 method for signature 'GRaster'
flowPath(x, y, return = "ID")
x |
A |
y |
A "points" |
return |
Character: Indicates the type of values "burned" into the cells of the output raster. Case is ignored and partial matching is used, but only one option can be selected.
|
A GRaster
.
flow()
, streams()
, the GRASS module r.drain
(see grassHelp("r.drain")
)
if (grassStarted()) {
# Setup
library(terra)
# Example data
madElev <- fastData("madElev")
madCoast4 <- fastData("madCoast4")
# Convert to GRaster and crop to a sub-portion (easier for visualizing)
elev <- fast(madElev)
coast4 <- fast(madCoast4)
ant <- coast4[coast4$NAME_4 == "Antanambe"]
elevAnt <- crop(elev, ant)
# Create a set of random points to serve as starting points:
starts <- spatSample(elevAnt, 10, as.points = TRUE, seed = 2)
# Remove points in water:
starts <- starts[complete.cases(starts)]
# Calculate flow paths and label each by ID:
paths <- flowPath(elevAnt, starts)
paths
plot(elevAnt, legend = FALSE, main = "Flow path for each point")
plot(paths, add = TRUE)
plot(starts, pch = 1, add = TRUE)
# Flow paths with cell values indicating number of cells from each start:
seqs <- flowPath(elevAnt, starts, return = "seq")
plot(elevAnt, legend = FALSE, main = "Sequentially-numbered flow paths")
plot(seqs, add = TRUE)
plot(starts, pch = 1, add = TRUE)
# We can convert flow paths to lines:
seqLines <- as.lines(seqs)
plot(seqLines)
seqLines
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.