I want to create a subset of an image with four bands. Therefore I am using the crop function in R.
A<-raster("L8_stacked.tif")
subset<-extent(c(639451, 660104, 5469254, 5489566))
B<-crop(A,subset)
As a result I get a raster with only one band in the .tif file. Do I have to define other options to get a subset image with 4 bands?
Raster bands. A raster dataset contains one or more layers called bands. For example, a color image has three bands (red, green, and blue) while a digital elevation model (DEM) has one band (holding elevation values), and a multispectral image may have many bands.
Basically, a band is represented by a single matrix of cell values, and a raster with multiple bands contains multiple spatially coincident matrices of cell values representing the same spatial area. An example of a single-band raster dataset is a digital elevation model (DEM).
We can use the crop() function to crop a raster to the extent of another spatial object. To do this, we need to specify the raster to be cropped and the spatial object that will be used to crop the raster. R will use the extent of the spatial object as the cropping boundary.
A RasterStack is a collection of RasterLayer objects with the same spatial extent and resolution. A RasterStack can be created from RasterLayer objects, or from raster files, or both. It can also be created from a SpatialPixelsDataFrame or a SpatialGridDataFrame object.
As the others already pointed out in the comments, the raster()
function returns a (single) RasterLayer object. If you want a multilayer raster object you need to use the stack()
or brick()
function load the image into R. I.e.:
A <- stack("L8_stacked.tif")
If you then apply your extent with the crop()
function, the result should be a raster stack containing all the bands from the original image.
To learn more on the raster package, read this document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With