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")))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With