在 terra SpatRaster 中命名分类级别时,如何防止添加额外级别?

How can I prevent an extra level from being added when naming categorical levels in terra SpatRaster?

我有一个包含 21 个类别的分类栅格:

>lc_2003

class       : SpatRaster 
dimensions  : 685, 827, 1  (nrow, ncol, nlyr)
resolution  : 4477.525, 4477.525  (x, y)
extent      : 4224289, 7927202, 2358424, 5425529  (xmin, xmax, ymin, ymax)
coord. ref. : NAD83 / Statistics Canada Lambert 
source      : memory 
name        : PHYSIOG 
min value   :       1 
max value   :      21 

>levels(lc_2003)
[[1]]
[1] ""

我想为每个分类级别命名。但是,当我这样做时,似乎添加了一个额外的级别,因此现在有 22 个级别。列表开头有一个额外的“NA”级别。

landcover_classes_cavmVec <- data.frame(lc_code = 1:21, lc_class = c("Cryptogam, herb barren", "Rush/grass, forb, cryptogam tundra", "Cryptogam barren complex (bedrock)",
                                                                     "Prostrate dwarf-shrub, herb tundra", "Graminoid, prostrate dwarf-shrub, forb tundra", "Prostrate/Hemiprostrate dwarf-shrub tundra", "Nontussock sedge, dwarf-shrub, moss tundra",
                                                                     "Tussock-sedge, dwarf-shrub, moss tundra", "Erect dwarf-shrub tundra", "Low-shrub tundra", "Missing (Cryprogram dwarf-shrub?)", "Sedge/grass, moss wetland",
                                                                     "Sedge, moss, dwarf-shrub wetland", "Sedge, moss, low-shrub wetland", "Noncarbonate mountain complex", "Carbonate mountain complex", "Nunatak complex",
                                                                     "Glaciers", "Water", "Lagoon", "Non-Arctic areas"))

>levels(lc_2003) <- landcover_classes_cavmVec

> levels(lc_2003)
[[1]]
 [1] NA                                              "Cryptogam, herb barren"                       
 [3] "Rush/grass, forb, cryptogam tundra"            "Cryptogam barren complex (bedrock)"           
 [5] "Prostrate dwarf-shrub, herb tundra"            "Graminoid, prostrate dwarf-shrub, forb tundra"
 [7] "Prostrate/Hemiprostrate dwarf-shrub tundra"    "Nontussock sedge, dwarf-shrub, moss tundra"   
 [9] "Tussock-sedge, dwarf-shrub, moss tundra"       "Erect dwarf-shrub tundra"                     
[11] "Low-shrub tundra"                              "Missing (Cryprogram dwarf-shrub?)"            
[13] "Sedge/grass, moss wetland"                     "Sedge, moss, dwarf-shrub wetland"             
[15] "Sedge, moss, low-shrub wetland"                "Noncarbonate mountain complex"                
[17] "Carbonate mountain complex"                    "Nunatak complex"                              
[19] "Glaciers"                                      "Water"                                        
[21] "Lagoon"                                        "Non-Arctic areas" 

如何防止这种情况发生?

奖金问题:检查 landcover_classes_cavmVec 中的 lc_code 变量是否确实与原始 1:21 [=14] 中的等效数字相匹配的好策略是什么=] 水平?我不知道如何验证此代码是否按照我的要求执行。

这是从?terra::levels

复制的
library(terra)
r <- rast(nrows=10, ncols=10)
set.seed(0)
values(r) <- sample(3, ncell(r), replace=TRUE)

我们有一个值为 1、2 和 3 的栅格。

您可以像这样分配类别

cls <- c("forest", "water", "urban")
d <- data.frame(id=1:3, cover=cls)
levels(x) <- d
x

levels(x)
#[[1]]
#[1] NA       "forest" "water"  "urban" 

cats(x)
#[[1]]
#  ID  cover
#1  0   <NA>
#2  1 forest
#3  2  water
#4  3  urban

因此,对于值为零的单元格,还有一个附加类别。这是为了适应 GDAL 库读取和写入类别的方式(它假定值介于 0 和 255 之间)。这就是你所看到的,这是正确的。

或者,您可以更改栅格,使其从零开始

x <- r - 1
levels(x) <- cls
names(x) <- "land cover"
levels(x)
#[[1]]
#[1] "forest" "water"  "urban" 

cats(x)
#[[1]]
#  ID category
#1  0   forest
#2  1    water
#3  2    urban

手册还显示了以下方法。也就是说,为缺失级别添加另一个(空)标签(在本例中为零)。

levels(r) <- c("", cls)