Two functions of the 'raster' package can be used to write raster to a file:

Supported formats include (in parenthesis is the file extension for the format):

The native R raster ('.grd') format preserves the layer names, but raster 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 GIS software packages you will need to write an additional hearder file. The ‘ENVI’ header format usually works fine. It creates two additional files, a '.hdr' file and a '.stx' files. The statistics file ('.stx') is an optional file that describes image statistics for each spectral band in a grayscale or multiband image. The file consists of a series of entries, one for each band, that records the minimum pixel value, the maximum pixel value, the mean, the standard deviation, and two linear contrast stretch parameters. To open a saved raster in, for example QGIS, select the ‘.gri’ binary file. An example of how to create a header file in ‘ENVI’ format is provided below.

Other formats than the native raster package (.grd) format do not preserve the layer names (but you could always save the layers individually). Currently, the files saved in netCDF format do not contain CRS information (“coord. ref.: NA”). The last 3 formats cannot store Multi-band rasters. For further details see help on ‘writeRaster’ and ‘writeFormat’.

If the 'prj' argument is TRUE, the CRS is written to a ‘.prj’ file. This can be useful when writing to a file type that does not store the crs, such as ASCII files.

For multi-layer rasters (RasterStack and RasterBrick objects), individual layers can be stored in the same file or in separate files. When the 'bylayer' argument is TRUE, layers are saved to separate files. In this case, to 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. Examples include:

I. Save raster object to R native raster grid format

I.A. For internal R usage (i.e. no need for header files).

I.B. For external use in some GIS software (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 “Effects of Cyclone Yasi on Green Cover at Mission Beach” tutorial. Putting the code snippets in context, by looking at a broader section 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 that we want to save

.

#************************************************************************************
# Save Results: Saving Rasters
#************************************************************************************

# Make a Raster Brick with all the Layers we want to save
# -------------------------------------------------------
ls(pattern="\\.rl$")

.

##  [1] "SGGC.2010q3.rl"                 "SGGC.2011q3.rl"                
##  [3] "SGGC.StudyArea.2010q3.reprj.rl" "SGGC.StudyArea.2010q3.rl"      
##  [5] "SGGC.StudyArea.2011q3.reprj.rl" "SGGC.StudyArea.2011q3.rl"      
##  [7] "SGGC.StudyArea.Diffq3.reprj.rl" "SGGC.StudyArea.Diffq3.rl"      
##  [9] "SGGC.StudyArea.StdDiffq3.rl"    "SPGC.2010q3.rl"                
## [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 = brick(SPGC.StudyArea.2010q3.rl, SPGC.StudyArea.2011q3.rl, 
                             SPGC.StudyArea.Diffq3.rl, SPGC.StudyArea.StdDiffq3.rl, 
                             SGGC.StudyArea.2010q3.rl, SGGC.StudyArea.2011q3.rl, 
                             SGGC.StudyArea.Diffq3.rl, SGGC.StudyArea.StdDiffq3.rl)
names(SGC.StudyArea.Res.rb) = c("SPGC_StudyArea_2010Winter", "SPGC_StudyArea_2011Winter", "SPGC_StudyArea_Diff", "SPGC_StudyArea_StdDiff", 
                                "SGGC_StudyArea_2010Winter", "SGGC_StudyArea_2011Winter", "SGGC_StudyArea_Diff", "SGGC_StudyArea_StdDiff")        
SGC.StudyArea.Res.rb   

.

## class       : RasterBrick 
## dimensions  : 295, 206, 60770, 8  (nrow, ncol, ncell, nlayers)
## resolution  : 30, 30  (x, y)
## extent      : 1484025, 1490205, -1996985, -1988135  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs 
## data source : in memory
## names       : SPGC_StudyArea_2010Winter, SPGC_StudyArea_2011Winter, SPGC_StudyArea_Diff, SPGC_StudyArea_StdDiff, SGGC_StudyArea_2010Winter, SGGC_StudyArea_2011Winter, SGGC_StudyArea_Diff, SGGC_StudyArea_StdDiff 
## min values  :                 39.215686,                 39.215686,           -7.843137,              -3.245601,                 39.215686,                 37.254902,          -25.490196,              -1.840595 
## 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 been run more than once.

