用R中data.frame的值替换栅格类

时间:2016-08-29 12:29:46

标签: r dataframe raster

我有一个分类结果的整数栅格。现在我想用数据帧中的浮点值替换类,即栅格类1 = 0.321; 2级= 0.232; 3级= 3.211。 数据框有很多列,我想替换几种不同情况的类:

Class C      N      ....
1    0.321   0.001 
2    0.232   0.012 
3    3.211   0.021 

有没有办法方便地执行此操作,例如将data.frame合并到栅格中? 我需要将生成的栅格与另一个栅格相乘以生成输出。

这是光栅文件的元数据:

 > LCC
 class       : RasterLayer 
 dimensions  : 3296, 3711, 12231456  (nrow, ncol, ncell)
 resolution  : 2, 2  (x, y)
 extent      : 514151.8, 521573.8, 7856419, 7863011  (xmin, xmax, ymin, ymax)
 coord. ref. : +proj=utm +zone=55 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
 data source : /home/..../Raster.tif 
 names       : Raster 
 values      : 0, 255  (min, max)

`

这是数据帧的元数据:

 >str(SOC)
 data.frame':   11 obs. of  57 variables:
  $ class             : int  8 9 5 6 7 4 1 2 3 0 ...
  $ area              : int  3135964 3941744 9048672 8564312 11568512     
  $ pixel_count       : int  783991 985436 2262168 2141078 2892128 ...
  $ percent_area      : Factor w/ 11 levels "0.17%","17.50%",..: 9 11 3 2 
  $ label.x           : Factor w/ 11 levels "Barren",..: 5 8 2 
  $ label.y           : Factor w/ 8 levels "Barren",..: 4 7 2 
  $ n                 : int  7 4 4 3 4 1 1 NA NA NA ...
  $ mean_C_100cm      : num  25.8 29 21.3 34.8 31.9 ...
  $ mean_N_100cm      : num  0.469 0.514 0.503 0.621 0.34 ...
 ....

`

3 个答案:

答案 0 :(得分:3)

有一个函数(subs)。

示例数据(关于achaio)

library(raster)
inp <- raster(ncol=10, nrow=10) 
set.seed(42)
inp[] <- sample(3, ncell(inp), replace=TRUE)
df <- data.frame(Class=c(1,2,3), C=c(0.321,0.232,3.211), N=c(0.001,0.012,0.021))

要用“C”替换标识符,您可以

x <- subs(inp, df, by=1, which=2)

或者,要同时获得“C”和“N”,请执行

y <- subs(inp, df, by=1, which=2:3)

事实上,正如梅斯指出的那样,你也可以使用reclassify(但仅限于一个变量)

z <- reclassify(inp, as.matrix(df)[, 1:2])

答案 1 :(得分:1)

如果我理解你想要什么,那么你可以使用df中的列中的值来分配栅格的值:

inp[] <- df[inp[],"C"]

其中df与您定义的一样,inp是整数栅格,值为1到3。

例如:

library(raster)
set.seed(42)  ## for reproducibility

inp <- raster(ncol=10, nrow=10) ## example is small, yours will be large
inp[] <- floor(runif(ncell(inp), min=1, max=4))  ## generate integers from 1 to 3
inp[]
##  [1] 3 3 1 3 2 2 3 1 2 3 2 3 3 1 2 3 3 1 2 2 3 1 3 3 1 2 2 3 2 3 3 3 2 3 1 3 1 1 3 2 2 2 1 3 2 3 3 2
## [49] 3 2 2 2 2 3 1 3 3 1 1 2 3 3 3 2 3 1 1 3 3 1 1 1 1 2 1 3 1 2 2 1 2 1 2 2 3 2 1 1 1 1 3 1 1 3 3 3
## [97] 1 2 3 2

df <- data.frame(Class=c(1,2,3), C=c(0.321,0.232,3.211), N=c(0.001,0.012,0.021))  ## your df

