创建空空间线对象

Create empty spatial lines object

我正在尝试创建一个空的 SpatialLines 对象。使用多边形很容易:

SpatialPolygons(list())

对于空间线,这不起作用:

SpatialLines(LinesList = list())
Error in bb[1, ] : incorrect number of dimensions
SpatialLines(LinesList = Lines(list(),ID = "a"))
Error in as.list.default(X) : 
  no method for coercing this S4 class to a vector
SpatialLines(LinesList = Lines(slinelist = Line(coords = cbind(x = c(), y = c())), ID = c()))
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘coordinates’ for signature ‘"NULL"’

有人知道我如何创建一个空的 SpatialLines 对象吗?

解决方法

我找到了一个可能不是最好的解决方法。我生成了一条没有长度的空间线:

SpatialLines(list(Lines(Line(coords = cbind(x = c(0,0), y = c(0,0))), ID = "A")))

有趣的问题!

我能够解决的唯一方法是创建一条虚拟线并将其删除,如下所示:

sl <- SpatialLines(LinesList = list(Lines(Line(matrix(0, ncol = 2)), ID = NA)))
sl <- sl[0]
length(sl)
# [1] 0

添加虚拟行时,长度按预期返回为 1:

length(rbind.SpatialLines(sl, SpatialLines(list(Lines(Line(coords = cbind(x = c(0,0), 
                                                                          y = c(0,0))), 
                                                           ID = "A")))))
# [1] 1