0

I'm using the randomPoints function in the dismo package to generate sets of point locations within two polygons.

I've converted the polygons to rasters, where the cells within the polygon = 1, and those outside it = NA. (I won't paste the raster code here, as it's huge, but it comes from a bioclimatic WorldClim layer: http://worldclim.org/)

The polygons are:

poly1 <- new("SpatialPolygonsDataFrame"
, data = structure(list(SISID = 22710813L, SCINAME = "Spreo albicapillus", 
DATE_ADD = "07/06/2010", SOURCE = "Stevenson and Fanshawe, 2002; Ash and Atkins, 2009; Redman et al., 2009", 
PRESENCE = 1L, ORIGIN = 1L, SEASONAL = 1L, DATA_SENS = NA_character_, 
SENS_COMM = NA_character_, COMPILER = "Joe Taylor (BirdLife International)", 
TAX_COM = NA_character_, DIST_COM = NA_character_, REVIEWERS = NA_character_, 
CITATION = "BirdLife International and NatureServe (2015) Bird Species Distribution Maps of the World."), .Names = c("SISID", "SCINAME", "DATE_ADD", "SOURCE", "PRESENCE", "ORIGIN", "SEASONAL", "DATA_SENS", "SENS_COMM", "COMPILER", "TAX_COM", "DIST_COM", "REVIEWERS", "CITATION"), row.names = 0L, class = "data.frame")
, polygons = list(<S4 object of class structure("Polygons", package = "sp")>)
, plotOrder = 1L
, bbox = structure(c(36.6990966800678, 2.58129882767878, 50.3499145511157, 11.6159057616416), .Dim = c(2L, 2L), .Dimnames = list(c("x", "y"), c("min", "max")))
, proj4string = new("CRS"
, projargs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

poly2 <- new("SpatialPolygonsDataFrame"
, data = structure(list(SISID = 22710767L, SCINAME = "Lamprotornis superbus", 
DATE_ADD = "11/01/2007", SOURCE = "Fry, 2000;", PRESENCE = 1L, 
ORIGIN = 1L, SEASONAL = 1L, DATA_SENS = NA_character_, SENS_COMM = NA_character_, 
COMPILER = "BirdLife International", TAX_COM = NA_character_, 
DIST_COM = NA_character_, REVIEWERS = NA_character_, CITATION = "BirdLife International and NatureServe (2015) Bird Species Distribution Maps of the World."), .Names = c("SISID", "SCINAME", "DATE_ADD", "SOURCE", "PRESENCE", "ORIGIN", "SEASONAL", "DATA_SENS", "SENS_COMM", "COMPILER", "TAX_COM", "DIST_COM", "REVIEWERS", "CITATION"), row.names = 0L, class = "data.frame")
, polygons = list(<S4 object of class structure("Polygons", package = "sp")>)
, plotOrder = 1L
, bbox = structure(c(30.8720703122951, -9.15698242230093, 49.7819213870624, 11.9288940434254), .Dim = c(2L, 2L), .Dimnames = list(c("x", "y"), c("min", "max")))
, proj4string = new("CRS"
, projargs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

# Convert polygons to raster masks
Mask1 <- mask(Base, poly1)
Mask2 <- mask(Base, poly2)

I have successfully used the function to generate 5000 points from the first raster, which contains 935310 cells.

# Check there are more than 5000 cells
length(values(Mask1)[!is.na(values(Mask1))])
[1] 935310

# Select points
pts1 <- randomPoints(Mask1, 5000)
length(pts1[,1])
[1] 5000

However, when I repeat the process for the second (larger) raster, I get the following error:

# Check there are more than 5000 cells
length(values(Mask2)[!is.na(values(Mask2))])
[1] 2454060

# Select points
pts2 <- randomPoints(Mask2, 5000)
Warning message:
In randomPoints(Mask2, 5000) :
  generated random points = 0.283 times requested number

length(pts2[,1])
[1] 1415

Whatever number of points I request, R returns roughly the same fraction (0.28 ish), despite the fact that the raster has plenty of cells to choose from. And if I ask for enough points, it will give me more than I wanted originally!

pts3 <- randomPoints(Mask2, 20000)
Warning message:
In randomPoints(Mask2, 20000) :
  generated random points = 0.284 times requested number

length(pts3[,1])
[1] 5680

Does anyone have any idea what might be going on?

2

This can happen if there are a lot of NA values in the raster. That makes it hard to find valid random points. In this case you can indeed ask for more points than you need and then subset.

For example, if you need 100 points:

x <- randomPoints(r, 500)
x <- x[1:100, ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.