I want to evaluate the gradient (slope and aspect) of an undefined surface (i.e. the function is unknown). To test my methods, here are the test data:
require(raster); require(rasterVis) set.seed(123) x <- runif(100, min = 0, max = 1) y <- runif(100, min = 0, max = 1) e <- 0.5 * rnorm(100) test <- expand.grid(sort(x),sort(y)) names(test)<-c('X','Y') z1 <- (5 * test$X^3 + sin(3*pi*test$Y)) realy <- matrix(z1, 100, 100, byrow = F)
I convert everything to raster and sketch using rasterVis::vectorplot . Everything looks good. The vector field indicates the direction of the highest magnitude of the change, and the shading is similar to my outline graph ...
test.rast <- raster(t(realy), xmn = 0, xmx = 1, ymn = 0, ymx = 1, crs = CRS("+proj")) vectorplot(test.rast, par.settings=RdBuTheme, narrow = 100, reverse = T)
However, I need a slope value matrix. Since I understand vectorplot, it uses the raster :: terrain function:
terr.mast <- list("slope" = matrix(nrow = 100, ncol = 100, terrain(test.rast, opt = "slope", unit = "degrees", reverse = TRUE, neighbors = 8)@ data@values , byrow = T), "aspect" = matrix(nrow = 100, ncol = 100, terrain(test.rast, opt = "aspect", unit = "degrees", reverse = TRUE, neighbors = 8)@ data@values , byrow = T))
however, the slope seems really high ... (90 degrees will be vertical, right ?!)
terr.mast$slope[2:6,2:6]
and if I draw the slope and aspect, they don't seem to be consistent with the vectorplot graphics.
plot(terrain(test.rast, opt = c("slope", "aspect"), unit = "degrees", reverse = TRUE, neighbors = 8))
My thoughts:
- Vectorplot should smooth the slope, but how?
- I'm pretty sure
raster::terrain uses the roving method to calculate the slope. Perhaps the window is too small ... can it be expanded? - Am I thinking it inappropriately? How else can I evaluate the surface slope undefined?