I downloaded a map in TIF format that came with the following files: a2_LOt_12.tif.vat.cpg; a2_LOt_12_v170321.tfw; a2_LOt_12_v170321.tif; a2_LOt_12_v170321.tif.aux; a2_LOt_12_v170321.tif.ovr
a2_LOt_12_v170321.tif.vat.dbf; a2_LOt_12_v170321.tif (XML doc).
In ArcGIS the file opens fine and I can access the attribute table:
However, I am having a tough time opening that table in R. I expected "levels()" to work, but it returns "NULL":
r <- raster("a2_LOt_12_v170321.tif")
levels(r)
Is there another way to access that attribute table in R?
Best Answer
Per the raster documentation, adding
'RAT = TRUE'
when reading in the file should solve your problem, so long as the .tif.aux.xml file holds category data. Demo:The tif.aux.xml generated by writeRaster above looks like:
so if your file looks similar you should be fine.
EDIT:
If the attribute data is in a dbf, try something like this:
Have a look at
levels(data_tif)[[1]]
and compare withdata_dbf
. Make sure number of rows matches, and look for columns in common (ID in the RAT may match VALUE in the dbf, for instance). You can then usebase::merge
ordplyr::left_join
to append the dbf data to the RAT.Note that write support for RATs is minimal - some advice is here: https://stackoverflow.com/questions/23840178/how-to-write-a-raster-with-rat-factors-in-r-raster-package
Thanks to this question for providing some data I could test with.