0

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)

canderson156
  • 1,045
  • 10
  • 24

1 Answers1

1

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.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thanks. I was imagining I needed to merge some kind of merge, and I was having a hard time picturing how the data is stored in a SpatRaster. Is the "lc_code" column I included in my landcover_classes dataframe necessary to match the class name with the raster value? I tried it both ways and recieved this warning message when I only included the lc_class column: Warning message: In class(object) <- "environment" : Setting class(x) to "environment" sets attribute to NULL; result will no longer be an S4 object – canderson156 Jun 25 '21 at 16:16
  • You can do it either way (see the examples in ?levels) --- I cannot speak to the warning message as I cannot generate it. – Robert Hijmans Jun 25 '21 at 19:08