rasterFromXYZ 需要 TRUE/FALSE 的缺失值

问题描述

我从 R 光栅包中的 rasterFromXYZ 函数收到了一些奇怪的错误消息。这是一个例子

library(raster)
xyz <- data.frame(x = c(5.463636,5.481818,5.5),y = c(51.42727,51.42727,51.42727),z = c(1.2,1.3,1.6))

r <- rasterFromXYZ(xyz)

##error
Error in if (nc > (2^31 - 1)) return(FALSE) : 
missing value where TRUE/FALSE needed
In addition: Warning message:
In min(dy) : no non-missing arguments to min; returning Inf

##specifying the resolution as 1
r <- rasterFromXYZ(xyz,res = 1)

##different error
Error in rasterFromXYZ(xyz,res = 1) : x cell sizes are not regular

x 坐标是完全规则的。我做错了什么?

解决方法

x 坐标没问题,但只有一个唯一的 y 坐标值。所以无法猜测垂直分辨率。

xyz
#         [,1]     [,2] [,3]
#[1,] 5.463636 51.42727  1.2
#[2,] 5.481818 51.42727  1.3
#[3,] 5.500000 51.42727  1.6

如果您将结果设置为与 x 坐标不匹配的 1,但您可以这样做

rasterFromXYZ(xyz,res=c(NA,1))
#class      : RasterLayer 
#dimensions : 1,3,3  (nrow,ncol,ncell)
#resolution : 0.018182,1  (x,y)
#extent     : 5.454545,5.509091,50.92727,51.92727  (xmin,xmax,ymin,ymax)
#crs        : NA 
#source     : memory
#names      : layer 
#values     : 1.2,1.6  (min,max)

开发版现在提供了更好的错误信息:

r <- rasterFromXYZ(xyz)
#Error in rasterFromXYZ(xyz) : more than one unique y value needed