是否有任何解决方案可以使用 R 将 X 或 Y 坐标分配给光栅图像的所有单元格?
例如,假设我有一个包含 3x3 像元的栅格。左下坐标为X=7,Y=15(以米为单位),X和Y方向分辨率均为+10 m(X向右增加,Y向上增加)。然后,我想生成栅格表面,其中每个单元都有 X 和 Y 值,如下所示:
X raster
7 17 27
7 17 27
7 17 27
Y surface
35 35 35
25 25 25
15 15 15
更新:this是实际的栅格对象。
有什么办法可以做到这一点吗?
我尝试了“raster”包,但找不到解决方案。
如有任何帮助,我们将不胜感激。
最佳答案
这是我在 @Carl 提交的评论中受到启发而找到的解决方案。
假设我的栅格名为 d
,具有以下特征:
class : RasterLayer
dimensions : 59, 67, 3953 (nrow, ncol, ncell)
resolution : 90, 90 (x, y)
extent : 482855.6, 488885.6, 4763517, 4768827 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=-3 +k=1 +x_0=500000 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : subset
values : 328, 1120 (min, max)
我首先生成了一个带有 x 坐标值的矩阵:
xm<-matrix(xFromCell(d,c(1:3953)),nrow=59,byrow=TRUE)
然后使用矩阵创建栅格:
x<-raster(xm,xmn=482855.6, xmx=488885.6,ymn=4763517,ymx=4768827)
最后,指定其投影:
projection(x)<-"+proj=tmerc +lat_0=0 +lon_0=-3 +k=1 +x_0=500000 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
我还显示了栅格,看看结果是否正常...
plot(x)
...并阅读光栅描述
x
class : RasterLayer
dimensions : 59, 67, 3953 (nrow, ncol, ncell)
resolution : 90, 90 (x, y)
extent : 482855.6, 488885.6, 4763517, 4768827 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=-3 +k=1 +x_0=500000 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : layer
values : 482900.6, 488840.6 (min, max)
我重复了这些步骤,但在第一步中使用了 yFromCell
。
关于R 将 x 或 y 坐标分配给栅格的像元以执行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22848836/