对 raster 包中的 mask() 函数的困惑
Confusion about the mask() function in the raster package
对于这个问题的基本性质,我提前表示歉意,但我对 mask() 函数在 R 的栅格包中的工作方式感到困惑。
阅读该函数的文档,如果栅格 x 中的单元格与掩码对象中的掩码值匹配(默认掩码值是 NA),则这些单元格听起来像是设置为 NA(默认值)。但是,Lovelace 等人在 Geocomputation with R 一书中对 mask() 函数的描述。 (https://geocompr.robinlovelace.net/spatial-operations.html#spatial-ras)(第 4.3.1 节)听起来好像栅格 x 中的单元格与掩码对象中的掩码值匹配时保持不变,如果不匹配则设置为 NA。他们给出了这个例子:
mask(elev, rmask, maskvalue = TRUE)
"we only want to keep those values of elev which are TRUE in rmask"
因此我感到困惑。如果有人能澄清哪种解释是正确的,我将不胜感激。
我想知道的原因是我想用来自包含数据质量代码的同一 MODIS 产品的栅格来掩盖包含 MODIS 树木覆盖率数据的栅格。我只想保留 "tree cover" 栅格中那些在 "quality" 栅格中具有 "good quality" 质量代码的值。阐明 mask() 函数的工作原理将帮助我确定是否需要使用代码 [1] 或代码 [2] 来实现我想要的:
[1]
good <- c(0,1,2,3,4,5...etc.) # The codes in the quality raster that represent good quality data
tree_cover_masked <- mask(tree_cover, quality, maskvalue = good, inverse = TRUE)
# i.e. set cells in tree_cover to NA if they match any value OTHER THAN the "good" values in the quality raster.
# This is the code I would use based on my interpretation of the function documentation.
[2]
tree_cover_masked <- mask(tree_cover, quality, maskvalue = good)
# i.e. keep values in tree_cover that match "good" values in the quality raster, and set all others to NA
# This is the code I would use based on my interpretation of Lovelace et al.
如果这个问题过于简单,再次表示歉意,但非常感谢您的帮助!
是什么阻止您制作一个小示例并测试哪种方法有效?在您的情况下, [1] 和 [2] 都不起作用,因为 maskvalue 是单个值(如果您提供更长的向量,则为第一个值)。您可能想先使用重新分类
示例数据
library(raster)
qual <- trees <- raster(nrow=4, ncol=4, xmn=0, xmx=1, ymn=0, ymx=1, crs='+proj=utm +zone=1')
values(trees) <- rep(1:4, 4)
values(qual) <- rep(1:8, 2)
创建一个具有良好 (4 - 8) 和不良 (1 - 8) 值的 RasterLayer,然后使用 mask
good <- reclassify(qual, rbind(c(0, 4, NA), c(4, 9, 1)))
# this would also work
# good <- reclassify(qual, cbind(0, 4, NA))
x <- mask(trees, good)
或者:
good <- subs(qual, data.frame(from=c(5,6,7,8,9), 1))
x <- mask(trees, good)
对于这个问题的基本性质,我提前表示歉意,但我对 mask() 函数在 R 的栅格包中的工作方式感到困惑。
阅读该函数的文档,如果栅格 x 中的单元格与掩码对象中的掩码值匹配(默认掩码值是 NA),则这些单元格听起来像是设置为 NA(默认值)。但是,Lovelace 等人在 Geocomputation with R 一书中对 mask() 函数的描述。 (https://geocompr.robinlovelace.net/spatial-operations.html#spatial-ras)(第 4.3.1 节)听起来好像栅格 x 中的单元格与掩码对象中的掩码值匹配时保持不变,如果不匹配则设置为 NA。他们给出了这个例子:
mask(elev, rmask, maskvalue = TRUE)
"we only want to keep those values of elev which are TRUE in rmask"
因此我感到困惑。如果有人能澄清哪种解释是正确的,我将不胜感激。
我想知道的原因是我想用来自包含数据质量代码的同一 MODIS 产品的栅格来掩盖包含 MODIS 树木覆盖率数据的栅格。我只想保留 "tree cover" 栅格中那些在 "quality" 栅格中具有 "good quality" 质量代码的值。阐明 mask() 函数的工作原理将帮助我确定是否需要使用代码 [1] 或代码 [2] 来实现我想要的:
[1]
good <- c(0,1,2,3,4,5...etc.) # The codes in the quality raster that represent good quality data
tree_cover_masked <- mask(tree_cover, quality, maskvalue = good, inverse = TRUE)
# i.e. set cells in tree_cover to NA if they match any value OTHER THAN the "good" values in the quality raster.
# This is the code I would use based on my interpretation of the function documentation.
[2]
tree_cover_masked <- mask(tree_cover, quality, maskvalue = good)
# i.e. keep values in tree_cover that match "good" values in the quality raster, and set all others to NA
# This is the code I would use based on my interpretation of Lovelace et al.
如果这个问题过于简单,再次表示歉意,但非常感谢您的帮助!
是什么阻止您制作一个小示例并测试哪种方法有效?在您的情况下, [1] 和 [2] 都不起作用,因为 maskvalue 是单个值(如果您提供更长的向量,则为第一个值)。您可能想先使用重新分类
示例数据
library(raster)
qual <- trees <- raster(nrow=4, ncol=4, xmn=0, xmx=1, ymn=0, ymx=1, crs='+proj=utm +zone=1')
values(trees) <- rep(1:4, 4)
values(qual) <- rep(1:8, 2)
创建一个具有良好 (4 - 8) 和不良 (1 - 8) 值的 RasterLayer,然后使用 mask
good <- reclassify(qual, rbind(c(0, 4, NA), c(4, 9, 1)))
# this would also work
# good <- reclassify(qual, cbind(0, 4, NA))
x <- mask(trees, good)
或者:
good <- subs(qual, data.frame(from=c(5,6,7,8,9), 1))
x <- mask(trees, good)