[R-sig-Geo] Combining multiple polylines into a single polyline

Don MacQueen macq at llnl.gov
Sun Jun 13 02:05:01 CEST 2010


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.

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)

-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062



More information about the R-sig-Geo mailing list