TransWikia.com

Saving GeoTIFF from R

Geographic Information Systems Asked by Floni on July 22, 2021

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.

2 Answers

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.

Correct answer by aldo_tapia on July 22, 2021

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)

Answered by obrl_soil on July 22, 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