R 将 x 或 y 坐标分配给栅格单元以执行计算 [英] R Assigning x or y coordinate to cells of a raster to perform calculations
问题描述
是否有任何解决方案可以使用 R 将 X 或 Y 坐标分配给光栅图像的所有单元格?
Is there any solution to assign to assign X or Y coordinate to all of the cells of a raster image using R?
例如,假设我有一个包含 3x3 像元的栅格.左下坐标为 X=7,Y=15(米),X 和 Y 方向的分辨率均为 +10 m(X 向右增加,Y 向上).然后,我想生成每个单元格都有 X 和 Y 值的栅格表面,如下所示:
For example, suppose I have a raster with 3x3 cells. The lower left coordinate is X=7,Y=15 (in meters), and resolution is +10 m for both, X and Y direction (X increases rightward, and Y upward). Then, I would like to generate raster surfaces where each cell has the X and Y value, like these ones:
X raster
7 17 27
7 17 27
7 17 27
Y surface
35 35 35
25 25 25
15 15 15
更新:这个是实际的光栅对象.
UPDATE: this is the actual raster object.
有没有办法做到这一点?
Is there any way to do this?
我尝试了包光栅",但找不到解决方案.
I tried the package 'raster', but couldn't find a solution.
任何帮助将不胜感激.
推荐答案
这是我找到的解决方案,灵感来自@Carl 提交的评论.
This is the solution that I found, inspired in the comment submitted by @Carl.
假设我的栅格名为 d
,具有以下特征:
Suppose my raster is called d
, with these characteristics:
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 坐标值生成了一个矩阵:
I first generated a matrix with the x coordinates values:
xm<-matrix(xFromCell(d,c(1:3953)),nrow=59,byrow=TRUE)
然后用矩阵创建一个栅格:
Then created a raster with the matrix:
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"
我还显示了光栅,看看结果是否正常...
I also displayed the raster, to see if results where OK...
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
.
I repeated these steps, but using yFromCell
in the first step.
这篇关于R 将 x 或 y 坐标分配给栅格单元以执行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!