Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R clip raster with multiple bands

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?

like image 367
ropsi Avatar asked Dec 02 '13 11:12

ropsi


People also ask

How many bands can a raster image have?

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.

What is a multi band raster?

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).

How do you crop a raster in the extent of another raster in R?

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.

What is a raster stack?

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.


1 Answers

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.

like image 96
maRtin Avatar answered Oct 05 '22 19:10

maRtin