Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

writeValues

Write values to a file


Description

Functions for writing blocks (>= 1 row(s)) of values to files. Writing has to start at the first cell of a row (identified with argument start) and the values written must represent 1 or more entire rows. Begin by opening a file with writeStart, then write values to it in chunks. When writing is done close the file with writeStop.

If you want to write all values of a Raster* object at once, you can also use writeRaster which is easier to use but more limited. The functions described here allow writing values to file using chunks of different sizes (e.g. 1 or 10 rows). Function blockSize can be used to suggest a chunk size to use.

Usage

## S4 method for signature 'RasterLayer,character'
writeStart(x, filename, options=NULL, format, prj=FALSE, ...)
## S4 method for signature 'RasterBrick,character'
writeStart(x, filename, options=NULL, format, prj=FALSE, ...)
## S4 method for signature 'RasterLayer,vector'
writeValues(x, v, start, ...)
## S4 method for signature 'RasterBrick,matrix'
writeValues(x, v, start, ...)
## S4 method for signature 'RasterLayer'
writeStop(x)
## S4 method for signature 'RasterBrick'
writeStop(x)

Arguments

x

Raster* object

filename

character. Output file name

options

character, see writeRaster

format

character, see writeRaster

prj

logical. If TRUE, a "prj" file is written

...

additional arguments as for writeRaster

v

vector (RasterLayer) or matrix (RasterBrick) of values

start

Integer. Row number (counting starts at 1) from where to start writing v

Value

RasterLayer or RasterBrick

See Also

Examples

## Not run: 
r <- raster(system.file("external/test.grd", package="raster"))
# write to a new binary file in chunks
s <- raster(r)
# 
tr <- blockSize(r)
tr
s <- writeStart(s, filename='test.grd',  overwrite=TRUE)
for (i in 1:tr$n) {
	v <- getValuesBlock(r, row=tr$row[i], nrows=tr$nrows[i])
	s <- writeValues(s, v, tr$row[i])
}
s <- writeStop(s)

if(require(rgdal)){
s2 <- writeStart(s, filename='test2.tif', format='GTiff', overwrite=TRUE)
# writing last row first
for (i in tr$n:1) {
	v <- getValuesBlock(r, row=tr$row[i], nrows=tr$nrows[i])
	s2 <- writeValues(s2, v, tr$row[i])
}
# row number 5 once more
v <- getValuesBlock(r, row=5, nrows=1)
writeValues(s2, v, 5)
s2 <- writeStop(s2)
}

## write values of a RasterStack to a RasterBrick
s <- stack(system.file("external/rlogo.grd", package="raster"))
# create empty brick
b <- brick(s, values=FALSE)  
b <- writeStart(b, filename="test.grd", format="raster",overwrite=TRUE)
tr <- blockSize(b)
for (i in 1:tr$n) {
	v <- getValuesBlock(s, row=tr$row[i], nrows=tr$nrows[i])
	b <- writeValues(b, v, tr$row[i])
}
b <- writeStop(b)
# note that the above is equivalent to 
# b <- writeRaster(s, filename="test.grd", format="raster",overwrite=TRUE)

## End(Not run)

raster

Geographic Data Analysis and Modeling

v3.4-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Jacob van Etten [ctb], Michael Sumner [ctb], Joe Cheng [ctb], Dan Baston [ctb], Andrew Bevan [ctb], Roger Bivand [ctb], Lorenzo Busetto [ctb], Mort Canty [ctb], Ben Fasoli [ctb], David Forrest [ctb], Aniruddha Ghosh [ctb], Duncan Golicher [ctb], Josh Gray [ctb], Jonathan A. Greenberg [ctb], Paul Hiemstra [ctb], Kassel Hingee [ctb], Institute for Mathematics Applied Geosciences [cph], Charles Karney [ctb], Matteo Mattiuzzi [ctb], Steven Mosher [ctb], Babak Naimi [ctb], Jakub Nowosad [ctb], Edzer Pebesma [ctb], Oscar Perpinan Lamigueiro [ctb], Etienne B. Racine [ctb], Barry Rowlingson [ctb], Ashton Shortridge [ctb], Bill Venables [ctb], Rafael Wueest [ctb]
Initial release
2021-05-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.