Date: Thu, 28 Mar 2024 18:50:34 +0000 (UTC) Message-ID: <2125664227.25.1711651834766@dd8e2a20274d> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_24_33558386.1711651834765" ------=_Part_24_33558386.1711651834765 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Two functions of the 'raster' package can be used to write raster to a f= ile:
Supported formats include (in parenthesis is the file extension for the = format):
The native R raster ('.grd') format preserves the layer names, but raste= r grid binary files are not compressed. The native R raster format consists= of two files: (1) '.gri' binary file, and (2) '.grd' header file. This is = enough to open the files in R. However, to open raster grid files in most G= IS software packages you will need to write an additional hearder file. The= =E2=80=98ENVI=E2=80=99 header format usually works fine. It creates two ad= ditional files, a '.hdr' file and a '.stx' files. The statistics file ('.st= x') is an optional file that describes image statistics for each spectral b= and in a grayscale or multiband image. The file consists of a series of ent= ries, one for each band, that records the minimum pixel value, the maximum = pixel value, the mean, the standard deviation, and two linear contrast stre= tch parameters. To open a saved raster in, for example QGIS, select the =E2= =80=98.gri=E2=80=99 binary file. An example of how to create a header file = in =E2=80=98ENVI=E2=80=99 format is provided below.
Other formats than the native&nb=
sp;raster
package (.grd
) format do not preserve the layer names (but you could always save t=
he layers individually). Currently, the files saved in netCDF format do not=
contain CRS information (=E2=80=9Ccoord. ref.: NA=E2=80=9D). The last 3 fo=
rmats cannot store Multi-band rasters. For further details see help on =E2=
=80=98writeRaster=E2=80=99 and =E2=80=98writeFormat=E2=80=99.
If the 'prj' argument is TRUE, the CRS is written to a =E2=80=98.prj=E2= =80=99 file. This can be useful when writing to a file type that does not s= tore the crs, such as ASCII files.
For multi-layer rasters (RasterStack and RasterBrick objects), individua= l layers can be stored in the same file or in separate files. When the 'byl= ayer' argument is TRUE, layers are saved to separate files. In this case, t= o name the separate files the user provide naming directions using the the = 'suffix' argument. The user can provide:
EXAMPLES
Examples of how to save raster objects to files are presented below. Exa= mples include:
I. Save raster object to R native raster = grid format
I.A. For internal R usage (i.e. no need f= or header files).
I.B. For external use in some GIS softwar= e (e.g. QGIS; header files are required).
II. Save raster objects to formats other = than R raster grid format Example using netCDF format.
The examples are taken from the =E2=80=9CEffects of Cyclone Yasi on Gree= n Cover at Mission Beach=E2=80=9D tutorial. Putting the code snippets in context, by looking at a broader secti= on of the R script, could be benefitial. Boxes with grey background contain code snippets, and boxes with white= background containt code (text) outputs.=
Previous step: Create a RasterBrick with all the rater layers th= at we want to save
.=
#******************************************************=
******************************
# Save Results: Sa=
ving Rasters
#*****************=
*******************************************************************
# Make a Raster Br=
ick with all the Layers we want to save
# ----------------=
---------------------------------------
ls(pattern=3D"\\.rl=
$")
.=
## [1] "SGGC.2010q3.rl" "SGGC.20=
11q3.rl" =20
## [3] "SGGC.StudyArea.2010q3.reprj.rl" "SGGC.StudyArea.2010q3.rl" =20
## [5] "SGGC.StudyArea.2011q3.reprj.rl" "SGGC.StudyArea.2011q3.rl" =20
## [7] "SGGC.StudyArea.Diffq3.reprj.rl" "SGGC.StudyArea.Diffq3.rl" =20
## [9] "SGGC.StudyArea.StdDiffq3.rl" "SPGC.2010q3.rl" =20
## [11] "SPGC.2011q3.rl" "SPGC.StudyArea.2010q3.reprj.rl"
## [13] "SPGC.StudyArea.2010q3.rl" "SPGC.StudyArea.2011q3.reprj.rl"
## [15] "SPGC.StudyArea.2011q3.rl" "SPGC.StudyArea.Diffq3.reprj.rl"
## [17] "SPGC.StudyArea.Diffq3.rl" "SPGC.StudyArea.StdDiffq3.rl"
.
.=
SGC.StudyArea.Res.rb =3D brick(SPGC.S=
tudyArea.2010q3.rl, SPGC.StudyArea.2011q3.rl,=20
SPGC.StudyArea.Diffq3.rl, SPGC.StudyArea.StdDi=
ffq3.rl,=20
SGGC.StudyArea.2010q3.rl, SGGC.StudyArea.2011q=
3.rl,=20
SGGC.StudyArea.Diffq3.rl, SGGC.StudyArea.StdDi=
ffq3.rl)
names(SGC.StudyArea.Res.rb) =3D c("SPGC_StudyArea_2010Winter", "SPGC_StudyArea_2011Winter", "SPGC_StudyArea_Diff", "SPGC_StudyAr=
ea_StdDiff",=20
"SGGC_StudyArea_2010Winter", "SGGC_StudyArea_2011Winter", "SGGC_StudyArea_Diff"=
, "SGGC_StudyArea_S=
tdDiff") =20
SGC.StudyArea.Res.rb
.=
## class : RasterBrick=20
## dimensions : 295, 206, 60770, 8 (nrow, ncol, ncell, nlayers)
## 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_StudyArea_2010Winter, SPGC_StudyArea_2011Winter, SPGC=
_StudyArea_Diff, SPGC_StudyArea_StdDiff, SGGC_StudyArea_2010Winter, SGGC_St=
udyArea_2011Winter, SGGC_StudyArea_Diff, SGGC_StudyArea_StdDiff=20
## min values : 39.215686, 39.215686, =
-7.843137, -3.245601, 39.215686, =
37.254902, -25.490196, -1.840595=20
## max values : 77.254902, 76.862745, =
7.058824, 2.273073, 78.823529, =
77.647059, 10.588235, 2.470940
.
.
.
I. Save raster object to R native raster grid format
.
I.A. For internal R usage (i.e. no need for header files)
The '.hdr', '.stx', and '.nc' files are listed because the script has be= en run more than once.
.
# Save RasterBrick in native 'raster' package format fo=
r 'internal R use'
# ----------------=
-------------------------------------------------------
# It will preserve=
the Layer Names, but raster grid binary files are not compressed.
# Creates two file=
s: (1) '.gri' binary file, and (2) '.grd' header file.
# Save Raster for =
Internal R consumption
writeRaster(SGC.StudyArea.Res.rb, "SGC_StudyArea_Res.grd", format=3D"raster", overwrite=3DTRUE)
list.files(pattern=3D"SGC_StudyArea_Res")
.=
## [1] "SGC_StudyArea_Res.grd" "SGC_StudyArea_Res=
.gri" "SGC_StudyArea_Res.hdr"
## [4] "SGC_StudyArea_Res.nc" "SGC_StudyArea_Res.stx"
.
.
.
I.B. For external use in some GIS software (e.g. QGIS; heade= r files are required)
The first step is similar to the case above. However, notice that in= this case we need to create not only the =E2=80=98.gri=E2=80=99 and =E2=80= =98.grd=E2=80=99 files, but also an R object (returned by the =E2=80=98writ= eRaster=E2=80=99 function).
The '.nc' file is listed because the script has been run more than o= nce.
.
# Save RasterBrick in native 'raster' package format fo=
r 'external' use
# ----------------=
-----------------------------------------------------
# Save Raster to b=
e opened in a GIS software package. It must include an adequate Header File=
s,
# here we use an '=
ENVI' header file
raster.grid =3D writeRaster(SGC.StudyArea.Res.rb, "SGC_StudyArea_Res.grd", format=3D"raster", overw=
rite=3DTRUE=
)
class(raster.grid)
.=
## [1] "RasterBrick"
## attr(,"package")
## [1] "raster"
.
.=
hdr(raster.grid, format=3D"ENVI")
list.files(pattern=3D"SGC_StudyArea_Res")
.=
## [1] "SGC_StudyArea_Res.grd" "SGC_StudyArea_Res=
.gri" "SGC_StudyArea_Res.hdr"
## [4] "SGC_StudyArea_Res.nc" "SGC_StudyArea_Res.stx"
.
.
.
II. Save raster objects to formats other than R raster grid form= at
As an example we use save a file with the raster in netCDF format. To do= this the library =E2=80=98ncdf4=E2=80=99 must be installed.
.
# Save RasterBrick in formats other than the native 'ra=
ster' package format
# ----------------=
---------------------------------------------------------
# Other formats wi=
ll not preserve the Layer Names (but could always save the layers individua=
lly)
# Save raster in '=
netCDF' format (requires the library `ncdf4`)
writeRaster(SGC.StudyArea.Res.rb, "SGC_StudyArea_Res.nc", format=3D"CDF", overwrite=3DTRUE)
.
.
.
Check
As a final step we load the raster brick with our data of interest from = the files save in R native raster grid and netCDF formats. We can see the l= oaded raster bricks have the same dimensions, resolution, and extent. Howev= er, the raster brick created from the file save in R native raster grid for= mat have preserved the layer names, while the counterpart created from the = file save in netCDF format has =E2=80=98generic=E2=80=99 layer names (i.e. = X1, X2,=E2=80=A6..). As mentioned above there is also an issue, at le= ast currently, with the netCDF file not storing CRS information.
.
# Check Contents and Layers Names of the Saved Raster F=
iles & Delete unwanted Files
# ~~~~~~~~~~~~~~~~=
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
check.nc.rb =3D brick("SGC_StudyArea_Res.nc")
check.nc.rb
.=
## class : RasterBrick=20
## dimensions : 295, 206, 60770, 8 (nrow, ncol, ncell, nlayers)
## resolution : 30, 30 (x, y)
## extent : 1484025, 1490205, -1996985, -1988135 (xmin, xmax, ymin, y=
max)
## coord. ref. : NA=20
## data source : C:/Users/uqbblanc/Documents/TERN/04b-DSDP_GitHub/Prep/Land=
scapes_AusCover-RemoteSensing/YasiEffectsonGCatMB/YasiEffectsonGCatMB_Tutor=
ial/SGC_StudyArea_Res.nc=20
## names : X1, X2, X3, X4, X5, X6, X7, X8=20
## unknown : 1, 2, 3, 4, 5, 6, 7, 8=20
## varname : variable
.
.=
check.grd.rb =3D brick("SGC_StudyArea_Res.grd")
check.grd.rb
.=
## class : RasterBrick=20
## dimensions : 295, 206, 60770, 8 (nrow, ncol, ncell, nlayers)
## 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 : C:/Users/uqbblanc/Documents/TERN/04b-DSDP_GitHub/Prep/Land=
scapes_AusCover-RemoteSensing/YasiEffectsonGCatMB/YasiEffectsonGCatMB_Tutor=
ial/SGC_StudyArea_Res.grd=20
## names : SPGC_StudyArea_2010Winter, SPGC_StudyArea_2011Winter, SPGC=
_StudyArea_Diff, SPGC_StudyArea_StdDiff, SGGC_StudyArea_2010Winter, SGGC_St=
udyArea_2011Winter, SGGC_StudyArea_Diff, SGGC_StudyArea_StdDiff=20
## min values : 39.215686, 39.215686, =
-7.843137, -3.245601, 39.215686, =
37.254902, -25.490196, -1.840595=20
## max values : 77.254902, 76.862745, =
7.058824, 2.273073, 78.823529, =
77.647059, 10.588235, 2.470940
.
.
In the last step, we remove the raster bricks created to check the raste= r files contents, as they are not required any longer.
.
rm(list=3Dls(pattern=3D"check"))
.
.
.