9

I want writeRaster to write the RAT (raster attribute table) that I've built in R.

I'm running R 3.0.1, raster 2.1-49, and rgdal 0.8-10.

My input raster looks like this:

r <-raster("F:/test.img")

class       : RasterLayer 
dimensions  : 3, 3, 9  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : 347325, 347415, 4301655, 4301745  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : F:\test.img 
names       : test 
values      : 1, 19  (min, max)

I then build my attribute table:

r <- ratify(r)
rat <- levels(r)[[1]]
rat$Pixel_Values <- c(1, 7, 8, 9, 19)
rat$Class_Names <- c("A", "B", "C", "D", "E")
levels(r) <- rat

Which results in a raster with attributes:

r

# class       : RasterLayer 
# dimensions  : 3, 3, 9  (nrow, ncol, ncell)
# resolution  : 30, 30  (x, y)
# extent      : 347325, 347415, 4301655, 4301745  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
# data source : F:\test.img 
# names       : test 
# values      : 1, 19  (min, max)
# attributes  :
#  ID Pixel_Values Class_Names
#   1            1           A
#   7            7           B
#   8            8           C
#   9            9           D
#  19           19           E

I then attempt to write my raster together with its RAT:

ratRaster <- "F:/testRat.img"
writeRaster(r, filename=ratRaster, datatype="INT1U", RAT=TRUE, progress="window", overwrite=TRUE)

But when I read it back into R, it becomes apparent that the attributes did not persist:

r2 <- raster(ratRaster)

r2
# class       : RasterLayer 
# dimensions  : 3, 3, 9  (nrow, ncol, ncell)
# resolution  : 30, 30  (x, y)
# extent      : 347325, 347415, 4301655, 4301745  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
# data source : F:\testRat.img 
# names       : testRat 
# values      : 1, 19  (min, max)

It would be quick and awesome to build RATs in R. How can I create and export the raster and keep the RAT?

jbaums
  • 27,115
  • 5
  • 79
  • 119
RichT
  • 304
  • 1
  • 3
  • 13
  • `library(raster); set.seed(1); r <- raster(matrix(sample(c(1, 7:9, 19), 100, replace=TRUE), 10))` will provide a reproducible example raster. – jbaums Mar 12 '15 at 00:28
  • Any reason why you did not accept my answer? It is not automatic when you give the bounty. – cmbarbu Mar 24 '15 at 10:06
  • @cmbarbu - it's not my question... I put a bounty on an existing Q. – jbaums Mar 25 '15 at 09:08

3 Answers3

4

Note that if you use raster's native .grd format (see doc, section 3.3) , the RAT table will be saved:

library(raster)
r <- raster(nrows=5, ncols=5)
r[] <- rep(1:5, 5)
r <- ratify(r)
rat <- levels(r)[[1]]
rat$Pixel_Values <- 1:5
rat$Class_Names <- c("A", "B", "C", "D", "E")
levels(r) <- rat

r
writeRaster(r, filename="raster_rat.grd")

Now re-open:

r2 <- raster("raster_rat.grd")
r2
Matifou
  • 7,968
  • 3
  • 47
  • 52
2

You could always write the RAT as a csv file, then join that data later.

Write your raster as you specified:

writeRaster(r, filename=ratRaster, datatype="INT1U", RAT=TRUE, progress="window", overwrite=TRUE)

Write the attribute data/table/RAT as a .csv file:

write.csv(rat, file="C:\\merp\\rat.csv", row.names = F)

Then you can join this data in another program later. For example, if exporting from R to ArcMap, write the raster to disk, write the attribute data as csv file, then join your RAT to the raster using the Add Join tool in ArcMap.

derelict
  • 3,657
  • 3
  • 24
  • 29
1

Reading the definition of writeRaster writing the RAT is clearly not implemented, at least for the native and the GTif format. Actually, one of the first things done is to remove the RAT. Not too surprising given the comments in ratify() help:

The functions documented here are mainly available such that files with a RAT can be read and processed; currently there is not too much further support.

From and to R you can always use

    save(r,file=ratRaster)

and then

    load(ratRaster)

It keeps everything.

Community
  • 1
  • 1
cmbarbu
  • 4,354
  • 25
  • 45
  • shame about not being able to write a raster with a RAT!! would save a lot of time.... – Sam Apr 14 '16 at 10:48