【问题标题】:Get raster cell coordinates using the 'calc' function in R使用 R 中的 'calc' 函数获取栅格单元坐标
【发布时间】:2020-06-29 20:02:40
【问题描述】:

我正在尝试在 R 中运行“计算”函数,但似乎无法找到获取正在处理的单元格坐标的方法。我想做的很简单: 在二进制栅格(0 和 1)上使用“计算”​​功能——如果栅格值为“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"’ 

谢谢大家!

【问题讨论】:

  • 您是从某个库中导入coordinates 函数还是您自己编写的?
  • 这是一个从包'raster'中定义的函数。
  • 该函数需要一个对象来计算坐标,而不是整数或整数向量。这可能是您遇到的问题。
  • 是的,这正是问题所在。变量“cell”只包含它的值,所以是“1”。但我想知道是否有人知道我如何获得它的坐标。

标签: r coordinates raster r-raster calc


【解决方案1】:

这是不可能的。但是您可以使用 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-08
    • 2015-07-14
    • 2022-08-17
    • 1970-01-01
    • 2018-09-27
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多