Skip to content
Prev 7071 / 29559 Next

'Customizing' spsample

Jean-Paul Kibambe Lubamba wrote:
Hi Jean-Paul,

My suggestion would be to take the function sample.Line (from sp) as a 
starting point and modify it to your specifications. You can find the 
source in the /R directory (spsample.R) of the source package, which you 
can download from CRAN.

cheers,
Paul

sample.Line
function (x, n, type, offset = runif(1), proj4string = 
CRS(as.character(NA)),
    ...)
{
    offset = offset[1]
    if (missing(n))
        n <- as.integer(NA)
    if (!is.finite(n) || n < 1)
        return(NULL)
    cc = coordinates(x)
    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)]
    if (type == "random")
        pts = runif(n) * maxl
    else if (type == "stratified")
        pts = ((1:n) - runif(n))/n * maxl
    else if (type == "regular")
        pts = ((1:n) - (1 - offset))/n * maxl
    else stop(paste("type", type, "not available for Line"))
    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)
    SpatialPoints(xy, proj4string)
}
<environment: namespace:sp>