[R-sig-Geo] split/divide SpatialLines (sp) into n segments

Kevin Barnett kevin.barnett at umconnect.umt.edu
Thu May 23 00:20:28 CEST 2013


I've been working on a problem related to the contents of this thread. 
Suppose I have a SpatialLines object representing the boundary of a study
area, and I would like to split/segment the line at sample points equally
spaced along the line.  I can connect the sample points with straight lines,
but I haven't figured out how to include the vertices of the SpatialLines
object between a given pair of sample points.  An example is below:  

library(sp)
Sl = SpatialLines(list(Lines(list(Line(cbind(c(1,2,2,1,1),c(1,1,2,2,1)))),
  ID="a")))

set.seed(123)
sample.points <- spsample(Sl, 10, type="regular")
plot(Sl)
plot(sample.points, pch=1, col="red", add=T)

cc <- coordinates(sample.points)
cc <- rbind(cc, cc[1,])

outputlist = list()
i <- 1
while(i < (nrow(cc))){
  coords1 <- cc[i,]
  coords2 <- cc[i+1,]
  bind <- rbind(coords1, coords2)
  outputlist[[i]] <- Lines(list(Line(bind)), as.character(i))
  i <- i+1
  }

out <- SpatialLines(outputlist)
plot(out, lty=2, col="green", add=T)

In this example, simply connecting the sample points with a straight line
would effectively cut off the corners of the original SpatialLines object. 
I can't seem to find an efficient solution that can be transferred to my
highly irregular SpatialLines object with thousands of sample points.

Thanks for any insight, 
Kevin       



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/split-divide-SpatialLines-sp-into-n-segments-tp7583234p7583629.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list