10

I have a raster file and I would like to save it to be able to open it with a attribute table in ArcMap.

When I use the command writeRaster(raster, "test_output11", format = "GTiff"), I don´t have the aux files such as .tfw, .xml, .ovr, .cpg, .dbf.

Should I produce all the files manually by changing format = "GTiff"? I can ´t find a general command for it.

1

2 Answers 2

14

Those files are generated in ArcGIS. Are auxiliary files for georeferencing and visualization, aren't part of GTiff as .shx or .dbf are in a shapefile.

  • .tfw is ESRI World file
  • .ovr are piramid layers
  • .xml is schema look and histogram
  • .cpg is for TIFF interpretation.
  • .dbf is for raster attribute table (thanks to @radouxju)

You can add .tfw using GDAL through raster package:

library(raster)

r <- raster()
values(r) <- sample(x = 1:10,size = ncell(r),replace = T)

writeRaster(r,'test.tif',options=c('TFW=YES'))

Check other options from GDAL GeoTIFF File Format to customize your export.

2
  • 1
    I would rather say the dbf is for raster attribute table.
    – radouxju
    Commented Oct 2, 2017 at 13:41
  • @radouxju yes. Edited
    – aldo_tapia
    Commented Oct 2, 2017 at 13:43
3

You need to ratify your raster and give it some attributes, then write to GTiff format. Both a .tif and a .tif.aux.xml file will be written out, and ArcGIS can use the latter to derive symbology (QGIS sadly doesn't...). Example:

library(raster)

set.seed(1)
r <- raster(matrix(sample(c(1, 7:9, 19), 100, replace=TRUE), 10))

r <- ratify(r)
levels(r)[[1]]$NAME <- letters[1:nrow(levels(r)[[1]])]
# check results with levels(r)[[1]]

writeRaster(r, 'C:/DATA/r.tif')

Note that there's not much point doing this with continuous data, such surfaces should be reclassified first.

If you want a .dbf RAT that Arc will pick up automatically, this will work:

# datatype must be integer and raster must be single band
writeRaster(r, 'C:/DATA/r_2.tif', datatype = 'INT2S')

library(tidyverse)

as.data.frame(table(as.vector(r)), 
                       stringsAsFactors = FALSE) %>%
  rename(VALUE = Var1, COUNT = Freq) %>%
  # you can add some more cols here, 
  # just keep nchar() < 254 and colnames < 8 chars:
  mutate(VALUE = as.integer(VALUE)) %>% 
  foreign::write.dbf(., 'C:/DATA/r_2.tif.vat.dbf', 
            factor2char = TRUE, max_nchar = 254)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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