I fitted a species distribution model and predicted the suitable habitat using this model, and then I got a map plotted in R.

I tried to save this prediction as a raster, using code:

rf<-writeRaster(px, filename="predict.tif", format="GTiff")

However, I could only export a tif format file without an attribute table. I could put this raster to ArcGIS software, but without an attribute table, it is difficult to run any analysis.

How can I export a raster with it's attribute table in R?

  • 1
    What do you mean by an attribute table? What are the values in your raster? The ratify function and things like as.factor might be what you want if you have a categorical raster and want to save labels for each class. – Spacedman Apr 14 '19 at 14:23
  • I put the raster (.tif file) in ArcGIS, but there is no attribute table of that file, which make it difficult to do any analysis. The attribute table I expect should contain 1, 0 or 0~1 values of each cell. – Lili Li Apr 17 '19 at 6:42
  • A raster with a single "attribute" will not have an attribute table because these are the values that encoded in the raster contains. You only really need an attribute table if you then have attributes associated with the raster values. In ArcGIS, if the raster is integer and not float, you can build an attribute table but, by default there will not be one. There is a tool in "Data Management toolbox > Raster toolset > Raster Properties toolset" to do this. – Jeffrey Evans Apr 19 '19 at 14:33

If you're writing your RasterLayer to GeoTiff format, the following steps should work:

  1. Ratify your RasterLayer. I assume you'll be wanting some kind of count data associated with each value in your layer, so when you use the ratify function, be sure to set count = TRUE.

    Ex: px <- ratify(px, count = TRUE)

  2. Install package foreign. This package will allow you to read and write .dbf files.

  3. Write the RAT from your RasterLayer object to a .tif.vat.dbf file using the write.dbf function. If px is a single-layer raster (not a RasterBrick), then the RAT you created with the ratify function is the first list element of levels(px), i.e. levels(px)[[1]].

    Ex: write.dbf(levels(px)[[1]], file = "predict.tif.vat.dbf")

*Note: make sure the .dbf you create is in the same directory as the GeoTiff you wrote!

Side note: each element of a RAT is simply a data.frame, thus you can add columns to it just as you would any standalone data.frame before writing it to a .dbf file.

The tif.vat.dbf is the database file format used for GeoTiff rasters for storing attribute table data. I hope this helps!

| improve this answer | |

I finally used rf<-writeRaster(px, filename="predict.tif", datatype="INT1U", format="GTiff", overwrite=TRUE) to export a integer type tiff file. And then I imported it in ArcGIS, shut down the background processing, and then I can create an attribute table for this raster layer using Arctool :)

| improve this answer | |

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.