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

Roger Bivand Roger.Bivand at nhh.no
Thu Jun 17 11:50:32 CEST 2010


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 at nhh.no



More information about the R-sig-Geo mailing list