计算栅格的质心

时间:2018-10-04 17:31:20

标签: r geospatial spatial

我有一个列表,其中包含有关南美许多动物的大概位置的信息。例如,这是存储信息的类型,以及为第一个人绘制的信息的外观。

示例:

> s[1]
[[1]]
class       : RasterLayer 
dimensions  : 418, 313, 130834  (nrow, ncol, ncell)
resolution  : 0.16666, 0.16666  (x, y)
extent      : -86.333, -34.16842, -55.91633, 13.74755  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : in memory
names       : layer 
values      : 0, 1  (min, max)
> plot(s[[1]])

enter image description here

注意:绿色区域均为“可能”位置,灰色区域为“不太可能”位置。

我想计算此可能位置的质心(即绿色区域的质心)。

@dww下面建议使用以下解决方案(适用于模拟数据),但导致我的数据出现错误消息。

colMeans(xyFromCell(s, which(s[]==1)))

Error in xyFromCell(s[1], which(s[] == 1)) : 
trying to get slot "extent" from an object of a basic class ("list") with no slots

1 个答案:

答案 0 :(得分:4)

要查找栅格r的值为1的像元的质心,可以使用

colMeans(xyFromCell(r, which(r[]==1)))

本质上,质心位于子集位置的纬度/经度的平均值。

这里有一些可重现的虚拟数据要测试:

r = raster(matrix(sample(0:1, 10000,T), nrow=100,ncol=100))
相关问题