【问题标题】:Joining tables to a raster attribute table to create new raster with the reclassified values python or R将表连接到栅格属性表以创建具有重新分类值 python 或 R 的新栅格
【发布时间】:2019-02-04 00:37:00
【问题描述】:

我正在尝试加入具有栅格分类的 .csv 文件,以使用新信息创建栅格。

.csv 文件中的 PatchID 对应于栅格 DN。

.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

光栅可能看起来像:

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

新的栅格应该如下所示:

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

我可以通过对栅格进行多边形化并将表格连接到 shapefile 来完成这项工作,但这个过程是 CPU 密集型的。

【问题讨论】:

    标签: python r join classification raster


    【解决方案1】:

    这是一个简单快速的解决方案(无需连接):

    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
    

    在这段代码中,我使用来自m 的值作为df$shape_index 中的位置[+1 调整]

    输入数据:

    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)
    

    【讨论】:

    • 非常感谢!它直接与我的栅格分类一起使用,然后我只需要将其转换回栅格。 reclass &lt;- matrix(df[,"shape_index"][as.vector(raster)+1])reclass_rast &lt;- raster(reclass, template = raster)
    • 如果 PatchID 值是连续的,这非常有效,当我必须将值更改为 0 时是否有解决方案。例如:m[m==3] &lt;- 0df&lt;-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))df &lt;- df[-4,]
    • 嗨@Pixelschubser,欢迎回来。我建议你打开一个新问题(链接到这个问题)并插入你想要的输出。之后我会很高兴地帮助你
    猜你喜欢
    • 1970-01-01
    • 2018-09-15
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    相关资源
    最近更新 更多