关于raster包中的mask()函数的困惑 [英] Confusion about the mask() function in the raster package

查看:151
本文介绍了关于raster包中的mask()函数的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个问题的最基本本质,我事先表示歉意,但是我对mask()函数在R的栅格数据包中的工作方式感到困惑.

I apologise in advance for the very basic nature of this question, but I'm confused about how the mask() function works in the raster package in R.

在阅读该函数的文档时,听起来好像栅格x中的像元设置为NA(默认值),如果这些像元与mask对象中的maskvalue匹配(默认maskvalue为NA).但是,Lovelace等人在《带R的地球运算与R 》一书中对mask()函数的描述. ( https://geocompr.robinlovelace.net/spatial-operations.html#spatial -ras )(第4.3.1节)使其听起来好像栅格x中的像元与遮罩对象中的maskvalue相匹配时为KEPT,否则为NA.他们给出了这个例子:

Reading the documentation for the function it sounds like cells in raster x are set to NA (the default) if these cells match a maskvalue in a mask object (the default maskvalue being NA). However, the description of the mask() function in the book Geocomputation with R by Lovelace et al. (https://geocompr.robinlovelace.net/spatial-operations.html#spatial-ras) (section 4.3.1) makes it sound as if cells in raster x are KEPT if they match a maskvalue in a mask object, and set to NA if they don't. They give this example:

mask(elev, rmask, maskvalue = TRUE)

"we only want to keep those values of elev which are TRUE in rmask"

让我感到困惑.如果有人可以澄清哪种解释是正确的,我将不胜感激.

Hence my confusion. I would be grateful if someone could clarify which interpretation is correct.

我想知道的原因是,我想用包含数据质量代码的同一MODIS产品中的栅格掩盖百分比树覆盖上包含MODIS数据的栅格.我只想保留树覆盖"栅格中那些在质量"栅格中具有优质"质量代码的值.澄清mask()函数的工作方式将有助于我确定是否需要使用代码[1]或代码[2]来实现所需的功能:

The reason I'd like to know is that I'd like to mask a raster containing MODIS data on percentage tree cover with a raster from the same MODIS product that contains data quality codes. I'd like to retain only those values in the "tree cover" raster that have "good quality" quality codes in the "quality" raster. Clarifying how the mask() function works will help me to determine whether I need to use code [1] or code [2] to achieve what I want:

[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.

如果这个问题过于简单,我们再次表示歉意,但我感谢您的帮助!

Apologies again if this question is very simplistic, but I'd be grateful for your help!

推荐答案

是什么使您无法制作一个小的示例并测试哪种方法有效?在您的情况下,[1]和[2]都不起作用,因为maskvalue是单个值(如果提供更长的向量,则为第一个值).您可能要先使用重分类

What stops you from making a small example and test which approach works? In your case, neither [1] nor [2] will work, as maskvalue is a single value (the first value if you provide a longer vector). You probably want to use reclassify first

示例数据

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

Create a RasterLayer with good (4 - 8) and bad (1 - 8) values and then use 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)

这篇关于关于raster包中的mask()函数的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