Skip to main content
updated to reflect changes in terra
Source Link
Robert Hijmans
  • 11.1k
  • 27
  • 37

See the cats and rats methods. Or levels which is the same as cats. cats is for categories (ID and label), rats for Raster Attribute Tables (ID and n labels). cats are more limited than rats, but generally easier to deal withmethods. They are used to have rasters that behave like "factors".

With the specific file, the problem is that terra uses GDAL to read it, and the GDAL driver for GRD/GRI is rather incomplete. RATS are read fine, I think, when using e.g. GeoTIFF The issues you describe have been fixed in terra 1. I may write a translator to make it easier1-17 (on its way to extract this from GRD/GRICRAN). But forFor now, what you can do is something along these lines:

library(raster)
f <- "nlcd_agg.grd"
r <- raster(f)

library(terra)
x <- rast(r)

# get the attributes
lev <- levels(r)[[1]]

Now set the attributes

# this fails because of a bug 
rats(x) <- lev
# work around 
rat <- lev
rats(x) <- lev
str(rats(x))

They are there, but they are ignored. I would use cats/levels.

lev <- lev[, c("ID", "Land.Cover.Class")]
lev[,2] <- as.character(lev[,2])

x <- rast(r)
levels(x) <- lev
is.factor(x)
#[1] TRUE

x
#class       : SpatRaster 
#dimensions  : 101, 121, 1  (nrow, ncol, nlyr)
#resolution  : 3750, 3750  (x, y)
#extent      : 1394535, 1848285, 1722765, 2101515  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
#source      : memory 
#name        : nlcd_2011_landcover_2011_edition_2014_03_31 
#min value   :                                Unclassified 
#max value   :                                             

# legend shows class names    
plot(x)

You can also set the color-table

lev <- levels(r)[[1]]
rgb <- lev[,c("Red", "Green", "Blue", "Opacity")]
coltab(x) <- round(rgb*255)
plot(x)

But I did not consider the case where there is a color table and attributes. To be done.ch

See the cats and rats methods. Or levels which is the same as cats. cats is for categories (ID and label), rats for Raster Attribute Tables (ID and n labels). cats are more limited than rats, but generally easier to deal with. They are used to have rasters that behave like "factors".

With the specific file, the problem is that terra uses GDAL to read it, and the GDAL driver for GRD/GRI is rather incomplete. RATS are read fine, I think, when using e.g. GeoTIFF. I may write a translator to make it easier to extract this from GRD/GRI. But for now, what you can do is something along these lines:

library(raster)
f <- "nlcd_agg.grd"
r <- raster(f)

library(terra)
x <- rast(r)

# get the attributes
lev <- levels(r)[[1]]

Now set the attributes

# this fails because of a bug 
rats(x) <- lev
# work around 
rat <- lev
rats(x) <- lev
str(rats(x))

They are there, but they are ignored. I would use cats/levels.

lev <- lev[, c("ID", "Land.Cover.Class")]
lev[,2] <- as.character(lev[,2])

x <- rast(r)
levels(x) <- lev
is.factor(x)
#[1] TRUE

x
#class       : SpatRaster 
#dimensions  : 101, 121, 1  (nrow, ncol, nlyr)
#resolution  : 3750, 3750  (x, y)
#extent      : 1394535, 1848285, 1722765, 2101515  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
#source      : memory 
#name        : nlcd_2011_landcover_2011_edition_2014_03_31 
#min value   :                                Unclassified 
#max value   :                                             

# legend shows class names    
plot(x)

You can also set the color-table

lev <- levels(r)[[1]]
rgb <- lev[,c("Red", "Green", "Blue", "Opacity")]
coltab(x) <- round(rgb*255)
plot(x)

But I did not consider the case where there is a color table and attributes. To be done.

See the cats and levels methods. They are used to have rasters that behave like "factors". The issues you describe have been fixed in terra 1.1-17 (on its way to CRAN). For now, what you can do is something along these lines:

library(raster)
f <- "nlcd_agg.grd"
r <- raster(f)

library(terra)
x <- rast(r)

# get the attributes
lev <- levels(r)[[1]]  
lev <- lev[, c("ID", "Land.Cover.Class")]
lev[,2] <- as.character(lev[,2])

x <- rast(r)
levels(x) <- lev
is.factor(x)
#[1] TRUE

x
#class       : SpatRaster 
#dimensions  : 101, 121, 1  (nrow, ncol, nlyr)
#resolution  : 3750, 3750  (x, y)
#extent      : 1394535, 1848285, 1722765, 2101515  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
#source      : memory 
#name        : nlcd_2011_landcover_2011_edition_2014_03_31 
#min value   :                                Unclassified 
#max value   :                                             

# legend shows class names    
plot(x)

ch

added 256 characters in body
Source Link
Robert Hijmans
  • 11.1k
  • 27
  • 37

You can also set the color-table

lev <- levels(r)[[1]]
rgb <- lev[,c("Red", "Green", "Blue", "Opacity")]
coltab(x) <- round(rgb*255)
plot(x)

But I did not consider the case where there is a color table and attributes. To be done.

You can also set the color-table

lev <- levels(r)[[1]]
rgb <- lev[,c("Red", "Green", "Blue", "Opacity")]
coltab(x) <- round(rgb*255)
plot(x)

But I did not consider the case where there is a color table and attributes. To be done.

Source Link
Robert Hijmans
  • 11.1k
  • 27
  • 37

See the cats and rats methods. Or levels which is the same as cats. cats is for categories (ID and label), rats for Raster Attribute Tables (ID and n labels). cats are more limited than rats, but generally easier to deal with. They are used to have rasters that behave like "factors".

With the specific file, the problem is that terra uses GDAL to read it, and the GDAL driver for GRD/GRI is rather incomplete. RATS are read fine, I think, when using e.g. GeoTIFF. I may write a translator to make it easier to extract this from GRD/GRI. But for now, what you can do is something along these lines:

library(raster)
f <- "nlcd_agg.grd"
r <- raster(f)

library(terra)
x <- rast(r)

# get the attributes
lev <- levels(r)[[1]]

Now set the attributes

# this fails because of a bug 
rats(x) <- lev
# work around 
rat <- lev
rats(x) <- lev
str(rats(x))

They are there, but they are ignored. I would use cats/levels.

lev <- lev[, c("ID", "Land.Cover.Class")]
lev[,2] <- as.character(lev[,2])

x <- rast(r)
levels(x) <- lev
is.factor(x)
#[1] TRUE

x
#class       : SpatRaster 
#dimensions  : 101, 121, 1  (nrow, ncol, nlyr)
#resolution  : 3750, 3750  (x, y)
#extent      : 1394535, 1848285, 1722765, 2101515  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
#source      : memory 
#name        : nlcd_2011_landcover_2011_edition_2014_03_31 
#min value   :                                Unclassified 
#max value   :                                             

# legend shows class names    
plot(x)

Labels are returned as cell values

x[c(7357, 5047, 7360, 9307)]
#  nlcd_2011_landcover_2011_edition_2014_03_31
#1                          Perennial Snow/Ice
#2                                  Open Water
#3                          Perennial Snow/Ice
#4                                Unclassified

And the categories are stored if you save the raster to a GeoTIFF

z <- writeRaster(x, "test.tif", overwrite=TRUE)
z