The Geoprocessing tool that will extract out a single band from your three band raster is the Make Raster layer tool. You would set the band index parameter to the band you wish to extract.
This is a quick and easy way to extract the band and as its a tool you could embed it into a models workflow. Note its not a permanent layer so you would need to save it off if you wanted to give it to another person.
I've tried out a bit and found that:
- RasterBricks, as mentioned by RobertH's answer, do work and are more user-friendly and easy to use;
- Rgdal methods like
readGDAL
also work, but with more parameters it's a little bit less user-friendly;
So which option should one use?
According to my tests (on my 420GB GeoTiff with dimensions of 18660x21592 and 374 bands) Rgdal is faster. Maybe due to less overhead of a higher level library such as the Raster-package.
Here are my results, using system.time
and replicate
:
With brick:
> modis_ndvi_ts_brick <- brick("../data/pa_br_mod13q1_ndvi_250_2000_2016.tif")
> system.time(replicate(100, modis_ndvi_ts_brick[7000,7000]))
user system elapsed
94.024 5.468 99.562
While with rgdal:
> system.time(replicate(100, readGDAL("../data/pa_br_mod13q1_ndvi_250_2000_2016.tif", offset=c(7000,7000), region.dim=c(1,1))))
#some printed output from readGDAL here
user system elapsed
88.752 5.400 94.213
So as you can see Rgdal is slightly faster. For those whose this does not make a difference, I recommend using RasterBrick, it's simpler. But for those whose, like me, are struggling to create a high performance code in which every millisecond matters: Use Rgdal
Note: I'm sorry for the not exactly reproducible data, but one could try it out with other data and post here if their results differ somehow
Best Answer
It is not clear on weather you want to subset bands upon reading into R or extract a single band from an existing raster stack. Once illustrated, both are quite simple.
We can use the 3 band R logo as an example.
To subset a band from an R raster stack/brick you use a double bracket, like you are indexing a list object.
To read a single band from a multiband raster, on disk, you use the "raster" function with the "band" argument.