简体   繁体   中英

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.

The PatchID in the .csv file corresponds to the raster DN.

The .csv file looks like:

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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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