0

I am trying to sample a raster (pixel values= 1 or NA, where 1 means opportunity as defined for my research).

The specs of my raster are

class      : RasterLayer 
dimensions : 15838, 3808, 60311104  (nrow, ncol, ncell)
resolution : 60, 60  (x, y)
extent     : 2402625, 2631105, 788651, 1738931  (xmin, xmax, ymin, ymax)
crs        : +proj=utm +zone=43 +ellps=WGS84 +units=m +no_defs 
source     : <folder> not including in this post 
names      : <name> not including in this post 
values     : 1, 1  (min, max)

I want to sample 30 random points in all pixels with value=1 for further analyses and research.

sampled_points <-raster::sampleRandom(above raster, size = 30 ,na.rm=TRUE, xy=TRUE, sp= TRUE)

But the Error in .bboxCoords(coords) : nrow(coords) > 0 is not TRUE is present. I have used sampleRandom() multiple times before in other analyses an dhave not encountered this problem. How do I solve this problem?

tg110
  • 840
  • 8
  • 25
  • I can only generate that error if all the cells in a raster are NA. How many non-NA cells are there? Sometimes the min/max reported can be lying, especially for large rasters. What's table(yourraster) say for itself? – Spacedman Sep 02 '20 at 16:33
  • I used length(getValues(myraster==1)) and length(getValues(is.na(myraster))) and get 60311104, same as ncell for both pixel values. I cannot use table(myraster) because of error- unique() applies to vectors – tg110 Sep 02 '20 at 17:02
  • @Spacedman- I couldn't figure out a solution. I reclassified all NA pixels to 0 and used sampleStratified() varying size and exp to sample and ignore all sampled points in pixels=0 – tg110 Sep 03 '20 at 11:34

1 Answers1

0

See this discussion about sampling rasters with a large number of NAs: Using sampleRandom() from large raster without NA values in R?

In short, this can lead to problems even if not all cells have NA values, but also if the number of NA is large and the sample size small so that a lot of NAs are drawn in the sampling process. A solution can be to produce a larger sample and then downsample from the actual values we obtain.

LuleSa
  • 25
  • 5