3

I am trying to create an empty SpatialLines object. With polygons it is easy:

SpatialPolygons(list())

For spatial lines this does not work:

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"’

Does someone know how it I could create an empty SpatialLines object?

Workaround

I found a workaround which is maybe not the best way of doing it. I generates a spatial line with no length:

SpatialLines(list(Lines(Line(coords = cbind(x = c(0,0), y = c(0,0))), ID = "A")))
Tobias Dekker
  • 980
  • 8
  • 19

1 Answers1

2

Interesting Q!

The only way I was able to work around was by creating a dummy line and removing it like this:

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

When adding your dummy line the length is returned as 1 as expected:

length(rbind.SpatialLines(sl, SpatialLines(list(Lines(Line(coords = cbind(x = c(0,0), 
                                                                          y = c(0,0))), 
                                                           ID = "A")))))
# [1] 1
loki
  • 9,816
  • 7
  • 56
  • 82