4

Does anyone know how I can extract the raster field names (VALUE, COUNT and other existing field names on a categorical raster file) in R?

I am using the raster package to load the raster files.

2 Answers 2

6

You should first create a stack of layer(s) from your raster object.

ra <- system.file("external/test.grd")
s <- stack(ra)

#how many layers in the stack
nlayers(s)

#info about the layer stack data
print(s)

More details here:

3

The short answer is: it depends.

I don't know too much about how the various raster types are put together, but I do know categorical GeoTiff attribute data are stored in a separate database (.dbf) file. This file is stored in the same directory as the .tif file, and the filename should look something like this: 'your_raster.tif.vat.dbf'. This file can be read to a data.frame object using the 'read.dbf' function from package 'foreign'. Example code below:

#load package
library(foreign)
#read database file
read.dbf('your_raster.tif.vat.dbf')

For a good tutorial outlining the structure of raster attribute data, here's a link to a really helpful ESRI instructional page.

After the attribute data are read to a data.frame, they can be added to a raster object using the 'ratify' function in package 'raster'. This function allows for categorical raster objects by generating a raster attribute table (RAT) to which categorical data can be written. For more information, see the factors{raster} document in R Documentation. Example code below:

#load package 
library(raster)
#import GeoTIFF to raster object
raster<-raster('myraster.tif')
#use ratify function to create new factored layer with raster attribute table
raster_factor<-ratify(raster,filename='raster_factor.tif')

*Note: I almost always use the filename argument where possible because I am working with large rasters, and package raster creates a temporary reference file for raster datafiles too large to be written to system memory. This temporary reference file is erased at the end of each R session, so it's generally a good idea to use the filename argument to save your object to a permanent file.

The resulting object will have a RAT that can be accessed via the 'levels' function. Example code below:

levels(raster_factor)

The 'levels' of the factored raster object is actually a list comprised of one data.frame object. This data.frame can have columns appended to it from the imported .dbf file. These data can then be extracted from the raster object by using the extract function and by setting the factors and df arguments to TRUE. See example code below:

#Extract categorical data from factored raster at specified coordinates
factorExtract<-extract(raster_factor,Extract_points,method='simple',factors=T,df=T)

I hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.