[R-sig-Geo] cleaning up SpatialLines

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Aug 4 00:11:41 CEST 2011


Dear all,

I have a SpatialLinesDataFrame containing a lot of short line fragments. Many of them have exactly one other linefragment at one (or both) of their end vertices. I would like to merge those linefragments to that each line is at both end vertices either connected to at least two other lines or to no other line.

Does someone has an effecient solution for this? Via R or via grass?

I managed already to add a grouping variable to the data.frame. But I still need to find a way to merge the coordinates of the lines from the same Group. See the example below.

Best regards,

Thierry

library(sp)
Raw <- 
SpatialLines(list(Lines(list(Line(cbind(x = c(0, 0, 1), y = c(0, 1, 1)))), ID = 1),
Lines(list(Line(cbind(x = c(2, 2, 1), y = c(0, 1, 1)))), ID = 2),
Lines(list(Line(cbind(x = c(-2, -2), y = c(0, -1)))), ID = 3)
))
Raw <- SpatialLinesDataFrame(Raw, data.frame(ID = 1:3, Group = c(1,1,2)))

Result <- 
        SpatialLines(list(Lines(list(Line(cbind(x = c(0, 0, 1, 2, 2), y = c(0, 1, 1, 1, 0)))), ID = 1),
                        Lines(list(Line(cbind(x = c(-2, -2), y = c(0, -1)))), ID = 2)
                ))
Result <- SpatialLinesDataFrame(Result, data.frame(ID = 1:2, Group = c(1,2)))




More information about the R-sig-Geo mailing list