Search code examples
rgisrasterterra

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


I'm working with the Circumpolar Arctic Vegetation map. Stored as a SpatRaster with terra, the raster has 21 land cover 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 ""

However, I would like to associate each value in the layer PHYSIOG with it's actual landcover class name. This would be useful to me for viewing the file in ArcGis, as well as for assessing which habitat type certain survey plots fall in.

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

How could I add this data to the SpatRaster?

(I'm not sure how to make a reproducible example of a SpatRaster. I'm going to ask this in a separate question)


Solution

  • You should be able to do

    levels(lc_2003) <- landcover_classes
    

    And see the results with

    plot(lc_2003)
    

    See the examples in ?terra::levels.