R:是否可以仅从栅格图层的 non-NA 区域采样随机点?

R: Is it possible to sample random points from only the non-NA region of a raster layer?

我一直在尝试从栅格图层中随机抽取 user-defined 个点(例如,兰伯特共形圆锥投影中的 24 个气候变量图层中的任何一个,来自 https://sites.ualberta.ca/~ahamann/data/climatewna.html) using spsample, within a region whose extent is defined by a polygon I generated by forming circles around a set of points and aggregating their boundaries, but cannot figure out how to only sample those portions of the raster within that region which are defined. The image below shows the raster layer and polygon I am working with:

我知道可以在该区域内进行采样,然后使用栅格图层作为遮罩,从采样点中移除那些落在该图层的 NA 区域内的点,但是,在这样做时,数字剩余的背景点数少于 spsample 函数中的 user-specified。这是我为完成此操作而编写的代码:

circles <- circles(coordinate_dat_train, 150000)
circle_polygon <- polygons(circles)
proj4string(circle_polygon) <- CRS("+proj=lcc +lat_1=49 +lat_2=77 +lat_0=0 +lon_0=-95 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")

sample_mask <- raster("C:/...path.../cv_NORM_6190_AHM.tif")
r_sample <- spsample(circle_polygon, 1000, type='random')
r_sample_cells <- as.data.frame(extract(sample_mask, r_sample, cellnumbers = TRUE))
r_sample_cells <- as.data.frame(r_sample_cells[!is.na(r_sample_cells$cv_NORM_6190_AHM),])
r_sample_cells <- r_sample_cells[,-2]
r_sample_coords <- xyFromCell(sample_mask, r_sample_cells, spatial = TRUE)

我也尝试过使用 rasterToPolygon 函数将栅格转换为多边形,找到该多边形与原始多边形的交点,并仅在该交点内进行采样,但我使用的是栅格图层工作如此之大,完成这个过程所需的时间是不可行的。

是否有其他方法可以完成此操作,这些方法在计算时间方面相当快?提前致谢!

从以多边形为界的栅格中 selecting 这样一组点的一种方法是首先用多边形遮盖栅格,然后通过 selecting 从被屏蔽的只有那些包含非 NA 值的单元格。这可以做到,在这种情况下,像这样:

sample_mask <- raster("C:/...path.../cv_NORM_6190_AHM.tif")
# This command masks the raster so that its extent equals that of the circle polygon.
mask <- mask(sample_raster, circle_polygon)
# This command creates a new raster layer consisting of non-NA values from the masked layer.
non_na_raster <- !is.na(mask)

然后,使用 sampleRandom 函数(该函数不会 select 来自一个单元格的多于一个点,除非要 selected 的点数超过单元格数在所讨论的栅格层内),可以明确定义他们希望从掩蔽栅格的陆地区域 selected 的点数,如下所示:

# This command counts the number of cells within the non-NA raster layer, giving an accurate measure of the
# terrestrial area from which background points are drawn, so that sampling density may be determined.
cellStats(non_na_raster, 'sum') 
r_sample <- as.data.frame(sampleRandom(x=non_na_raster, size = 1000, na.rm = TRUE, ext = circle_polygon, xy = TRUE))

可以使用其他一些随机点 selection 函数,例如 spsample,但 sampleRandom 是特定于 raster 包的函数,并且避免对同一个栅格单元进行多次采样,这就是我在这种情况下使用它的原因。