[R-sig-Geo] subsetting a WKT Multilinestring

Roger Bivand Roger.Bivand at nhh.no
Mon Apr 22 20:39:32 CEST 2013


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

> Consider:
>
> library(rgeos)
> mls<-readWKT("MULTILINESTRING((3 4, 10 50, 20 25),(30 30, 70 50, 24 55)
>             ,(30 12, 50 5, 65 7))")

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.

>
> mls is, as expected, a SpatialLines object, and
> plotting it shows that indeed it has three individual
> lines. My question is: is there some way to subset this
> object so as to obtain a smaller SpatialLines object?
> (say, by excluding the second Line)?
>
> ------------------------
> Philip A. Viton
> City Planning, Ohio State University
> 275 West Woodruff Avenue, Columbus OH 43210
> viton.1 at osu.edu
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list