[R-sig-Geo] cleaning up SpatialLines
Dylan Beaudette
dylan.beaudette at gmail.com
Thu Aug 4 00:45:44 CEST 2011
On Wed, Aug 3, 2011 at 3:11 PM, ONKELINX, Thierry
<Thierry.ONKELINX at inbo.be> wrote:
> 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)))
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
Would something like this work:
# using your example from above ...
library(rgeos)
# merge lines by group, manually
Raw.u <- gLineMerge(Raw[Raw$Group == 1, ])
# automated approach, using spatial intersection as the criteria for splitting
Raw.u <- gLineMerge(Raw)
... note that IDs are lost in the second, automated approach.
Iterating over IDs might be the safest approach.
Dylan
More information about the R-sig-Geo
mailing list