How to write a raster with RAT coefficients in an R-raster package

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

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

My input raster view 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) 

Then I create an 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 

The result is 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 

Then I try to write my raster file along with my RAT:

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

But when I read it back to R, it becomes obvious that the attributes were not saved:

 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 amazing to create a RAT in R. How can I create and export a raster file and save the RAT?

+6
source share
3 answers

You can always write RAT as a csv file, and then join this data later.

Name your raster file as you indicated:

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

Enter the attribute data / table / RAT in the CSV file:

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

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

+2
source

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

The functions described here are available so that files with RAT can be read and processed; currently support is not too much.

From and to R you can always use

  save(r,file=ratRaster) 

and then

  load(ratRaster) 

He stores everything.

+1
source

Note that if you use the native .grd raster 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 open again:

 r2 <- raster("raster_rat.grd") r2 
+1
source

Source: https://habr.com/ru/post/969786/


All Articles