Skip to content
Prev 18084 / 29559 Next

subsetting a WKT Multilinestring

On Mon, 22 Apr 2013, Philip A. Viton wrote:

            
length(mls)
#[1] 1

mls1<-readWKT("GEOMETRYCOLLECTION(LINESTRING(3 4, 10 50, 20 25),
   LINESTRING(30 30, 70 50, 24 55), LINESTRING(30 12, 50 5, 65 7))")
length(mls1)
#[1] 3

may be the incantation you want, then:

length(mls1[-2])
#[1] 2

A MULTILINESTRING is an sp "Lines" object, built of one or more "Line" 
objects. An sp "SpatialLines" object is a GEOMETRYCOLLECTION of 
MULTILINESTRING or LINESTRING objects, where the single LINESTRING objects 
are "promoted" to MULTILINESTRING/Lines status as observed data is 
attached to them, not to LINESTRING/Line objects. If you work with the 
object definitions, you don't have to unpick them afterwards.

Hope this clarifies,

Roger

PS:

unfold might be (I think Barry has said this, I have too somewhere):

crds <- coordinates(mls)
nobjs <- sum(sapply(crds, length))
out <- vector(mode="list", length=nobjs)
i <- 1
for (j in seq(along=crds)) {
   jcrds <- crds[[j]]
   for (k in seq(along=jcrds)) {
     out[[i]] <- Lines(list(Line(jcrds[k])), as.character(i))
     i <- i + 1
   }
}
SLout <- SpatialLines(out)
length(SLout)

In another context it was necessary to check that the coordinate matrices 
had at least two unique coordinates - not done here.