[R-sig-Geo] Converting a line feature to evenly spaced points

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Jul 8 21:37:55 CEST 2011


So I delved a bit. Second day in a row I've been messing with the guts
of sample.Line:

#' sample every distance interval along a line
#'
#' @param cc a two-column matrix of x and y coords for the line
#' @param dist distance separating sample points
#'
#' @return points along the line, separated by the given distance
measured along the line
#'
sampleEvery <- function (cc, dist){
    lengths = LineLength(cc, longlat = FALSE, sum = FALSE)
    if (any(abs(lengths) < .Machine$double.eps)) {
        wl <- which(abs(lengths) < .Machine$double.eps)
        cc <- cc[-(wl), ]
        lengths <- lengths[-(wl)]
    }
    csl = c(0, cumsum(lengths))
    maxl = csl[length(csl)]
    pts = seq(0,sum(lengths),by=dist)
    int = findInterval(pts, csl, all.inside = TRUE)
    where = (pts - csl[int])/diff(csl)[int]
    xy = cc[int, , drop = FALSE] + where * (cc[int + 1, , drop = FALSE] -
        cc[int, , drop = FALSE])
    if (nrow(xy) < 1)
        return(NULL)
    return(xy)
}

Original by Roger and Edzer, no doubt.

Barry



More information about the R-sig-Geo mailing list