This is something that rgeos is able to do and I believe that the necessary functions are already implemented. GEOS provides a simple function called LineMerge that looks at a collection of linestrings and joins any that share endpoints.

There are some limitation as it just ignores more complex cases:
# Lines overlaps, not merged
l1=RGEOSLineMerge( readWKT("MULTILINESTRING((1 1,3 3),(2 2,4 4))") )
# Line intersects midpoint of other line, not merged
l2=RGEOSLineMerge( readWKT("MULTILINESTRING((1 1,3 3),(2 2,1 4))") )
# Three endpoints intersect, not merged
l3=RGEOSLineMerge( readWKT("MULTILINESTRING((1 1,2 2),(2 2,3 3),(2 2,1 4))") )

But for the simple case you describe it will work fine:

l1 <- matrix(c(1,1, 6,6, 4,8), byrow=TRUE, ncol=2)
l2 <- matrix(c(4,8, 7,9), byrow=TRUE, ncol=2)
l3 <- matrix(c(7,9, 11,10, 13,13), byrow=TRUE, ncol=2)

LL1 <- Lines( list( Line(l1)) , ID='32')
LL2 <- Lines( list( Line(l2)) , ID='147')
LL3 <- Lines( list( Line(l3)) , ID='21')

## deliberately constructed out of order
SL <- SpatialLines( list( LL2, LL3, LL1) )
#Produces a single Line
mSL <- RGEOSLineMerge(SL)

With that said you will also need to be careful when using SpatialLinesDataFrame since lines are being merged / removed it is very difficult to track which row belongs to which geometry or geometries after this kind of operation and for the time being the data slot is just ignored by rgeos and a SpatialLines is returned.

You can see my previous post on the mailing list for more background on rgeos.

Hope this helps,
-Colin

On Jun 17, 2010, at 2:50 AM, Roger Bivand wrote:

> On Sat, 12 Jun 2010, Don MacQueen wrote:
> 
>> I have an input shapefile representing roads in a city, read into R where it becomes a SpatialLInesDataFrame.
>> 
>> Looking at just one road at a time, it looks fine when plotted. But in the underlying structure, the single real-world road has been broken up into many shorter polylines, and they are not included in the "correct" order, i.e., end to end.
> 
> Is this an rgeos question - you seem to need a topology operation on geometries? Since rgeos is work in progress, there isn't support yet, and I don't know if there will be, but the use case seems convincing.
> 
> Roger
> 
>> 
>> When I sample along the road using spsample() with type='regular', the points aren't equally spaced. I assumed this was because it is broken up into separate lines. So I thought to combine the many short polylines into a single long polyline, i.e., matching endpoints of one polyline with the beginning of the next, dropping redundant nodes, and creating a new SLDF object.
>> 
>> I'm wondering if there's already an algorithm for this somewhere (in R or GRASS, preferably!).
>> (That's my main question)
>> 
>> 
>> However, in my toy example below, consisting of just three polylines making up a single road, spsample() does appear to equally space the points along the entire length. So perhaps my real roads have some other structural problem. Perhaps some endpoints don't match.
>> 
>> Thanks
>> -Don
>> 
>> Here's a toy example:
>> 
>> l1 <- matrix(c(1,1, 6,6, 4,8), byrow=TRUE, ncol=2)
>> l2 <- matrix(c(4,8, 7,9), byrow=TRUE, ncol=2)
>> l3 <- matrix(c(7,9, 11,10, 13,13), byrow=TRUE, ncol=2)
>> 
>> LL1 <- Lines( list( Line(l1)) , ID='32')
>> LL2 <- Lines( list( Line(l2)) , ID='147')
>> LL3 <- Lines( list( Line(l3)) , ID='21')
>> 
>> ## deliberately constructed out of order
>> SL <- SpatialLines( list( LL2, LL3, LL1) )
>> df <- data.frame( name=rep('Road',3))
>> row.names(df) <- c('147','21','32')
>> SLDF <- SpatialLinesDataFrame( SL, df)
>> 
>> ## I would like to reduce to a structure like this:
>> lc <- rbind(l1 , l2[-1,] , l3[-1,])
>> df1 <- data.frame( name='Road')
>> row.names(df1) <- 'a'
>> SLDFfix <- SpatialLinesDataFrame( SpatialLines( list( Lines( list( Line(lc)) , ID='a'))) , df1)
>> 
>> 
> 
> -- 
> Roger Bivand
> Economic Geography Section, Department of Economics, Norwegian School of
> Economics and Business Administration, Helleveien 30, N-5045 Bergen,
> Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
> e-mail: Roger.Bivand@nhh.no
> 


	[[alternative HTML version deleted]]

