0

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
1

You were very close. You just need to set the extent and the CRS; but you should not explicitly change the resolution -- as that action will discard the cell values. The resolution is changed implicitly by changing the extent. So all you need to do is:

crs(af1_area) <- crs(af1_patch)
extent(af1_area) <- extent(af1_patch)
| improve this answer | |
0

I can't reproduce your error, the approach described works for me (the res() values are changed to what I assigned), but note that the "value" field in your last output is gone (same with mine) so that's probably not the way to go.

I use GDAL for a similar process (gdalcopyproj.py)

gdalcopyproj.py source_file dest_file

If you want to stick with R use:

library(raster)
?resample()
| improve this answer | |
  • Yes the gdalcopyproj.py works well, the other R alternatives shouldn't ... – Leosar Mar 11 '16 at 17:31
  • so, answered? If yes please mark it so – unnic Mar 14 '16 at 14:47
  • The options you give for R are not correct, I think that part should be deleted. – Leosar Mar 14 '16 at 16:49
  • I'm pretty sure the provided options were R are correct when adding the correct option to the pro4string. But I nonetheless changed it to a R function that is tailored to your problem and works well. Please provide a reproducible example next time. – unnic Mar 15 '16 at 7:13
  • I'm sorry in this case I can provide a reproducible example, the raster function resample does not copy the metadata and is not a complete answer, I may suggest you split your answer in two, python works perfectly. But I cant copy the metadata using any of R commands you suggested and others I tried, so this part is not an answer. – Leosar Mar 15 '16 at 14:09

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.