如何 select 每个栅格网格单元一个点?

How to select one point per raster grid cell?

我有一个高度聚类的点 shapefile ("search_effort.shp") 和一个 NDVI 栅格(米分辨率:30.94948、30.77829)。我想通过为每个栅格网格单元选择 1 个点来对我的 search_effort.shp 进行子集化,并创建一个新的 search_effort shapefile。我正在使用 R 版本 4.0.3

我想我可以使用“gridsample”包(在 'raster' v1.3-1 中),但它已从 CRAN 存储库中删除,我不想使用存档版本。在 R 中还有其他方法可以做到这一点吗?

我也尝试过 sample.grid 但我不知道如何将我的栅格指定为网格,并尝试了以下方法:

# NDVI raster to be used as the reference extent
NDVI_extent <-readGDAL('C:/Model_layers/NDVI.tif')

# Load the file names  
layername <- "SearchEffort"

# Read in the shapefile
search_effort <- readOGR(dsn= ".", layer = layername)
plot(search_effort)

# Set the reference extent
r <- raster(NDVI_extent)

# Extract coordinates from the shapefile
search_effort@coords <- search_effort@coords[, 1:2]

#Subset points
sample.grid(search_effort, cell.size = c(30.94948, 30.77829), n = 1)

我收到以下错误: “validObject(.Object) 中的错误:无效 class “GridTopology” 对象:cellsize 的维度不正确。” 无论我指定 cell.size,我都会收到相同的错误。

示例数据

library(raster)
r <- raster(res=30)
values(r) <- 1:ncell(r)
x <- runif(1000,-180,180)
y <- runif(1000,-90,90)
xy <- cbind(x, y)

解决方案

library(dismo)
s <- gridSample(xy, r, n=1) 

插图

plot(as(r, "SpatialPolygons"))
points(s, col="red")
points(xy, cex=.1, col="blue")