1

I'm playing around with the stars package (0.4-1) in R, but I can't get how to export a grid object in ASCII format, as the traditional writeRaster(r, filename=file.path(dir.list[j], 'test.asc'), format="ascii") would do.

On one side I tried

write_stars(r, dsn=file.path(dir.list[j], 'test.asc'))

but I get

Error in CPL_write_gdal(mat, file, driver, options, type, dims, from,  : 
  driver not recognized.

On the other hand, if I try to convert it to a RasterLayer using the provided function I get different errors:

r = as(r)
Error in .identC(thisClass, Class) : 
  "Class"  is not specified and doesn't have a default value


r = stars::as(r)
Error: 'as' is not an object from 'namespace:stars'

The help page is not that helpful and I don't find any other info in the web.

4

Can your GDAL write ArcInfo ascii grids?

> rds = st_drivers(what="raster")
> rds["AAIGrid",]
           name           long_name write copy is_raster is_vector  vsi
AAIGrid AAIGrid Arc/Info ASCII Grid FALSE TRUE      TRUE     FALSE TRUE

that shows mine can't write them. The raster package gets round this by implementing its own ASCII Grid writing code, and giving it a non-GDAL name of "ascii".

stars seems to try and replicate this - its undocumented detect.driver function returns "ascii" and then passes this to GDAL which will fail to recognise it:

> stars:::detect.driver("foo.asc")
[1] "ascii"

So this fails because there's no "ascii" driver:

> write_stars(x1, dsn="/tmp/foo1.asc", band=1)
Error in CPL_write_gdal(mat, file, driver, options, type, dims, from,  : 
  driver not recognized.

and this fails because I don't have write capability for AAIGrid:

> write_stars(x1, dsn="/tmp/foo1.asc", band=1, driver="AAIGrid")
Error in CPL_write_gdal(mat, file, driver, options, type, dims, from,  : 
  driver does not support Create() method.

If you do have write capability for that driver then the above should work.

So let's convert to Raster...

r = as(r) isn't telling as what Class you wanted it converted to:

> r = as(x1)
Error in .identC(thisClass, Class) : 
  argument "Class" is missing, with no default

so tell it...

> r = as(x1, "Raster")
> r
class      : RasterBrick 
dimensions : 352, 349, 122848, 6  (nrow, ncol, ncell, nlayers)

Then you can write (one layer of) that to an ASCII Grid using writeRaster.

2
  • ha! now I understand why, thank you very much. BTW, no writing capability here too, so I had to convert back to 'Raster'; I got misled by the help page which looks like asking only for the from parameter.
    – Nico
    May 26 '20 at 8:55
  • This is very usefull as I did not find the way to compress the rasters when using stars::st_rasterize. Now I use r <- as(x, "Raster") and later raster::writeRaster(r,file,options = c("COMPRESS=DEFLATE")) Dec 2 '20 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.