Random sampling with a distance constraint

Jérôme Guélat, July 2012

This code illustrates the rSSI() function from the spatstat package…

If you don't need a distance constraint, it is probably better to use the spsample() function in the sp package.

library(spatstat)
library(rgdal)
library(maptools)
# Import perimeter as a shapefile and convert to a 'owin' object
peri <- readOGR(".", "testperi", verbose = FALSE)
peri_owin <- as.owin(peri)
plot(peri_owin)

plot of chunk unnamed-chunk-3

# Sample 100 points, minimal distance between the points = 50m
pts <- rSSI(50, 100, peri_owin)
pts
##  planar point pattern: 100 points 
## window: polygonal boundary
## enclosing rectangle: [573022, 573980] x [173036, 173997] units  
plot(pts)

plot of chunk unnamed-chunk-5

# Check the distances
coord <- cbind(pts$x, pts$y)
distmat <- dist(coord)
min(distmat)
## [1] 50.13
# Transform to SpatialPointsDataFrame and export to Shapefile
pts <- SpatialPointsDataFrame(coord, data = data.frame(id = 1:nrow(coord)))
writeOGR(pts, ".", "sampleConstr", "ESRI Shapefile")