Search code examples
rraster

Save raster to USGS DEM Format in R


Similar to this question:

I would like to know how to do the reverse and save an .img raster image into a USGS DEM format.

Based on GDAL docs, it seems like it would be possible but when I run rgdal::getGDALDriverNames() in R I get the following:

name       long_name                                                            create  copy isRaster
139         USGSDEM                          USGS Optional ASCII DEM (and CDED)  FALSE  TRUE     TRUE

which seems to imply that it won't create these files?

I was hoping to do something like:

library(raster)

# read
img <- raster("Raster_100ft_2022_10_18.img")

# convert to DEM
writeRaster(img, 'test.dem')

But raster doesn't seem to recognize that output format.

Is there some other method to save as USGS DEM files? Thanks


Solution

  • For me it works with terra. If that's proper "USGSDEM" file, that's another question. From gdal reference it should save the file as well: https://gdal.org/drivers/raster/usgsdem.html

    f <- system.file("ex/elev.tif", package="terra")
    r <- terra::rast(f)
    terra::writeRaster(r, filename = "test.dem", filetype = "USGSDEM", overwrite = TRUE)
    
    raster::raster("test.dem")
    #> class      : RasterLayer 
    #> dimensions : 90, 95, 8550  (nrow, ncol, ncell)
    #> resolution : 0.008333333, 0.008333333  (x, y)
    #> extent     : 5.741667, 6.533333, 49.44167, 50.19167  (xmin, xmax, ymin, ymax)
    #> crs        : +proj=longlat +datum=WGS84 +no_defs 
    #> source     : test.dem 
    #> names      : elevation 
    #> values     : 141, 547  (min, max)
    

    Created on 2022-10-20 with reprex v2.0.2