Convert polyline shapefile to points 1m apart and add xy coords in R

I am a bit unclear on your question but if you have an sp line object then you can use "spsample" in a loop to create a systematic distance based sample for each line. Here is a function that implements a distance based sample along a line.

The arguments are x = sp SpatialLinesDataFrame and sdist = Sampling distance.

# Function for systematic distance line sample
sample.line <- function(x, sdist=100)
   {
    if (!require(sp)) stop("sp PACKAGE MISSING")
     if (!inherits(x, "SpatialLinesDataFrame")) stop("MUST BE SP SpatialLinesDataFrame OBJECT")
      lgth <- SpatialLinesLengths(x) 
      lsub <- x[1,]
        ns <- round( (lgth[1] / sdist), digits=0)
          lsamp <- spsample(lsub, n=ns, type="regular", offset=c(0.5,0.5))
            results <- SpatialPointsDataFrame(lsamp, data=data.frame(ID=rep(rownames(x@data[1,]),ns))) 
    for (i in 2:dim(x)[1] ) 
      {    
       lsub <- x[i,]
         ns <- round( (lgth[i] / sdist), digits=0)
           lsamp <- spsample(lsub, n=ns, type="regular")
       lsamp <- SpatialPointsDataFrame(lsamp, data=data.frame(ID=rep(rownames(x@data[i,]),ns)))
       results <- rbind(results, lsamp)     
     }
  ( results )
}

require(sp)
# Create some data
l1 = cbind(c(1,2,3),c(3,2,2))
  l1a = cbind(l1[,1]+.05,l1[,2]+.05)
    l2 = cbind(c(1,2,3),c(1,1.5,1))
Sl1 = Line(l1)
  Sl1a = Line(l1a)
    Sl2 = Line(l2)
      S1 = Lines(list(Sl1, Sl1a), ID="a")
        S2 = Lines(list(Sl2), ID="b")
sp.lines = SpatialLines(list(S1,S2))
  ldf <- data.frame(ID=c(1,2), Y=c(0.5,0.25), row.names=c("a","b"))
    sp.lines <- SpatialLinesDataFrame(sp.lines, ldf)   

# Run sample function and display results   
lsamp <- sample.line(sp.lines, sdist=0.5)   
  plot(sp.lines, col = c("red", "blue"))
    plot(lsamp, pch=20, add=TRUE)

Tags:

R

Shapefile