计算光栅的质心

tnt*_*tnt 4 r spatial geospatial

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

例子:

> 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]])
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

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

@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
Run Code Online (Sandbox Code Playgroud)

dww*_*dww 6

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

colMeans(xyFromCell(r, which(r[]==1)))
Run Code Online (Sandbox Code Playgroud)

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

以下是一些可重复的虚拟数据进行测试:

r = raster(matrix(sample(0:1, 10000,T), nrow=100,ncol=100))
Run Code Online (Sandbox Code Playgroud)