如何将 class 名称添加到 terra SpatRaster 中的数字栅格值?

How can I add a class name to numeric raster values in a terra SpatRaster?

我正在使用 Circumpolar Arctic Vegetation 地图。存储为带 terra 的 SpatRaster,栅格有 21 个土地覆盖 classes.

> str(lc_2003)
Formal class 'SpatRaster' [package "terra"] with 1 slot
  ..@ ptr:Reference class 'Rcpp_SpatRaster' [package "terra"] with 17 fields
  .. ..$ depth    : num 0
  .. ..$ extent   :Reference class 'Rcpp_SpatExtent' [package "terra"] with 2 fields
  .. .. ..$ valid : logi TRUE
  .. .. ..$ vector: num [1:4] 3946387 7965081 2200504 5681579
  .. .. ..and 27 methods, of which 13 are  possibly relevant:
  .. .. ..  align, as.points, ceil, compare, finalize, floor, initialize, intersect, round, sample,
  .. .. ..  sampleRandom, sampleRegular, union
  .. ..$ filenames: chr ""
  .. ..$ hasRange : logi TRUE
  .. ..$ hasTime  : logi FALSE
  .. ..$ hasValues: logi TRUE
  .. ..$ inMemory : logi TRUE
  .. ..$ messages :Reference class 'Rcpp_SpatMessages' [package "terra"] with 2 fields
  .. .. ..$ has_error  : logi FALSE
  .. .. ..$ has_warning: logi FALSE
  .. .. ..and 18 methods, of which 4 are  possibly relevant:
  .. .. ..  finalize, getError, getWarnings, initialize
  .. ..$ names    : chr "PHYSIOG"
  .. ..$ origin   : num [1:2] 102.7 91.3
  .. ..$ range_max: num 21
  .. ..$ range_min: num 1
  .. ..$ res      : num [1:2] 5172 3881
  .. ..$ rgb      : logi FALSE
  .. ..$ time     : num 0
  .. ..$ timestep : chr "seconds"
  .. ..$ units    : chr ""

但是,我想将图层 PHYSIOG 中的每个值与其实际的土地覆盖 class 名称相关联。这对我在 ArcGis 中查看文件以及评估某些调查地块属于哪种栖息地类型很有用。

landcover_classes <- 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"))

如何将此数据添加到 SpatRaster?

(我不确定如何制作 SpatRaster 的可重现示例。我将在另一个问题中提出这个问题)

你应该可以做到

levels(lc_2003) <- landcover_classes

并通过

查看结果
plot(lc_2003)

参见 ?terra::levels 中的示例。