如何为每个栅格网格单元选择一个点?

问题描述

我有一个高度聚类的点shapefile(“ search_effort.shp”)和一个NDVI栅格(以m为单位的分辨率:30.94948,30.77829)。我想通过为每个栅格网格单元选择1点并创建一个新的search_effort shapefile来对我的search_effort.shp进行子集化。我正在使用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)中的错误:无效的类“ GridTopology”对象:单元大小具有错误的尺寸。” 无论我指定的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")