Skip to content

Commit

Permalink
for #1257
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Sep 9, 2023
1 parent f4e02ac commit 29ca1f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 9 additions & 2 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ function(x, y, ...) {



extractAlong <- function(x, y, ID=TRUE, cells=FALSE, xy=FALSE) {
extractAlong <- function(x, y, ID=TRUE, cells=FALSE, xy=FALSE, online=FALSE) {

stopifnot(inherits(x, "SpatRaster"))
if (inherits(y, "sf")) {
Expand Down Expand Up @@ -551,7 +551,14 @@ extractAlong <- function(x, y, ID=TRUE, cells=FALSE, xy=FALSE) {
} else {
colnames(res) <- c("ID", "cell", names(x))
if (xy) {
res <- data.frame(res[,1:2], xyFromCell(x, res$cell), res[, -c(1:2), drop=FALSE])
xycrd <- xyFromCell(x, res$cell)
if (online) {
pts <- vect(xycrd, crs="local")
crs(y) <- "local"
n <- nearest(pts, y)
xycrd <- crds(n)
}
res <- data.frame(res[,1:2], xycrd, res[, -c(1:2), drop=FALSE])
}
}
if (!cells) res$cell <- NULL
Expand Down
5 changes: 3 additions & 2 deletions man/extractAlong.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ Extract raster values along a line. That is, the returned values are ordered alo
}

\usage{
extractAlong(x, y, ID=TRUE, cells=FALSE, xy=FALSE)
extractAlong(x, y, ID=TRUE, cells=FALSE, xy=FALSE, online=FALSE)
}

\arguments{
\item{x}{SpatRaster}
\item{y}{SpatVector with lines geometry}
\item{ID}{logical. Should an ID column be added? If so, the first column returned has the IDs (record numbers) of input SpatVector \code{y}}
\item{cells}{logical. If \code{TRUE} the cell numbers are also returned}
\item{xy}{logical. If \code{TRUE} the coordinates of the cells are also returned. See \code{\link{xyFromCell}}}
\item{xy}{logical. If \code{TRUE} the coordinates of the cells traversed by \code{y} are also returned. See \code{\link{xyFromCell}}}
\item{online}{logical. If \code{TRUE} the returned coordinates are snapped to \code{y}}
}

\value{
Expand Down

0 comments on commit 29ca1f1

Please sign in to comment.