如何解决将SpatialPointsDataframe转换为SpatialGridDataframe
我正在尝试将SpatialPointsDataframe转换为SpatialGridDataframe,以便使用landsat包中的lopersp()函数来获取坡度的值。我试图将SpatialPointsDataframe转换为栅格以将其转换,但它也无法正常工作,并且出现错误。
as(pts,“ spatialGridDataFrame”)中的错误: 没有将“ SpatialPointsDataFrame”强制为“ spatialGridDataFrame”的方法或默认值
以下是我的代码:
#create fake data
x <- rep(rep(seq(12,36,0.5),41))
y <-rep(seq(32,52,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有任何建议吗?谢谢!
解决方法
从as
到spatialGridDataFrame
的转换需要一个class SpatialPixelsDataFrame
类的对象:
类中的对象
可以通过调用as(x, “ SpatialGridDataFrame”),其中x是类 SpatialPixelsDataFrame类,或通过rgdal导入。已订购 完整的网格被存储,而不是存储或无序的非NA单元;
因此,您可以将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')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。