2

我有一个分类栅格,它有 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" 

我该如何防止这种情况发生?

lc_code额外的问题:检查变量是否landcover_classes_cavmVec确实与原始 1:21lc_2003级别中的等效数字匹配的好策略是什么?我不知道如何验证这段代码是否符合我的要求。

4

1 回答 1

2

这是复制自?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)
于 2021-12-21T21:36:44.713 回答