To subset the extent of a raster to a desired area, we use the 'crop' function in the 'raster' package. This is the most commonly used function to modify the extent of a raster object. The raster function takes two main arguments:
(1) a Raster* object (or an SpatialPolygons*, or SpatialLines*, or SpatialPoints* object)
(2) an Extent object or any object from which an Extent object can be extracted (i.e. RasterLayer, RasterStack, RasterBrick and objects of the Spatial* classes). The extent object will act as a cookie cutter.
For an adequate behaviour, the raster and extent need to be in the same Coordinate Reference System (CRS).
Below examples on how to crop raster objects is provided. These examples were taken from the “Effects of Cyclone Yasi on Green Cover at Mission Beach” tutorial. It can be beneficial to put the code snippets in context by looking at a broader section of the R script. Code snippets have a grey background, and outputs have a white background.
In the example, a SpatialPolygons object for the area of interest (i.e. the Study Area) had been previously created to prepare a map showing the Region & Study Area. The raster is in EPSG:3577(=‘GDA94’/‘Australian Albers’), an Australian-specific CRS (further details for the EPSG:3577 CRS in this link). The polygon for the Study Area, on the other hand, is in EPSG:4326 CRS, a general world spatial reference (more details for this CRS in this link. Therefore, we need to transform the CRS for the Study Area spatial polygon form EPSG:4326 to EPSG:3577. We could then directly use this re-projected polygon as an argument in the ‘crop’ function, as the ‘crop’ function would internally extract the extent for this object. However, for clarity we first create an Extent object from the re-projected Study Area, and use this Extent object as an argument in the ‘crop’ function.
RasterLayer object Coordinate Reference System (CRS)
Reproject SpatialPolygons object and create an Extent object
Crop (Spatially Subset) RasterLayer objects
Example 1: SPGC for the Winter of 2010
Example 2: SPGC for the Winter of 2011