【问题标题】:How can I add a class name to numeric raster values in a terra SpatRaster?如何将类名添加到 terra SpatRaster 中的数值栅格值?
【发布时间】:2021-09-08 07:45:08
【问题描述】:

我正在使用极地北极植被地图。存储为带有 terra 的 SpatRaster,该栅格具有 21 个土地覆盖类别。

> 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 层中的每个值与其实际的土地覆盖类名称相关联。这对我在 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 示例。我将在单独的问题中提出这个问题)

【问题讨论】:

    标签: r gis raster terra


    【解决方案1】:

    你应该可以的

    levels(lc_2003) <- landcover_classes
    

    查看结果

    plot(lc_2003)
    

    请参阅?terra::levels 中的示例。

    【讨论】:

    • 谢谢。我想象我需要合并某种合并,我很难想象数据是如何存储在 SpatRaster 中的。我包含在我的 landcover_classes 数据框中的“lc_code”列是否需要将类名与栅格值匹配?当我只包含 lc_class 列时,我尝试了两种方法并收到此警告消息:警告消息:在类(对象)
    • 无论哪种方式都可以(请参阅 ?levels 中的示例) --- 因为无法生成警告消息,所以我无法说出警告消息。
    猜你喜欢
    • 2021-11-15
    • 2021-09-17
    • 2021-04-13
    • 2022-01-23
    • 1970-01-01
    • 2021-11-30
    • 2022-07-13
    • 2021-09-08
    • 1970-01-01
    相关资源
    最近更新 更多