我有一个 3 维的 SpatialPoints 对象:
x <- c(1,1,1,2,2,2,3,3,3)
y <- c(1,2,3,1,2,3,1,2,3)
z <- c(1,3,5,2,1,2,1,2,3)
xyz <- cbind(x,y,z)
ss <- SpatialPoints(xyz)
dimensions(ss)
还有一个栅格
对象:
rr <- raster(matrix(round(runif(49,0,10)),7,7), xmn=0, xmx=4, ymn=0, ymx=4, crs=NA, template=NULL)
我想使用 SpatialPoints
对象提取栅格值:
extract(rr,ss)
#Error in .xyValues(x, coordinates(y), ..., df = df) :
# xy should have 2 columns only.
#Found these dimensions: 9, 3
如果需要,您可以可视化数据:
plot(rr)
plot(ss, add=T)
所以问题是栅格包的extract
函数需要一个二维SpatialPoints对象。我的(在我的真实数据中)是 3 维的。有没有办法删除我的点形状的第三个维度?我试过:
coordinates(ss) <- coordinates(ss)[,-3]
#Error in `coordinates<-`(`*tmp*`, value = c(1, 1, 1, 2, 2, 2, 3, 3, 3, :
# setting coordinates cannot be done on Spatial objects, where they have #already been set
我不想从头开始重建我的形状。
最佳答案
只需覆盖 S4 对象的 coords
槽即可:
ss@coords <- ss@coords[, 1:2]
我不知道您的 SpatialPoints
对象是如何创建的,但如果您使用 rgdal::readOGR
,则会有一个 pointDropZ
参数(默认FALSE
)
关于删除 SpatialPointsDataFrame 中的第三个维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41227500/