【问题标题】:Band math with calc and / or overlay returning single-valued images带计算和/或叠加返回单值图像的带数学
【发布时间】:2019-04-03 10:33:51
【问题描述】:

当我尝试运行波段数学时,结果始终是一种颜色的图像,并且最小值和最大值与预测的值非常不同。 我在这里没有找到任何显示此问题的问题。 我是这样设计的

r.stack <- stack("path to raster file"))

我使用重采样而不是裁剪来去除原始图像中的白色边缘

prj <- "+proj=utm +zone=23 +south +datum=WGS84 +units=m"
r <- raster(res=11.47, ext=extent(c(301496,  323919, 9888968, 9913982)), crs=prj, vals=NA
r.stack <- resample(r.stack, r)

之后图像具有以下配置:

> class       : RasterBrick 
> dimensions  : 2181, 1955, 4263855, 4  (nrow, ncol, ncell, nlayers)
> resolution  : 11.47, 11.47  (x, y)
> extent      : 301496, 323919.8, 9888966, 9913982  (xmin, xmax, ymin, ymax)
>coord. ref. : +proj=utm +zone=23 +south +datum=WGS84 +units=m +ellps=WGS84 +towgs84=0,0,0 
>data source : in memory
>names       : l.1, l.2, l.3, l.4
>min values  :       -36.12217,           -45.12768,   -46.30455,                       -35.26328 
>max values  :      10.567671,         4.050200,         3.878345,                       11.613799

然后使用下面的函数进行计算

f <- function(x){
      (x[[2]])/(x[[1]])
}

s <- r.stack[[c(1,2)]]
r2 <- calc(s, f)

而且我也很有趣地运行了叠加层

f <- function(x,y){
     y/x
}
r2 <- overlay(r.stack[[1]], r.stack[[2]], fun= f)

任何方法都会导致image of one value

我是否遗漏了一些步骤?

【问题讨论】:

  • I use resampling instead of crop to cut out the white edges。这通常是一个坏主意,因为它会降低您的数据质量。使用croptrim

标签: r overlay calc


【解决方案1】:

这是您的代码,其中包含一些示例数据(没有这些数据很难回答问题)。我把一个函数简化了一点,但是结果是一样的。

library(raster)
b <- brick(system.file("external/rlogo.grd", package="raster"))
b <- b/10 + 1

f <- function(x){ x[2]/ x[1] }
s <- b[[c(1,2)]]
r1 <- calc(s, f)

f <- function(x,y){ y / x }
r2 <- overlay(b[[1]], b[[2]], fun= f)

或者干脆

r3 <- b[[2]] / b[[1]]   
r3
#class       : RasterLayer 
#dimensions  : 77, 101, 7777  (nrow, ncol, ncell)
#resolution  : 1, 1  (x, y)
#extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=merc +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#data source : in memory
#names       : layer 
#values      : 0.7692308, 1.7  (min, max)

r1r2 是一样的。

你得到“单一颜色”的原因是因为大多数值都接近 1,但也有一些大的异常值;可能是因为除以 -1 和 1 之间的数字?这可能说明了这一点:

q <- quantile(r3, c(0.1, 0.9))
d <- clamp(r3, q[1], q[2])
plot(d)

看看极端情况

i <- which.max(r3)
b[i][,2:1]

【讨论】:

    猜你喜欢
    • 2017-01-11
    • 2018-08-10
    • 2017-08-14
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 2019-10-28
    相关资源
    最近更新 更多