无法逻辑寻址 rasterToPoints 提取的 x、y 值

Unable to logically adress x,y-values of rasterToPoints extraction

我不确定如何描述这个问题。我有一种感觉,它很琐碎,但我无法掌握它。

我有一堆光栅对象(对象NDVI)。从这些我提取 xy 坐标使用 rasterToPoints

xycoord1 <- rasterToPoints(NDVI)
xycoord <- xycoord1[,c(1:2)]

在预处理过程中,我剔除了几个不可用的像素,结果是:

> str(xycoord.short)
 num [1:20054, 1:2] 3802292 3802523 3802755 3802987 3803218 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "x" "y"

不,我只是想找到某个 xy 坐标。 例如

> which(xycoord.short[,1]==3802292)
integer(0)

但我似乎无法 "get hold" 例如一列中的值。

> xycoord.short[,1][1]
[1] 3802292
> xycoord.short[,1][1]==xycoord.short[,1][1]
[1] TRUE
> xycoord.short[,1][1]==3802292
[1] FALSE

谁能帮我解决这个问题?我只是没有找到问题所在。它与通过 rasterToPoints 的初始提取有关吗?谢谢!

编辑: 我的 xy 坐标的前 10 行的 dput 输出

xy <- structure(c(3802291.63636448, 3802523.29272274, 3802754.94908101, 
                 3802986.60543927, 3803218.26179754, 3803449.9181558, 3803681.57451406, 
                 3803913.23087233, 3804144.88723059, 3804376.54358886, -49690.2888476191, 
                 -49690.2888476191, -49690.2888476191, -49690.2888476191, -49690.2888476191, 
                 -49690.2888476191, -49690.2888476191, -49690.2888476191, -49690.2888476191, 
                 -49690.2888476191), .Dim = c(10L, 2L), .Dimnames = list(NULL, 
                                                                         c("x", "y")))

编辑2: 在发布 dput 输出后它是有意义的,因为值显然是四舍五入的。 使用确切的数字有效...

> any(xycoord.short[,1]==3802291.63636448)
[1] TRUE

这里是四舍五入 "problem"。您的坐标在我们所说的 "double" 中(10.3 是双精度),但您正在尝试基于整数(比如 10)进行子集化。您在这里可以做的是四舍五入 n 个地方和基于此的子集。

例如,让我们检查八位数字。

format(xy, digits = 8)

      x             y            
 [1,] "3802291.636" " -49690.289"
 [2,] "3802523.293" " -49690.289"
 [3,] "3802754.949" " -49690.289"
 [4,] "3802986.605" " -49690.289"
 [5,] "3803218.262" " -49690.289"
 [6,] "3803449.918" " -49690.289"
 [7,] "3803681.575" " -49690.289"
 [8,] "3803913.231" " -49690.289"
 [9,] "3804144.887" " -49690.289"
[10,] "3804376.544" " -49690.289"

所以本质上,当你在寻找 3802292 时,它并没有找到它,因为它实际上是 3802291.636...

您可以指定最多 x 个正确位置的精确坐标,也可以四舍五入您的数字并对其进行处理。或者,您可以指定包含所需值的一系列值。