将 SpatialPointsDataframe 转换为 SpatialGridDataframe

Turn SpatialPointsDataframe to SpatialGridDataframe

我正在尝试将 SpatialPointsDataframe 转换为 SpatialGridDataframe,以便使用 landsat 包中的 slopeasp() 函数来获取坡度值。我尝试将 SpatialPointsDataframe 转换为栅格以进行转换,但它也没有用,我收到错误消息。

as(pts, "spatialGridDataFrame") 错误: 没有将“SpatialPointsDataFrame”强制转换为“spatialGridDataFrame”的方法或默认值

下面是我的代码:

#create fake data
x <- rep(rep(seq(12,36,0.5),41))
y <-rep(seq(32,52,0.5), each=49)
z <- rnorm(2009, 26.5, 44.0)
pts <- as.data.frame(matrix(c(x,y,z),  ncol=3,  byrow=FALSE))
colnames(pts)=c("x", "y", "z")

# create a SpatialPointsDataFrame
coordinates(pts) = ~x+y                                        
# create an empty raster object to the extent of the points
rast <- raster(ext=extent(pts), resolution=250)
# rasterize  irregular points 
rasOut<-rasterize(pts, rast, pts$z, fun = mean)

#attempt from spdf to sgdf
sgdf <- as(pts, 'spatialGridDataFrame')
#atempt from raster to sgdf
sgdf <- as(rasOut, 'spatialGridDataFrame')

有人对如何获取 spatialGridDataFrame 有任何建议吗?谢谢!

asspatialGridDataFrame 的转换需要 class class SpatialPixelsDataFrame 的对象:

Objects from the Class

Objects can be created by calls of the form as(x, "SpatialGridDataFrame"), where x is of class SpatialPixelsDataFrame-class, or by importing through rgdal. Ordered full grids are stored instead or unordered non-NA cells;

因此,您可以将 pts 转换为 SpatialPixelsDataFrame,然后将结果 SpatialPixelsDataFrame 转换为 SpatialGridDataFrame

#attempt from spdf to sgdf
sgdf <- as(pts, 'SpatialPixelsDataFrame')

# Convert the SpatialPixelsDataFrame (sgdf) to spatialGridDataFrame
sgdf <- as(sgdf, "SpatialGridDataFrame")

您也可以用同样的方式转换您的 rasOut。在一行代码中,这是:

#atempt from raster to sgdf
sgdf <- as(as(rasOut, 'SpatialPixelsDataFrame'), 'SpatialGridDataFrame')