Save raster objects to files

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

  • 'writeRaster': Writes an entire Raster* object to a file in a supported format.
  • 'writeValues': Writes chucks (e.g. by row) of a Raster* object to a file in a supported format.

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

  • native R ‘raster’ package format (.grd): It conserves the original file names in the individual band names.
  • netCDF (.nc): Requires the library ncdf4.
  • GeoTiff (.tif): Requires the library rgdal.
  • ENVI .hdr labelled (.envi),
  • ESRI hdr. labelled (.bil)
  • Erdas Imagine Images (.img)
  • ESRI ASCII (.asc)
  • SAGA GIS (.sdat),
  • IDRISI (.rst).

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:

  • A vector with filenames, with as many file names as layers (i.e. names(x))
  • A single filename that will get a unique suffix (i.e. a number between 1 and nlayers(x))



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. It can be beneficial to put 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.



Previous step: Create a RasterBrick with all the rater layers that we want to save



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.


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.



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.



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.


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