1

I have a land classification raster with factor levels set (pixel val of 1 = 'type a', etc), plus a data frame holding the same factor in the first column, and then several columns of numeric attribute data.

I'd like to generate a new rasterBrick/rasterStack where that numeric data is mapped out - one map for each attribute. Not sure how to do this properly in R. I've tried

test <- merge(raster@data@attributes[[1]], data.frame, by = 'category')

but that gives me a single-band rasterLayer with multiple attributes (i.e. a RAT with more columns), which I can't then write to GTiff properly.

4
  • GTiff doesn't support RAT (does it?). Fwiw Kea was created for this reason, but you'll have to set up GDAL for it
    – mdsumner
    Nov 14 '16 at 11:44
  • This sounds like you want to reclassify the raster according to a table (vector) indexed by the factor levels. That's native to R: see the help for [.
    – whuber
    Nov 14 '16 at 15:30
  • Any chance to have a small reproducible example of your issue?
    – Matifou
    Nov 14 '16 at 22:58
  • Thanks for the comments. Unfortunately KEA isn't an option on this project; it requires outputs in a more commonly used format. [ appears to be recommended only for small datasets, and mine certainly aren't! That also makes providing examples a bit problematic, but I'll see what I can do. I've had some success with deratify().
    – obrl_soil
    Nov 15 '16 at 2:00
0

Turns out deratify() was the function I needed, just had to tweak the RAT a bit to make it all work.

After the merge, the RAT columns had to be rearranged so that ID was column 1 again. deratify() can then be run to produce a RasterBrick, each layer of which contains the data from one of the RAT columns (one can also specify a subset of those columns if necessary). The brick can then be written to a multiband TIF. Just a shame that band names can't be set, otherwise it works quite well.

I'd still be interested in seeing other solutions to reclassifying/recoding rasters based on an index. reclassify() with a two-column matrix produced nonsense results for me, and subs() just ate all my RAM and then crashed R.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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