Skip to contents

translate() is a wrapper of the gdal_translate command-line utility (see https://gdal.org/programs/gdal_translate.html). The function can be used to convert raster data between different formats, potentially performing some operations like subsetting, resampling, and rescaling pixels in the process. Refer to the GDAL documentation at the URL above for a list of command-line arguments that can be passed in cl_arg.

Usage

translate(src_filename, dst_filename, cl_arg = NULL, quiet = FALSE)

Arguments

src_filename

Character string. Filename of the source raster.

dst_filename

Character string. Filename of the output raster.

cl_arg

Optional character vector of command-line arguments for gdal_translate (see URL above).

quiet

Logical scalar. If TRUE, a progress bar will not be displayed. Defaults to FALSE.

Value

Logical indicating success (invisible TRUE). An error is raised if the operation fails.

Examples

# convert the elevation raster to Erdas Imagine format and resample to 90m
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster")

# command-line arguments for gdal_translate
args <- c("-tr", "90", "90", "-r", "average")
args <- c(args, "-of", "HFA", "-co", "COMPRESSED=YES")

img_file <- paste0(tempdir(), "/", "storml_elev_90m.img")
translate(elev_file, img_file, args)
#> 0...10...20...30...40...50...60...70...80...90...100 - done.

ds <- new(GDALRaster, img_file)
ds$getDriverLongName()
#> [1] "Erdas Imagine Images (.img)"
ds$bbox()
#> [1]  323476.1 5101842.0  327796.1 5105082.0
ds$res()
#> [1] 90 90
ds$getStatistics(band=1, approx_ok=FALSE, force=TRUE)
#> 0...10...20...30...40...50...60...70...80...90...100 - done.
#> [1] 2438.0000 3040.0000 2674.1649  133.0809
ds$close()

deleteDataset(img_file)
#> [1] TRUE