Computes DEM derivatives from a .vec elevation raster with Horn's 3x3
method, on the same haloed tile-row strip pass as focal() – the input is
read one strip at a time and, when path is given, the outputs are streamed
straight back to a multi-band .vec. Matches terra's
terrain() / shade() conventions.
Arguments
- x
A
vectra_raster(fromvec_open_raster()) or a path to a.vecelevation raster.- v
Derivatives to compute, any of
"slope","aspect","hillshade","TPI"(topographic position index),"roughness","TRI"(terrain ruggedness index). The return follows the input: one matrix for a singlev, a named list for several.- unit
Angular unit for
slopeandaspect:"degrees"(default) or"radians".- azimuth, altitude
Sun position for
"hillshade", in degrees. Defaults 315 (NW) and 45.- band
Band to read (1-based). Default 1.
- path
Optional output
.vecpath (one band perv, named afterv). When given the result is streamed to disk and the openedvec_open_raster()handle is returned invisibly; whenNULLthe result is returned in memory.- dtype
Storage dtype for
.vecoutput (seevec_write_raster()). Default"f32".- compression
Compression effort for
.vecoutput. Default"fast".
Value
When path is NULL: a numeric matrix for a single v, or a named
list of matrices for several, each carrying gt, extent, and crs
attributes (row 1 northmost). When path is given, the written multi-band
vectra_raster handle (invisibly).
Details
Slope and aspect use the Horn (1981) finite-difference gradient over
the 3x3 neighbourhood; aspect is degrees clockwise from north (flat cells
return 90). hillshade is the cosine of the incidence angle for the given
sun position, clamped at 0. TPI is the cell minus the mean of its eight
neighbours; roughness is the range over the 3x3; TRI is the mean
absolute difference to the eight neighbours. Cells whose 3x3 neighbourhood
touches a nodata value or the raster edge return NA.
See also
focal() for arbitrary moving windows.
Examples
# A tilted surface so slope and aspect are well defined.
z <- outer(1:8, 1:8, function(r, c) 10 + 2 * c + r)
f <- tempfile(fileext = ".vec")
vec_write_raster(z, f, dtype = "f64", extent = c(0, 0, 8, 8))
slp <- terrain(f, v = "slope")
deriv <- terrain(f, v = c("slope", "aspect", "hillshade"))
names(deriv)
unlink(f)