writeValues
for writing in chunks (e.g. by row).
When writing a file to disk, the file format is determined by the 'format=' argument if supplied, or else by the file extension (if the extension is known). If other cases the default format is used. The 'factory-fresh' default format is 'raster', but this can be changed using setOptions
.writeRaster(x, filename, ...)
x
is a RasterLayer object
writeRaster(x, filename='', ...)
x
RasterLayer object
filename
Output filename
...
Additional arguments. See below
}
Full function call when x
is a RasterBrick or RasterStack object
writeRaster(x, filename='', bandorder='BIL', ...)
x
RasterLayer object
filename
Output filename
bandorder
Character. 'BIL', 'BIP', or 'BSQ'. For 'native' file formats only. For some other formats you can use the 'options' arugment (see below)
..
Additional arguments. See below
}
Additional arguments
options
Character. File format specific GDAL options. E.g., when writing a geotiff file: options=c("COMPRESS=NONE", "TFW=YES")
format
Character. Output file type. See writeFormats
datatype
Character. Output data type. See dataType
overwrite
Logical. If TRUE
, "filename" will be overwritten if it exists
NAflag
Numeric. To overwrite the default value used to represent NA
in a file
}
NetCDF files have the following additional, optional, arguments: varname, varunit, longname, xname, yname, zname, zunit
.writeFormats
for supported file types ("formats", "drivers").
The rgdal package is needed, except for these file formats: 'raster', 'BIL', 'BIP', 'BSQ', 'SAGA', 'ascii', 'IDRISI', and 'CDF'. Some of these formats can be used with or without rgdal (idrisi, SAGA, ascii). You need the 'ncdf' library for the 'CDF' format.
In multi-band files (i.e. files saved from RasterStack or RasterBrick objects), in the native 'raster' format, the bandorder can be set to BIL ('Bands Interleaved by Line'), BIP ('Bands Interleaved by Pixels') or BSQ ('Bands SeQuential'). Note that bandorder is not the same as filetype here.
Supported file types include:
raster
'Native' raster package format .grd Yes
ascii
ESRI Ascii .asc No
SAGA
SAGA GIS .sdat No
IDRISI
IDRISI .rst No
CDF
netCDF (requires ncdf) .nc Yes
GTiff
GeoTiff (requires rgdal) .tif Yes
ENVI
ENVI .hdr Labelled .envi Yes
EHdr
ESRI .hdr Labelled .bil Yes
HFA
Erdas Imagine Images (.img) .img Yes
}writeFormats
, and for faster writing: writeValues
r <- raster(system.file("external/test.grd", package="raster"))
# take a small part
r <- crop(r, extent(179880, 180800, 329880, 330840) )
# write to an integer binary file
rf <- writeRaster(r, filename="allint.grd", datatype='INT4S', overwrite=TRUE)
# make a brick and save multi-band file
b <- brick(r, sqrt(r))
bf <- writeRaster(b, filename="mutli.grd", bandorder='BIL', overwrite=TRUE)
# write to a new geotiff file (depends on rgdal)
if (require(rgdal)) {
rf <- writeRaster(r, filename="test.tif", format="GTiff", overwrite=TRUE)
bf <- writeRaster(b, filename="mutli.tif", options="INTERLEAVE=BAND", overwrite=TRUE)
}
# write to netcdf
if (require(ncdf)) {
rnc <- writeRaster(r, filename='netCDF.nc', format="CDF", overwrite=TRUE)
}
Run the code above in your browser using DataLab