ExtractAlongTransect: Extract Raster Values Along a Transect Line

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/ExtractAlongTransect.R

Description

Extract values from raster layer(s) along a user defined transect line.

Usage

1
ExtractAlongTransect(transect, r)

Arguments

transect

'SpatialPoints' or 'SpatialLines'. Transect line or its vertices.

r

'RasterLayer', 'RasterStack', or 'RasterBrick'. Raster layer(s)

Details

The transect line is described using a simple polygonal chain. The transect line and raster layer(s) must be specified in a coordinate reference system.

Value

A 'list' with components of class 'SpatialPointsDataFrame'. These components represent continuous piecewise line segments along the transect. The following variables are specified for each coordinate point in the line segment:

dist

distance along the transect line.

2, ..., n

extracted value for each raster layer in r, where column names match their respective raster layer name.

Author(s)

J.C. Fisher, U.S. Geological Survey, Idaho Water Science Center

See Also

PlotCrossSection

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
coords <- rbind(c(-100, -90), c(80, 90), c(80, 0), c(40, -40))
crs <- sp::CRS("+init=epsg:4326")
transect <- sp::SpatialPoints(coords, proj4string = crs)
r <- raster::raster(nrows = 10, ncols = 10,
                    ymn = -80, ymx = 80, crs = crs)
names(r) <- "value"
set.seed(0)
r[] <- runif(raster::ncell(r))
r[4, 6] <- NA
PlotMap(r)
l <- sp::Lines(list(sp::Line(coords)), ID = "Transect")
lines(sp::SpatialLines(list(l), proj4string = crs))
points(transect, pch = 19)
segs <- ExtractAlongTransect(transect, r)
for (i in seq_along(segs)) points(segs[[i]])

dev.new()
xlab <- "Distance along transect"
ylab <- "Raster value"
xlim <- range(vapply(segs, function(i) {
  range(i@data[, "dist"])
}, c(0, 0)))
ylim <- range(vapply(segs, function(i) {
  range(i@data[, "value"], na.rm = TRUE)
}, c(0, 0)))
PlotGraph(NA, xlab = xlab, ylab = ylab,
          xlim = xlim, ylim = ylim, type = "n")
cols <- GetColors(length(segs), scheme = "bright")
for (i in seq_along(segs))
  lines(segs[[i]]@data[, c("dist", "value")],
        col = cols[i], lwd = 2)
coords <- sp::coordinates(transect)
n <- length(transect)
d <- cumsum(c(0, as.matrix(dist((coords)))[cbind(1:(n - 1), 2:n)]))
abline(v = d, lty = 2)
mtext(sprintf("(%d, %d)", coords[1, 1], coords[1, 2]),
      line = -1, adj = 0, cex = 0.7)
mtext(sprintf("(%d, %d)", coords[n, 1], coords[n, 2]),
      line = -1, adj = 1, cex = 0.7)

graphics.off()

inlmisc documentation built on Jan. 25, 2022, 1:14 a.m.