r - 使用 R 中的 'calc' 函数获取栅格单元坐标
问题描述
我正在尝试在 R 中运行“计算”函数,但似乎找不到获取正在处理的单元格坐标的方法。我要做的很简单:在二进制栅格(0 和 1)上使用“calc”函数——如果栅格值为“0”,则更改为“NA”。如果栅格值为“1”,则应用一系列过程,我需要将单元坐标存储到变量中。
processAllCells = function(cell) {
if (cell == 0) {
cell = NA
return(cell)
}
else {
cellCoords = coordinates(cell) ### This is what I'm trying to do. This does not work. See the error message.
### Here will go further processes using the cell coordinates.
return(cell)
}
}
outputRaster = calc(lake, processAllCells)
错误信息 :
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘coordinates’ for signature ‘"integer"’
In addition: Warning message:
In if (cell == 0) { :
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘coordinates’ for signature ‘"integer"’
感谢大家!
解决方案
这是不可能的。但是您可以使用 x 和 y 坐标制作一个 RasterLayer 并在calc
.
library(raster)
r <- raster(nrow=10, ncol=10, values=1:100)
x <- init(r, "x")
y <- init(r, "y")
接着
s <- stack(r, x, y)
#x <- calc(s, your-function)
推荐阅读
- python - pandas/sqlalchemy/pyodbc: Result object does not return rows from stored proc when UPDATE statement appears before SELECT
- javascript - 如何停止以角度上传零字节图像?
- python - Duo API 目录同步
- asp.net-core - ASP.NET Core AddContext
- botframework - LUIS AI 不返回名称等内置实体
- maven - 构建助手 Maven 插件不删除整个正则表达式
- vue.js - 我应该在 gitignore 中为 nodejs/vuejs 应用程序添加 app.js 文件吗?
- php - 使用带有开始日期和结束日期 PHP 的输入插入表
- c - C 编程,stm32 uint64_t 变量不起作用
- sql-server - 导入时来自 SQL Azure 的 BACPAC 错误