【问题标题】:Function that don't create SpatialGridDataFrame object不创建 SpatialGridDataFrame 对象的函数
【发布时间】:2016-02-06 16:55:22
【问题描述】:

我尝试创建一个函数,将卫星图像的数字数字转换为辐射,但我不知道为什么我的最终对象是数字而不是 SpatialGridDataFrame 对象,如果我在函数类中指定(结果)==“空间网格数据帧”。 我的代码是:

require(raster)
require(sp)

RasterLayer 模拟

r <- raster(nrows=10, ncols=10)
r <- setValues(r, 1:ncell(r))
plot(r)

band2<- as(r, 'SpatialGridDataFrame')  ### convert in SpatialGridDataFrame

DN 发光功能

radconvL<-function(x, band = 2)
{
     Lmax <- switch(as.character(band), 
                    "2" = 120.64,
                    "3" = 151.31,
                    "4" = 157.57,
                    "5" = 69.03,
                    NA)

     if (is.na(Lmax)) stop("invalid band")

     Lmin = 0
     Qmax = 127
     x <- as.vector(as.matrix(x))
     results <- x

     x <- Lmin + ((Lmax-Lmin)*x)/Qmax
     if (class(results) == "SpatialGridDataFrame")
         results@data[, 1] <- x
     else if (is.data.frame(x))
         results <- data.frame(matrix(x, nrow = nrow(results),
             ncol = ncol(results)))
     else results <- x
     print(paste(band, Lmax))
     print(results)
     results
}
--

试试这个功能

teste2<-radconvL(band2, band = 2)
str(test2)## Numeric!!!! Why???

有人可以帮我吗?

谢谢,

亚历山大

【问题讨论】:

    标签: r function spatial r-raster


    【解决方案1】:

    我将向您展示如何完成这项工作:

    radconvL <- function(x, band = 2) {
         Lmax <- switch(band, 
                        "2" = 120.64,
                        "3" = 151.31,
                        "4" = 157.57,
                        "5" = 69.03,
                        NA)
    
         if (is.na(Lmax)) stop("invalid band")
         Lmin = 0
         Qmax = 127
         Lmin + ((Lmax-Lmin)*x)/Qmax
    }
    
    library(raster)
    b <- brick(system.file("external/rlogo.grd", package="raster"))
    test <- radconvL(b[[2]], band = 2)
    

    testRasterLayer,但如果您需要 SpatialGridDataFrame(为什么?)请使用:

    sptest <- as(test, 'SpatialGridDataFrame')
    

    这不是您问题的直接答案,但很难理解您为什么要在函数中执行某些操作。例如,您这样做:

     x <- as.vector(as.matrix(x))
     results <- x
     x <- Lmin + ((Lmax-Lmin)*x)/Qmax
    

    所以resultsx 是一个向量,但是你这样做:

    if (class(results) == "SpatialGridDataFrame")
    #(...)
    else if (is.data.frame(x))
    #(...)
    else results <- x
    

    当我们知道x 是一个向量(而不是SpatialGridDataFramedata.frame)时,这有什么关系?这总是会使results 等于x。所以很明显,结果总是数字的。

    您声明您这样做:class(results) == "SpatialGridDataFrame",但您没有这样做。无论哪种方式,这都行不通(这类似于在你的自行车上贴一个带有“汽车”的便利贴;这不会神奇地突然给它四个轮子和一个引擎)。

    如果您想通过将所有值加载到内存中来加快速度,您可以这样做:

    radconvL <- function(x, band = 2) {
         Lmax <- switch(band, 
                        "2" = 120.64,
                        "3" = 151.31,
                        "4" = 157.57,
                        "5" = 69.03,
                        NA)
    
         if (is.na(Lmax)) stop("invalid band")
         Lmin = 0
         Qmax = 127
         setValues(x, Lmin + ((Lmax-Lmin)*values(x))/Qmax)
    }
    

    【讨论】:

    • 非常感谢 RobertH 的回答!!!我在使用 SpatialGridDataFrame 的问题与处理器速度有关,我有 28250001 像素的卫星图像,如果我直接使用光栅格式,您可以将花费的时间进行成像?
    • 我认为这可能并不重要(请参阅rasterOptions 以使用更多 RAM)。但我添加了另一个版本的向量计算函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    相关资源
    最近更新 更多