Date: Thu, 28 Mar 2024 18:30:02 +0000 (UTC) Message-ID: <1584908703.35.1711650602180@6451faed5b85> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_34_1962998594.1711650602180" ------=_Part_34_1962998594.1711650602180 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
To subset the extent of a raster to a desired area, we use the 'crop' fu= nction 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 ar= guments:
(1) a Raster* object (or an SpatialPolygons*, or SpatialLines*,= or SpatialPoints* object)
(2) an Extent object or any object from which an Extent obj= ect can be extracted (i.e. RasterLayer, RasterStack, RasterBrick and o= bjects of the Spatial* classes). The extent object will act as a cookie cut= ter.
For an adequate behaviour, the raster and extent need to be in the same = Coordinate Reference System (CRS).
EXAMPLES
Below examples on how to crop raster objects is provided. These examples= were taken from the =E2=80=9CEffects of Cyclone Yasi on Green Cover at Mis= sion Beach=E2=80=9D tutorial. Putting the code snippets in context, by look= ing at a broader section of the R script, could be benefitial. Boxes with grey background contain = span>code snippets, and b= oxes with white background containt code = (text) outputs.
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 Re= gion & Study Area. The raster is in EPSG:3577(=3D=E2=80=98GDA94=E2= =80=99/=E2=80=98Australian Albers=E2=80=99), an Australian-specific CRS (fu= rther details for the EPSG:3577 CRS in this l= ink). The polygon for the Study Area, on the other hand, is in = ;EPSG:4326 CRS, a general world spatial reference (more details for th= is 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 a= rgument in the =E2=80=98crop=E2=80=99 function, as the =E2=80=98crop=E2=80= =99 function would internally extract the extent for this object. However, = for clarity we first create an Extent object from the re-projected Study Ar= ea, and use this Extent object as an argument in the =E2=80=98crop=E2=80=99= function.
RasterLayer object Coordinate Reference System (CRS)
.=
#=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
# Subset Rasters t=
o Study Area
#=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
#-----------------=
-----------------------------------------------------------------
# Create Cookie Cu=
tter for the Study Area
#-----------------=
-----------------------------------------------------------------
# Change Study Are=
a spatial polygon CRS (EPSG:4326) to the Australian-specific CRS used for r=
aster (EPSG:3577).
# Find Raster CRS<=
/span>
SPGC.2010q3.rl
.=
## class : RasterLayer=20
## dimensions : 135159, 141481, 19122430479 (nrow, ncol, ncell)
## resolution : 30, 30 (x, y)
## extent : -1944645, 2299785, -4910195, -855425 (xmin, xmax, ymin, y=
max)
## coord. ref. : +proj=3Daea +lat_1=3D-18 +lat_2=3D-36 +lat_0=3D0 +lon_0=3D=
132 +x_0=3D0 +y_0=3D0 +ellps=3DGRS80 +units=3Dm +no_defs=20
## data source : C:/Users/uqbblanc/Documents/TERN/04b-DSDP_GitHub/Prep/Land=
scapes_AusCover-RemoteSensing/YasiEffectsonGCatMB/YasiEffectsonGCatMB_Tutor=
ial/SPGC_2010q3.vrt=20
## names : SPGC_2010q3=20
## values : 0, 255 (min, max)
.
.=
crs(SPGC.2010q3.rl)
.=
## CRS arguments:
## +proj=3Daea +lat_1=3D-18 +lat_2=3D-36 +lat_0=3D0 +lon_0=3D132 +x_0=3D0 =
+y_0=3D0
## +ellps=3DGRS80 +units=3Dm +no_defs
.
.=
# Double check that this is the EPSG:3577 CRS (=3D to t=
he raster's CRS)
CRS("+init=3Depsg:3=
577")
.=
## CRS arguments:
## +init=3Depsg:3577 +proj=3Daea +lat_1=3D-18 +lat_2=3D-36 +lat_0=3D0
## +lon_0=3D132 +x_0=3D0 +y_0=3D0 +ellps=3DGRS80 +towgs84=3D0,0,0,0,0,0,0
## +units=3Dm +no_defs
.
.
.
Reproject SpatialPolygons object and create an Extent object
.=
# Transform the Spatial Polygon CRS (from EPSG:4326 to =
EPSG:3577)
StudyArea.SP.reprj =3D spTransform(StudyArea.SP, CRS("+init=3Depsg:3577"))
# Study Area Repro=
jected Extent
StudyArea.extent.reprj =3D extent(StudyArea.SP.reprj)=20
StudyArea.extent.reprj
.=
## class : Extent=20
## xmin : 1484019=20
## xmax : 1490198=20
## ymin : -1996975=20
## ymax : -1988144
.
.
.
Crop (Spatially Subset) RasterLayer objects
.
Example 1: SPGC for the Winter of 2010
.=
#------------------------------------------------------=
----------------------------
# Subset (i.e. cro=
p) raster layer to area of interest
#-----------------=
-----------------------------------------------------------------
# Subset both Year=
-Season rasters
SPGC.StudyArea.2010q3.rl =3D crop(SPGC.2010q3.rl, StudyArea.extent.reprj)
SPGC.StudyArea.2010q3.rl
.=
## class : RasterLayer=20
## dimensions : 295, 206, 60770 (nrow, ncol, ncell)
## resolution : 30, 30 (x, y)
## extent : 1484025, 1490205, -1996985, -1988135 (xmin, xmax, ymin, y=
max)
## coord. ref. : +proj=3Daea +lat_1=3D-18 +lat_2=3D-36 +lat_0=3D0 +lon_0=3D=
132 +x_0=3D0 +y_0=3D0 +ellps=3DGRS80 +units=3Dm +no_defs=20
## data source : in memory
## names : SPGC_2010q3=20
## values : 100, 197 (min, max)
.
Example 2: SPGC for the Winter of 2011
.=
SPGC.StudyArea.2011q3.rl =3D crop(SPG=
C.2011q3.rl, StudyArea.extent.reprj)
SPGC.StudyArea.2011q3.rl
.=
## class : RasterLayer=20
## dimensions : 295, 206, 60770 (nrow, ncol, ncell)
## resolution : 30, 30 (x, y)
## extent : 1484025, 1490205, -1996985, -1988135 (xmin, xmax, ymin, y=
max)
## coord. ref. : +proj=3Daea +lat_1=3D-18 +lat_2=3D-36 +lat_0=3D0 +lon_0=3D=
132 +x_0=3D0 +y_0=3D0 +ellps=3DGRS80 +units=3Dm +no_defs=20
## data source : in memory
## names : SPGC_2011q3=20
## values : 100, 196 (min, max)
.
.
.