rspatial / raster

R raster package https://rspatial.github.io/raster/reference/raster-package.html
GNU General Public License v3.0
161 stars 53 forks source link

Error in .startGDALwriting to save multiband raster by layer #307

Closed sjevelazco closed 1 year ago

sjevelazco commented 1 year ago

Hi all, package has an error to save multiband rasters (rasterStack or rasterBrick). Do you know how I canfix this problem?

Thanks

Follow some codes to reproduce this error

rhijmans commented 1 year ago

Thank you for reporting this. It works again:

library(raster)
r <- raster(system.file("external/test.grd", package="raster"))
r <- stack(r, r, r)
filenames = file.path(tempdir(), paste0(names(r), ".tif"))
raster::writeRaster(r, filenames, bylayer = T, overwrite = T)
stack(filenames)
#class      : RasterStack 
#dimensions : 115, 80, 9200, 3  (nrow, ncol, ncell, nlayers)
#resolution : 40, 40  (x, y)
#extent     : 178400, 181600, 329400, 334000  (xmin, xmax, ymin, ymax)
#crs        : +proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +datum=WGS84 +units=m +no_defs 
#names      :   test.1,   test.2,   test.3 
#min values : 138.7071, 138.7071, 138.7071 
#max values : 1736.058, 1736.058, 1736.058 

You can also use "terra" (the replacement of "raster")

library(terra)
x <- rast(r)
writeRaster(x, filenames, overwrite = TRUE)
mdsumner commented 1 year ago

is this tif creation vectorized on band? I had no idea 🙏

rhijmans commented 1 year ago

@mdsumner that is right. "raster" can create filenames like "file_1.tif", "file_2.tif" for you if you use bylayer=TRUE. With "terra" there is no such argument; but you can supply a vector of filenames with a length equal to the number of layers.

GomesPW commented 1 year ago

@rhijmans Hi @rhijmans . The error below keeps popping up for me. I use your routine and it still keeps showing up. Can you help me?

Error in .startGDALwriting(x, filename, gdal = options, ...) : formal argument "sources" matched by multiple actual arguments

kadyb commented 1 year ago

This example also doesn't work in the current stable and devel versions (it worked fine before):

library("raster")
r = stack(system.file("external/rlogo.grd", package = "raster"))
r = readAll(r)
tmp = tempfile(fileext = ".tif")
writeRaster(r, tmp)
#> Error in .startGDALwriting(x, filename, gdal = options, ...) : 
#>   no slot of name "file" for this object of class "RasterStack"