I would like to join groups of lines into multilinestrings if they touch. In the plot below, I should get two objects of sf
class MULTILINESTRING
from the 6 lines. What is an easy way of doing this (when having 2 mio lines)? Is there maybe a pre-defined command that let's one do this?
data:
library(sf)
lines <- st_sfc(list(st_linestring(rbind(c(0,0), c(0.5, 0.7))),
st_linestring(rbind(c(0.5, 0.7), c(1, 1))),
st_linestring(rbind(c(2,0), c(1,1))),
st_linestring(rbind(c(1,1), c(1, 3))),
st_linestring(rbind(c(3,2), c(4,2))),
st_linestring(rbind(c(3,2), c(5,3)))))
I tried the following
library(igraph)
components(graph.adjlist(st_touches(lines)))
graph.adjlist
transforms the list of elements that touch each other to a graph object and the function components
identifies the components of each cluster.
But is there a way to do connect the multiple LINESTRING
into MULTILINESTRING
if they touch?