估计未定义曲面的渐变

时间:2013-05-03 18:14:56

标签: r surface raster terrain gradients

我想估计 undefined 曲面的渐变(斜率和宽高比)(即函数未知)。为了测试我的方法,这里是测试数据:

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)
# And a few plots for demonstration #
persp(sort(x), sort(y), realy, 
      xlab = 'X', ylab = "Y", zlab = 'Z',
      main = 'Real function (3d)', theta = 30, 
      phi = 30, ticktype = "simple", cex=1.4)
      contour(sort(x), sort(y), realy, 
      xlab = 'X', ylab = "Y",
      main = 'Real function (contours)', cex=1.4)

我使用rasterVis::vectorplot将所有内容转换为栅格和绘图。一切都很好看。矢量场指示最高变化幅度的方向,阴影类似于我的等高线图...

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)

但是,我需要一个斜率值矩阵。据我了解vectorplot,它使用raster :: terrain函数:

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))
然而,坡度似乎很高......(90度是垂直的,对吧?!)

terr.mast$slope[2:6,2:6] 
#         [,1]     [,2]     [,3]     [,4]     [,5]
#[1,] 87.96546 87.96546 87.96546 87.96550 87.96551
#[2,] 84.68628 84.68628 84.68627 84.68702 84.68709
#[3,] 84.41349 84.41350 84.41349 84.41436 84.41444
#[4,] 84.71757 84.71757 84.71756 84.71830 84.71837
#[5,] 79.48740 79.48741 79.48735 79.49315 79.49367

如果我绘制坡度和坡度,他们似乎不同意vectorplot图形。

plot(terrain(test.rast, opt = c("slope", "aspect"), unit = "degrees", 
     reverse = TRUE, neighbors = 8))

我的想法:

  1. Vectorplot必须平滑斜率,但是如何?
  2. 我很确定raster::terrain正在使用流动窗口方法来计算斜率。也许窗户太小了......这可以扩大吗?
  3. 我是否以不恰当的方式谈论这件事?我怎么能估计未定义表面的斜率?

2 个答案:

答案 0 :(得分:11)

我使用RasterLayer

中的函数为您的数据构建raster
library(raster)
library(rasterVis)

test.rast <- raster(ncol=100, nrow=100, xmn = 0, xmx = 1,  ymn = 0, ymx = 1)
xy <- xyFromCell(test.rast, 1:ncell(test.rast))
test.rast[] <- 5*xy[,1] + sin(3*pi*xy[,2])

让我们用levelplot

显示这个对象
levelplot(test.rast)

levelplot

vectorplot的渐变矢量字段:

vectorplot(test.rast)

vectorplot

如果您只需要坡度,请使用terrain

slope <- terrain(test.rast, unit='degrees')

levelplot(slope, par.settings=BTCTheme())

slope

但是,如果我理解你,你真的需要渐变,所以 你应该计算斜率和方面:

sa <- terrain(test.rast, opt=c('slope', 'aspect'))

为了理解vectorplot绘制箭头的方式, 这里我展示了它(修改过的)代码中水平部分 计算箭头的垂直分量:

dXY <- overlay(sa, fun=function(slope, aspect, ...){
    dx <- slope*sin(aspect) ##sin due to the angular definition of aspect
    dy <- slope*cos(aspect)
    c(dx, dy)
    })

由于原始RasterLayer的结构,所以 水平分量几乎是不变的,所以让我们画出来 注意垂直分量。下一个代码覆盖了 这个垂直分量上的矢量场的箭头。

levelplot(dXY, layers=2, par.settings=RdBuTheme()) +
    vectorplot(test.rast, region=FALSE)

dY

最后,如果您需要使用斜率和方面的值 getValues

saVals <- getValues(sa)

答案 1 :(得分:0)

您还可以为此使用imager函数:

library(imager)

# view (plot) image matrix
image(realy)

# compute gradient
gradient <- imgradient(as.cimg(realy), "xy")

# view x and y gradients
plot(gradient$x)
plot(gradient$y)

# access matrix values
mat.x <- as.matrix(gradient$x)
mat.y <- as.matrix(gradient$y)