【问题标题】:How to select one point per raster grid cell?如何为每个栅格网格单元选择一个点?
【发布时间】:2020-11-06 02:42:20
【问题描述】:

我有一个高度聚集的点 shapefile(“search_effort.shp”)和一个 NDVI 栅格(分辨率以 m 为单位: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) 中的错误:无效的类“GridTopology”对象:cellsize 的尺寸不正确。” 无论我指定的 cell.size 是什么,我都会收到相同的错误。

【问题讨论】:

    标签: r sample r-raster


    【解决方案1】:

    示例数据

    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")
    

    【讨论】:

    • 谢谢!这看起来可行,但我遇到了以下错误: ss[1:min(n, nrow(ss)), 1:2] 中的错误:下标越界你知道问题可能是什么吗?
    • 我回答了我自己的问题 - 我需要从 SPDF 中消除第三维:search_effort@coords
    • 最好不要使用@,但是在这种情况下coordinates(search_effort)
    • 谢谢,感谢您的宝贵时间和帮助!
    猜你喜欢
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    相关资源
    最近更新 更多