.

# Save RasterBrick in native 'raster' package format for 'internal R use'
# -----------------------------------------------------------------------
# It will preserve the Layer Names, but raster grid binary files are not compressed.
# Creates two files: (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="raster", overwrite=TRUE)
list.files(pattern="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; header 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 ‘.gri’ and ‘.grd’ files, but also an R object (returned by the ‘writeRaster’ function).

The '.nc' file is listed because the script has been run more than once.

.

# Save RasterBrick in native 'raster' package format for 'external' use
# ---------------------------------------------------------------------
# Save Raster to be opened in a GIS software package. It must include an adequate Header Files,
# here we use an 'ENVI' header file 
raster.grid = writeRaster(SGC.StudyArea.Res.rb, "SGC_StudyArea_Res.grd", format="raster", overwrite=TRUE)
class(raster.grid)

.

## [1] "RasterBrick"
## attr(,"package")
## [1] "raster"

.

.

hdr(raster.grid, format="ENVI")
list.files(pattern="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 format

As an example we use save a file with the raster in netCDF format. To do this the library ‘ncdf4’ must be installed.

.

# Save RasterBrick in formats other than the native 'raster' package format
# -------------------------------------------------------------------------
# Other formats will not preserve the Layer Names (but could always save the layers individually)

# Save raster in 'netCDF' format (requires the library `ncdf4`)
writeRaster(SGC.StudyArea.Res.rb, "SGC_StudyArea_Res.nc", format="CDF", overwrite=TRUE)

.

.

.

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 loaded raster bricks have the same dimensions, resolution, and extent. However, the raster brick created from the file save in R native raster grid format have preserved the layer names, while the counterpart created from the file save in netCDF format has ‘generic’ layer names (i.e. X1, X2,…..).  As mentioned above there is also an issue, at least currently, with the netCDF file not storing CRS information.

.

# Check Contents and Layers Names of the Saved Raster Files & Delete unwanted Files
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
check.nc.rb = brick("SGC_StudyArea_Res.nc")
check.nc.rb

.

## class       : RasterBrick 
## dimensions  : 295, 206, 60770, 8  (nrow, ncol, ncell, nlayers)
## resolution  : 30, 30  (x, y)
## extent      : 1484025, 1490205, -1996985, -1988135  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
## data source : C:/Users/uqbblanc/Documents/TERN/04b-DSDP_GitHub/Prep/Landscapes_AusCover-RemoteSensing/YasiEffectsonGCatMB/YasiEffectsonGCatMB_Tutorial/SGC_StudyArea_Res.nc 
## names       : X1, X2, X3, X4, X5, X6, X7, X8 
## unknown     : 1, 2, 3, 4, 5, 6, 7, 8 
## varname     : variable

.

.

check.grd.rb = brick("SGC_StudyArea_Res.grd")
check.grd.rb

.

## class       : RasterBrick 
## dimensions  : 295, 206, 60770, 8  (nrow, ncol, ncell, nlayers)
## resolution  : 30, 30  (x, y)
## extent      : 1484025, 1490205, -1996985, -1988135  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs 
## data source : C:/Users/uqbblanc/Documents/TERN/04b-DSDP_GitHub/Prep/Landscapes_AusCover-RemoteSensing/YasiEffectsonGCatMB/YasiEffectsonGCatMB_Tutorial/SGC_StudyArea_Res.grd 
## names       : SPGC_StudyArea_2010Winter, SPGC_StudyArea_2011Winter, SPGC_StudyArea_Diff, SPGC_StudyArea_StdDiff, SGGC_StudyArea_2010Winter, SGGC_StudyArea_2011Winter, SGGC_StudyArea_Diff, SGGC_StudyArea_StdDiff 
## min values  :                 39.215686,                 39.215686,           -7.843137,              -3.245601,                 39.215686,                 37.254902,          -25.490196,              -1.840595 
## 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 raster files contents, as they are not required any longer.

.

rm(list=ls(pattern="check"))

.

.

.