Versions Compared

Key

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

...

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

.

.

.

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.

.

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"

.

.

.

I.B. For external use in some GIS software (e.g. QGIS; header files are required)

...

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

.

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"

.

.

.

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.

.

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

.

.

In the last step, we remove the raster bricks created to check the raster files contents, as they are not required any longer.

.

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

.

.

.