相当于 raster::deratify in terra r package

问题描述 投票:0回答:1

我有一个带有栅格属性表 (RAT) 的栅格。我想使用栅格属性表的列值之一生成新栅格。可以按照此

问题
中所述使用raster::deratify来完成。我从
raster
包中给出一个简单的例子

library(raster)
r <- raster(nrow=10, ncol=10)
values(r) = 1
r[51:100] = 2
r[3:6, 1:5] = 3
r <- ratify(r)
rat <- levels(r)[[1]]
rat$landcover <- c("Pine", "Oak", "Meadow")
rat$code <- c(0.002,0.0056,0.0124)
levels(r) <- rat
r
# extract values for some cells
i <- extract(r, c(1, 2, 25, 100))
i
# get the attribute values for these cells
factorValues(r, i)

# create a single-layer raster using the code of RAT
x <- deratify(r, "code")

extract(x, c(1, 2, 25, 100))

如何在

terra
R包中实现?

r r-raster terra
1个回答
0
投票

根据您的示例 RasterLayer 制作 SpatRaster

library(terra)
x <- rast(r)
x
#class       : SpatRaster 
#dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
#resolution  : 36, 18  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
#source(s)   : memory
#categories  : landcover, code 
#name        :  layer 
#min value   :   Pine 
#max value   : Meadow 

“代码”是第二类,所以你可以做

y <- as.numeric(x, 2)
y
#class       : SpatRaster 
#dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
#resolution  : 36, 18  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
#source(s)   : memory
#name        :   code 
#min value   : 0.0020 
#max value   : 0.0124
© www.soinside.com 2019 - 2024. All rights reserved.