将矩阵转换为R中的栅格 [英] convert matrix to raster in R

查看:969
本文介绍了将矩阵转换为R中的栅格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有空间坐标和一个变量的矩阵数据.空间分辨率为1000米.

I have a matrix data with spatial coordinates and one variable. The spatial resolution is 1000 meters.

> str(dat1)
> List of 3
> $ x: num [1:710] 302340 303340 304340 305340 306340 ...
> $ y: num [1:1241] 5431470 5432470 5433470 5434470 5435470 ...
> $ z: num [1:710, 1:1241] 225 225 225 225 225 ...

我想将其转换为栅格格式.

I want to convert it into raster format.

> dat1$x[1:10]
> [1] 302339.6 303339.6 304339.6 305339.6 306339.6 307339.6 308339.6 309339.6 310339.6 311339.6
> dat1$y[1:10]
>  [1] 5431470 5432470 5433470 5434470 5435470 5436470 5437470 5438470 5439470 5440470

我用下面的代码来做到这一点.但是我得到的分辨率与我得到的分辨率不同.有什么更好的方法可以使我的真实数据获得相同的分辨率?

I used the following code to do it. But the resolution I get is not the same with that I have. Any better way to get the same resolution with my real data?

> r <-raster(
             dat1$z,
             xmn=range(dat1$x)[1], xmx=range(dat1$x)[2],
             ymn=range(dat1$y)[1], ymx=range(dat1$y)[2], 
             crs=CRS("+proj=utm +zone=11 +datum=NAD83")
            )
> r

class       : RasterLayer 
dimensions  : 710, 1241, 881110  (nrow, ncol, ncell)
resolution  : 571.3135, 1746.479  (x, y)
extent      : 302339.6, 1011340, 5431470, 6671470  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=11 +datum=NAD83 
data source : in memory
names       : layer 
values      : 13.65059, 248.6229  (min, max)

推荐答案

尝试阅读栅格帮助.从矩阵创建栅格时,行和列的感觉与您想像的不一样.您正在给它提供一个1241x710的矩阵,但要从错误的向量中获取最大值和最小值.

Try reading the help for raster. When creating a raster from a matrix, the sense of rows and columns isn't what you think it is. You were feeding it a 1241x710 matrix but taking the max and min from the wrong vectors.

尝试以下操作:

> # small version of your test set
> dat1=list()
> dat1$x=seq(302339.6,by=1000,len=71)
> dat1$y=seq(5431470,by=1000,len=124)
> dat1$z=matrix(runif(71*124),71,124)
> str(dat1)
List of 3
 $ x: num [1:71] 302340 303340 304340 305340 306340 ...
 $ y: num [1:124] 5431470 5432470 5433470 5434470 5435470 ...
 $ z: num [1:71, 1:124] 0.765 0.79 0.185 0.461 0.421 ...
> image(dat1,asp=1)

不错的正方形像素.现在创建您的栅格:

Nice square pixels. Now create your raster:

r <-raster(
             dat1$z,
             xmn=range(dat1$x)[1], xmx=range(dat1$x)[2],
             ymn=range(dat1$y)[1], ymx=range(dat1$y)[2], 
             crs=CRS("+proj=utm +zone=11 +datum=NAD83")
            )
plot(r)

完全非正方形像素.而且,如果仔细看,矩阵会从图像图中旋转90度.或转置之类的东西.

Totally NON-square pixels. And if you look carefully, the matrix is rotated 90 degrees from the image plot. Or transposed or something.

解决方案:只需从x,y,z列表创建栅格:

Solution: just create the raster from the x,y,z list:

 > r=raster(dat1);plot(r)

平方像素,与图像绘制相同,并且分辨率现在是您期望的:

Square pixels, same way round as image plot, and resolution is now what you expect:

> r
class       : RasterLayer 
dimensions  : 124, 71, 8804  (nrow, ncol, ncell)
resolution  : 1000, 1000  (x, y)
extent      : 301839.6, 372839.6, 5430970, 5554970  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : in memory
names       : layer 
values      : 7.738103e-05, 0.9995497  (min, max)

这篇关于将矩阵转换为R中的栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