Search code examples
rrasterspatialtiffgeotiff

.tiff file doesn't match original RasterLayer in R


I create a RasterLayer that had the following attributes.

> xx
class      : RasterLayer 
dimensions : 450, 3245, 1460250  (nrow, ncol, ncell)
resolution : 0.1109399, 0.4  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : memory
names      : layer 
values     : -0.003137004, 0.003499978  (min, max)

I saved it as a .tiff file using the following code:

  i = 20200105
  filename = paste(i,".tiff",sep="")
  tiff(file = filename)
  plot(plot_tif)
  dev.off()

I then tried to import it into another piece of code using the following code:

filename <-'20200105.tiff' 
tif <- raster(filename)

And this is what it looks like:

class      : RasterLayer 
band       : 1  (of  4  bands)
dimensions : 480, 480, 230400  (nrow, ncol, ncell)
resolution : 1, 1  (x, y)
extent     : 0, 480, 0, 480  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : /2_plot_nc/20200105.tiff 
names      : X20200105 
values     : 0, 255  (min, max)

i.e. They don't match at all and it's totally wrong. Can someone tell me where I messed up?


Solution

  • Have you tried the writeRaster function instead? Also, try with the ".tif" extension (you make need to use the rgdal library, too, I'm not sure).

    i <- 20200105
    filename <- paste(i,".tif",sep="")
    
    writeRaster(x = xx, 
                filename = filename)