rspatial / raster

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

writeRaster() does not follow "filetype= " argument with ".grd" file extension. #288

Closed mmrgeodk closed 2 years ago

mmrgeodk commented 2 years ago

writeRaster() does not follow "filetype= " argument when the filename has a ".grd" file extension.

The command writeRaster(any_raster, filename = "any_raster.grd", filetype = "GS7BG") results in a grd-file in raster-format and not in "GS7BG (Golden Software Surfer Binary)" format. The same for other filetypes with .grd file extensions.

kadyb commented 2 years ago

So you can't load file in Sufer that was created in {terra}? I ask because it seems that the driver works properly when writing.

library("terra") # v. 1.6.17
r = rast(nrows = 5, ncols = 5, vals = 1:25)
writeRaster(r, "test.grd", filetype = "GS7BG")
describe("test.grd")[1]
#> [1] "Driver: GS7BG/Golden Software 7 Binary Grid (.grd)"
mmrgeodk commented 2 years ago

On closer inspection, it seems to be a issue when terra:writeRaster() is used on a raster::raster.

library(raster)
library(terra) # v 1.6.17
r = raster(nrows = 5, ncols = 5, vals = 1:25)
terra::writeRaster(r, "test.grd", filetype = "GS7BG", overwrite = TRUE)
describe("test.grd")[1]
#> [1] "Driver: RRASTER/R Raster"

rt = rast(nrows = 5, ncols = 5, vals = 1:25)
terra::writeRaster(rt, "test2.grd", filetype = "GS7BG", overwrite = TRUE)
describe("test2.grd")[1]
#> [1] "Driver: GS7BG/Golden Software 7 Binary Grid (.grd)"
kadyb commented 2 years ago

terra::writeRaster() requires SpatRaster not Raster* object. raster::writeRaster() supports limited number of formats excluding GS7BG so that's the reason why it doesn't work.

kadyb commented 2 years ago

Sorry, I wrote too quickly and {raster} supports this format too: https://rspatial.github.io/raster/reference/writeFormats.html

rhijmans commented 2 years ago

Thanks, that was a bug in raster::writeRaster. I now get:

library(raster)
r <- raster(nrows = 5, ncols = 5, vals = 1:25)
writeRaster(r, "surf.grd", format = "GS7BG", overwrite = TRUE)
terra::describe("surf.grd")[1]
#[1] "Driver: GS7BG/Golden Software 7 Binary Grid (.grd)"

Note that although you can call terra::writeRaster with a RasterLayer object, what will be used is raster::writeRaster because the dispatch of the generic writeRaster depends on the class of x (the first argument). The package::method notation can make a difference when another package (probably not importing terra or raster) overwrites writeRaster, but that is not the case here.

Also note that while terra::writeRaster has the argument filetype, raster::writeRaster has the equivalent argument format; so you must use format here.

mmrgeodk commented 2 years ago

Thanks for correcting!