简体   繁体   English

将表连接到栅格属性表以使用重新分类的值python或R创建新栅格

[英]Joining tables to a raster attribute table to create new raster with the reclassified values python or R

I'm trying to join a .csv file with a raster classification to create a raster with the new information. 我正在尝试将具有栅格分类的.csv文件加入,以使用新信息创建栅格。

The PatchID in the .csv file corresponds to the raster DN. .csv文件中的PatchID对应于栅格DN。

The .csv file looks like: .csv文件如下所示:

PatchID       area  shape_index 
      0   11592800        24.78
      1    2556440         7.02
      2    3562800        14.70
      3    2945450         2.48
      4    3892610         6.36
      5 7678785680        11.68
      6 1756784547         2.43

the Raster can look like: 栅格看起来像:

0 0 0 0 0 0 1 1 1 1 0
0 0 2 2 0 0 1 1 0 0 0
0 0 0 2 2 0 0 0 0 3 3
0 0 2 0 2 0 4 4 0 3 3
0 0 0 0 2 0 0 4 0 0 3
0 5 0 0 0 0 4 4 0 0 0
0 5 5 5 0 0 0 0 0 6 6

the new raster should look like: 新的栅格应如下所示:

24.78 24.78 24.78 24.78 24.78 24.78  7.02  7.02  7.02  7.02 24.78
24.78 24.78 14.70 14.70 24.78 24.78  7.02  7.02 24.78 24.78 24.78
24.78 24.78 24.78 14.70 14.70 24.78 24.78 24.78 24.78  2.48  2.48
24.78 24.78 14.70 24.78 14.70 24.78  6.36  6.36 24.78  2.48  2.48
24.78 24.78 24.78 24.78 14.70 24.78 24.78  6.36 24.78 24.78  2.48
24.78 11.68 24.78 24.78 24.78 24.78  6.36  6.36 24.78 24.78 24.78
24.78 11.68 11.68 11.68 24.78 24.78 24.78 24.78 24.78  2.43  2.43

I can do the job by polygonizing the raster and join the table to the shapefile but this procedure is CPU-intensive. 我可以通过将栅格多边形化并将表格连接到shapefile来完成这项工作,但是此过程需要占用大量CPU。

Here an easy and fast solution (without join): 这里是一个简单快速的解决方案(无需加入):

t((matrix(df[,"shape_index"][as.vector(m)+1],nrow=11,ncol=7)))
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11]
[1,] 24.78 24.78 24.78 24.78 24.78 24.78  7.02  7.02  7.02  7.02 24.78
[2,] 24.78 24.78 14.70 14.70 24.78 24.78  7.02  7.02 24.78 24.78 24.78
[3,] 24.78 24.78 24.78 14.70 14.70 24.78 24.78 24.78 24.78  2.48  2.48
[4,] 24.78 24.78 14.70 24.78 14.70 24.78  6.36  6.36 24.78  2.48  2.48
[5,] 24.78 24.78 24.78 24.78 14.70 24.78 24.78  6.36 24.78 24.78  2.48
[6,] 24.78 11.68 24.78 24.78 24.78 24.78  6.36  6.36 24.78 24.78 24.78
[7,] 24.78 11.68 11.68 11.68 24.78 24.78 24.78 24.78 24.78  2.43  2.43

In this code I'm using values from m as a position [+1 adjusted] in df$shape_index 在这段代码中,我使用m中的值作为df$shape_index的位置[+1调整]

Input data: 输入数据:

df<-data.frame(PatchID=c(0,1,2,3,4,5,6),
area=c(11592800,2556440,3562800,2945450,3892610,7678785680,1756784547),
shape_index=c(24.78,7.02,14.70,2.48,6.36,11.68,2.43))

m<-matrix(c(0,0,0,0,0,0,1,1,1,1,0,
0,0,2,2,0,0,1,1,0,0,0,
0,0,0,2,2,0,0,0,0,3,3,
0,0,2,0,2,0,4,4,0,3,3,
0,0,0,0,2,0,0,4,0,0,3,
0,5,0,0,0,0,4,4,0,0,0,
0,5,5,5,0,0,0,0,0,6,6),nrow=7,ncol=11)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM