R – Import Multi-Band TIFF

geotiff-tiffrrasterstack

I am trying to import a TIFF file that contains 2 bands. When using the following code in R, it seems only the first band is being recognised.

S1<-"my/path/"
S1<-list.files(S1, full.names = TRUE, pattern="tif$")
S1<-lapply(1:length(S1), function (x) {raster(S1[x])})

S1 being at this point:

> S1
[[1]]
class       : RasterLayer 
band        : 1  (of  2  bands)
dimensions  : 3865, 6899, 26664635  (nrow, ncol, ncell)
resolution  : 14.83, 14.83  (x, y)
extent      : 361363.5, 463675.7, 5760647, 5817965  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : /shared/Training/EARSEL0918_UrbanClassification_Germany/Processing/Coherence__20180412_20180424_TC.tif 
names       : Coherence__20180412_20180424_TC 

My objective is to create a raster stack containing both layers (for further processing in R). If I run

S1<-stack(S1)

> S1
class       : RasterStack 
dimensions  : 3865, 6899, 26664635, 1  (nrow, ncol, ncell, nlayers)
resolution  : 14.83, 14.83  (x, y)
extent      : 361363.5, 463675.7, 5760647, 5817965  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : Coherence__20180412_20180424_TC 

Best Answer

If you read a TIFF with raster like you have in your loop you'll only get one layer read:

> r = raster("./GcrfPicture.tif")
> r
class       : RasterLayer 
band        : 1  (of  4  bands)
dimensions  : 720, 960, 691200  (nrow, ncol, ncell)
resolution  : 1, 1  (x, y)

The 1 (of 4 bands) is telling you that the source had four bands but its only read one.

Use stack instead:

> s = stack("./GcrfPicture.tif")
> s
class       : RasterStack 
dimensions  : 720, 960, 691200, 4  (nrow, ncol, ncell, nlayers)
Related Question