R 估计未定义曲面的梯度

R 估计未定义曲面的梯度,r,surface,raster,terrain,gradient,R,Surface,Raster,Terrain,Gradient,我想估计未定义曲面(即函数未知)的坡度(坡度和坡向)。为了测试我的方法,以下是测试数据: 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)) name

我想估计未定义曲面(即函数未知)的坡度(坡度和坡向)。为了测试我的方法,以下是测试数据:

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)
但是,我需要一个斜率值矩阵。据我所知,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))
如果我画斜率和坡向,它们似乎和矢量图不一致

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

  • 矢量图必须平滑坡度,但如何平滑
  • 我相当肯定,
    graster::terrain
    正在使用一种流动窗口方法来计算坡度。也许窗户太小了。。。这能扩大吗
  • 我这样做是不是不恰当?否则我如何估计未定义曲面的坡度
    我使用
    光栅
    中的函数使用您的数据构建一个
    光栅层

    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(test.rast)
    
    vectorplot(test.rast)
    

    如果您只需要坡度,则可以使用
    地形

    slope <- terrain(test.rast, unit='degrees')
    
    levelplot(slope, par.settings=BTCTheme())
    
    由于原始光栅层
    的结构 水平分量几乎是常数,所以让我们画 注意垂直部分。下一个代码覆盖 此垂直分量上的向量场箭头

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

    最后,如果需要坡度和坡向的值,请使用
    getValues

    saVals <- getValues(sa)
    

    saVals您也可以为此使用
    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)
    
    库(成像仪)
    #查看(打印)图像矩阵
    图像(真实)
    #计算梯度
    
    渐变该对象的数据槽中的单位不是“度”,而是“”。此外,test.rast不是一个S3对象,它返回一个带有
    test.rast$slope
    的错误。我看你拼写了“terr.mast”。你有其他东西在附近砰砰响吗???谢谢你的帮助。这正是我想要的。
    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)
        })
    
    levelplot(dXY, layers=2, par.settings=RdBuTheme()) +
        vectorplot(test.rast, region=FALSE)
    
    saVals <- getValues(sa)
    
    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)