TransWikia.com

Modifying the TIFF's attribute table value with R

Geographic Information Systems Asked by user173675 on January 28, 2021

I use raster::raster("test.tiff", RAT = T) to read the file with the format of TIFF.
The TIFF is derived from ArcGIS pro generating using Feature to Raster -> raster -> tiff.
ArcGIS TIFF’s attribution table as below:

enter image description here

As you can see, the fields of OID, Value, Count are not allowed to edit in ArcGIS Pro. But I really need to make Value <- Name. Thus, though ArcGIS Pro doesn’t allow I to modify Value field, I could use other platform like Python or R to edit the value , and finally I resave the modified data to the TIFF But I don’t know how to resave the TIFF. And I learned another way as below:

db = read.dbf('test.tif.vat.dbf')
db$Value = factor(db$Name)
write.dbf(db,'test.tif.vat.dbf')

Even though I can save without any warning, but finally , when I open the TIFF in ArcGIS Pro , there’s any change in attribution table .

The reason that I want to modify the value of attribute

enter image description here

Can you tell me what should I do ?

One Answer

This post may help with working with raster attribute tables (RAT) in R. The key is in working with levels. If you have an associated attribute table you can access it using levels(x)[[1]] or for a specific attribute (eg., colors column for plotting) levels(x)[[1]]$color. So, if you want to modify the raster attributes you could coerce it to a data.frame, operate on it, put it back into the correct slot, and then write out the raster. Something along the lines of:

library(raster)
r <- raster(extent(571823, 616763, 4423540, 4453690), resolution=100)
  r[] <- sample(c(1:4), ncell(r), replace = TRUE) 
    r <- ratify(r)

Here we modify the attribute table

( r.df <- levels(r)[[1]] )
  ( r.df <- data.frame(r.df, y=runif(nrow(r.df))) )

Now, put the modified data.frame back into the attribute slot. Please note that first column must be ID with values that correspond with the values in the raster. Then you can write out the raster using raster::writeRaster

levels(r) <- r.df
  levels(r)  

Answered by Jeffrey Evans on January 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP