2

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 
  • Use brick() not raster stack(lapply(S1, brick)) - but do check the length of S1, make sure brick() gives two layers (it will "1 (of 2 bands)") but I'm seeing that S1 is length 1 here. – mdsumner Sep 6 '18 at 10:23
  • Do you mean a multi- band raster? – Vince Sep 6 '18 at 10:50
  • @Vince, yes exactly – GCGM Sep 6 '18 at 12:16
  • @mdsumner, I have tried with brick but the same was happening – GCGM Sep 6 '18 at 12:16
  • Can you confirm that brick("/shared/Training/EARSEL0918_UrbanClassification_Germany/Processing/Coherence__20180412_20180424_TC.tif") has one or two layers? – mdsumner Sep 7 '18 at 1:57
4

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)
  • Did not know stack could be use to import files in such way. It works, thank you – GCGM Sep 6 '18 at 12:16
  • @GCGM, stack has iteration build in so, you can just pass it a vector of file names. If the rasters do not align, you can use quick=TRUE but, this is of limited utility for any type of analysis. – Jeffrey Evans Sep 6 '18 at 15:34
  • fwiw, stack is the same as brick in the multi-layer/band/channel distinction, just lazier and able to deal with more disparate inputs - it's not easy to characterize the difference and it does depend both on the formats used and the way you use them. – mdsumner Sep 7 '18 at 2:03
  • @JeffreyEvans you mean if the rasters do align ? – mdsumner Sep 7 '18 at 2:04
  • @mdsumner nope, you can create a stack with non-aligned rasters using this argument to bypass the check. In this way you can store rasters in a stack, rather than a list. However, if you try to use any raster functions (eg., calc, overlay) on the stack, as opposed to a single raster in the stack, you will receive an error. – Jeffrey Evans Sep 7 '18 at 2:46

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.