
Last chance! 50% off unlimited learning
Sale ends in
blockSize
can be used to suggest chunk size.writeStart(x, filename, ...)
writeValues(x, v, ...)
writeStop(x)
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
}writeStart
, then write values to it in chunks. When writing is done close the file with writeStop
. blockSize
can help set the number of rows of each chunk and it returns the row-numbers from where to start reading.
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
.
Files with a 'native' format must be written in sequence of cell numbers. E.g. when writing by row, you must write all rows, and you must write them in sequence. Start at row=1, and end at row=nrow(raster).
In contrast, the values of files written via rgdal can be written in any order (but you must write 1 or more entire rows, you cannot write part of a row). For files written via rgdal, an argument start
must be provided to writeValues
so that it knows where to write the values. This argument is ignored for the native formats (values are assumed to be in sequence).
You cannot overwrite an isolated single row in an existing raster file.writeRaster
, blockSize
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$size)
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$size)
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$size)
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)
Run the code above in your browser using DataLab