I have two geotiff raster files, one has all the metadata information and in the other the information was lost. I know that all the metadata information was exactly the same so I want to copy it from one file to the other. I tried to use raster, because I made all the processing in R.
This is the file with metadata
af1_patch <-raster(a_files[6])
af1_patch
class : RasterLayer
dimensions : 38400, 38400, 1474560000 (nrow, ncol, ncell)
resolution : 231.656, 231.656 (x, y)
extent : -2223901, 6671703, -4447802, 4447802 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs
data source : Forest_patches_AF_1.tif
names : Forest_patches_AF_1
values : 0, 255 (min, max)
And this is the file without the metadata
af1_area <-raster(a_files[1])
af1_area
class : RasterLayer
dimensions : 38400, 38400, 1474560000 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 0, 38400, 0, 38400 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : africa_AF_1.tif
names : africa_AF_1
values : 0, 255 (min, max)
I tried to copy the metadata using:
res(af1_area) <- res(af1_patch)
crs(af1_area) <- crs(af1_patch)
extent(af1_area) <- extent(af1_patch)
but it doesn't work, dimensions and resolution are incorrect and the data values are lost:
af1_area
class : RasterLayer
dimensions : 166, 166, 27556 (nrow, ncol, ncell)
resolution : 53588, 53588 (x, y)
extent : -2223901, 6671703, -4447802, 4447802 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs
hist(af1_area)
Error in .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...) :
cannot make a histogram; need data on disk or in memory
Thanks!