Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


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

...

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.

...

The examples are taken from the “Effects of Cyclone Yasi on Green Cover at Mission Beach” tutorial. It can be beneficial to put  Putting 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, 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

...

Div
stylebackground-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
#************************************************************************************
# Save Results: Saving Rasters
#************************************************************************************

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


Div
stylebackground-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
##  [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"

...

Div
stylebackground-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
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   


Div
stylebackground-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
## 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

...

Div
stylebackground-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
# 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")


Div
stylebackground-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
## [1] "SGC_StudyArea_Res.grd" "SGC_StudyArea_Res.gri" "SGC_StudyArea_Res.hdr"
## [4] "SGC_StudyArea_Res.nc"  "SGC_StudyArea_Res.stx"

...

Div
stylebackground-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
# 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)


Div
stylebackground-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
## [1] "RasterBrick"
## attr(,"package")
## [1] "raster"

...

Div
stylebackground-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
hdr(raster.grid, format="ENVI")
list.files(pattern="SGC_StudyArea_Res")


Div
stylebackground-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
## [1] "SGC_StudyArea_Res.grd" "SGC_StudyArea_Res.gri" "SGC_StudyArea_Res.hdr"
## [4] "SGC_StudyArea_Res.nc"  "SGC_StudyArea_Res.stx"

...

Div
stylebackground-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
# 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.

...

Div
stylebackground-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
# Check Contents and Layers Names of the Saved Raster Files & Delete unwanted Files
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
check.nc.rb = brick("SGC_StudyArea_Res.nc")
check.nc.rb


Div
stylebackground-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
## 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

...

Div
stylebackground-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
check.grd.rb = brick("SGC_StudyArea_Res.grd")
check.grd.rb


Div
stylebackground-color: white; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
## 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

...

code
Div
stylebackground-color: #F8F9F9; border: 1px solid #666; font-size: 12px; padding: 0.5rem 0.5rem;
rm(list=ls(pattern="check"))
rm(list=ls(pattern="check"))