I am doing some work where I need to generate both a) random spatial points b) non-random spatial points, over a polygon i.e. for b) the points probability depends on for example an East-West gradient, or distance from some point source or something else
For a) I can generate random points over a polygon using the spsample()
command in the sp
package as follows:
# Load a spatial polygon from maptools package
library(maptools)
nc <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], proj4string=CRS("+proj=longlat +datum=NAD27"))
plot(nc)
library(sp)
pts <- spsample(nc, 100, type="random")
plot(nc)
points(pts, pch=19, col="red")
This gives exactly what I want for a). But, can this be modified for b) so points are more likely in the East than the West for example ? (and whilst still being able to specify I want 100 points ?)
I only know how to do this using the spatstat
package. With the function rpoint
You can use any function of the coordinates x,y to define your uneven density of points. Here I define the function to have the value 0 at the West end of the region and grow linearly with slope 100 towards the East:
library(maptools)
library(spatstat)
nc <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
proj4string=CRS("+proj=longlat +datum=NAD27"))
nc <- as.owin(nc)
west0 <- nc$xrange[1]
f <- function(x, y, ...){ 100 * (x - west0) }
pts <- rpoint(1000, f, win = nc)
plot(pts)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With