## generate output raster the same size as inp
out <- raster(ncol=10,nrow=10)
## map values of out to values in column C of df
## can overwrite inp here if desired, but for example we want to keep inp
## for following steps
out[] <- df[inp[],"C"]
out[]
##  [1] 3.211 3.211 0.321 3.211 0.232 0.232 3.211 0.321 0.232 3.211 0.232 3.211 3.211 0.321 0.232 3.211
## [17] 3.211 0.321 0.232 0.232 3.211 0.321 3.211 3.211 0.321 0.232 0.232 3.211 0.232 3.211 3.211 3.211
## [33] 0.232 3.211 0.321 3.211 0.321 0.321 3.211 0.232 0.232 0.232 0.321 3.211 0.232 3.211 3.211 0.232
## [49] 3.211 0.232 0.232 0.232 0.232 3.211 0.321 3.211 3.211 0.321 0.321 0.232 3.211 3.211 3.211 0.232
## [65] 3.211 0.321 0.321 3.211 3.211 0.321 0.321 0.321 0.321 0.232 0.321 3.211 0.321 0.232 0.232 0.321
## [81] 0.232 0.321 0.232 0.232 3.211 0.232 0.321 0.321 0.321 0.321 3.211 0.321 0.321 3.211 3.211 3.211
## [97] 0.321 0.232 3.211 0.232

## can create a brick and add layers that map values from N column of df
out.brick <- brick(x=out)
out[] <- df[inp[],"N"]
out.brick <- addLayer(out.brick, out)
out.brick[[2]][]
##  [1] 0.021 0.021 0.001 0.021 0.012 0.012 0.021 0.001 0.012 0.021 0.012 0.021 0.021 0.001 0.012 0.021
## [17] 0.021 0.001 0.012 0.012 0.021 0.001 0.021 0.021 0.001 0.012 0.012 0.021 0.012 0.021 0.021 0.021
## [33] 0.012 0.021 0.001 0.021 0.001 0.001 0.021 0.012 0.012 0.012 0.001 0.021 0.012 0.021 0.021 0.012
## [49] 0.021 0.012 0.012 0.012 0.012 0.021 0.001 0.021 0.021 0.001 0.001 0.012 0.021 0.021 0.021 0.012
## [65] 0.021 0.001 0.001 0.021 0.021 0.001 0.001 0.001 0.001 0.012 0.001 0.021 0.001 0.012 0.012 0.001
## [81] 0.012 0.001 0.012 0.012 0.021 0.012 0.001 0.001 0.001 0.001 0.021 0.001 0.001 0.021 0.021 0.021
## [97] 0.001 0.012 0.021 0.012

希望这有帮助。

答案 2 :(得分:0)

我使用重分类函数重新编写了aichio的示例。这是一种对我有用的方法:

library(raster)
set.seed(42)  ## for reproducibility

inp <- raster(ncol=10, nrow=10) ## example is small, yours will be large
inp[] <- floor(runif(ncell(inp), min=1, max=4))  ## generate integers from 1 to 3

df <- data.frame(Class=c(1,2,3), C=c(10.321,1.232,0.211), N=c(0.001,0.012,0.021))  ## your df

## generate output raster the same size as inp
out <- raster(ncol=10,nrow=10)


#### here I generate a matrix that defines the
#### reclassification with an upper and lower limit

mtr <- data.frame(cl_low =df$Class -1, cl_high = df$Class, C =df$C)
data.matrix(mtr)  ### use as matrix

# now reclassify using the matrix and transfer the result in a raster brick

out <- reclassify(inp, rcl=mtr)
out.brick <- brick(x=out)

# now the same can be done for next variable
mtr <- data.frame(cl_low =df$Class -1, cl_high = df$Class, N =df$N)
data.matrix(mtr)
out <- reclassify(inp, rcl=mtr)
out.brick <- addLayer(out.brick, out)
out.brick@layers