MyNixOS website logo
Description
Wrappers for 'GDAL' Utilities Executables
R's 'sf' package ships with self-contained 'GDAL' executables, including a bare bones interface to several 'GDAL'-related utility programs collectively known as the 'GDAL utilities'. For each of those utilities, this package provides an R wrapper whose formal arguments closely mirror those of the 'GDAL' command line interface. The utilities operate on data stored in files and typically write their output to other files. Therefore, to process data stored in any of R's more common spatial formats (i.e. those supported by the 'sf' and 'terra' packages), first write them to disk, then process them with the package's wrapper functions before reading the outputted results back into R. GDAL function arguments introduced in GDAL version 3.5.2 or earlier are supported.

GDAL Utility Functions for R

Project Status: Active - The project has reached a stable, usable state and is being actively developed. License CRAN RStudio mirror downloads

The R package sf ships with self-contained GDAL executables, including a bare bones interface to several of the GDAL-related utility programs collectively known as the GDAL utilities (the full set of which are documented here). For each of those utilities, this package provides an R wrapper whose formal arguments closely mirror those of the GDAL command line interface.

The R functions in this package mirror, as closely possible, the utilities to which they provide interfaces. Each function has the same name as its corresponding command-line utility, takes the same arguments and, given the same inputs and arguments, produces identical output. All of the GDAL utilities operate on data stored in files and typically write their output directly to other files. As a result, processing data stored in any of R's more common spatial formats (i.e. those supported by the sp, sf, and raster packages) will require first writing to disk, then processing with the package's wrapper functions before reading back into R.

Installation

To install the development version from GitHub, run:

library(devtools)
install_github("JoshOBrien/gdalUtilities")

Usage

Translating a GDAL command-line call into the equivalent R call is, in most cases, a straightforward excercise. To do so, just follow these basic rules:

  1. Argument names: With very few exceptions, the name of the R argument corresponding to a GDAL utility's command line flag is gotten by removing the - or -- from the flag. So, for instance, in a call to gdalUtilities::gdal_translate(), the command line flags -projwin_srs and --config become the formal arguments projwin_srs and config. The two exceptions are the command-line flags -3d and -if, which are represented in gdalUtilities, by the formal arguments threeD and IF.

  2. Character and numeric arguments: Flags that are followed by one or more character strings or numbers may be specified by passing either a character or numeric vector to the corresponding formal argument. So, for example, to shrink the size of the raster "in.tif" by 50% in each dimension, you could do:

    gdal_translate("in.tif", "out.tif", outsize = c("50%","50%"))
    
  3. Logical arguments: Many flags are logical, specifying options that are turned on when they are present, and not when they are absent. In the equivalent R functions (as in their corresponding GDAL utilities) these options are all off by default, and can be turned on by setting them to TRUE. In gdal_rasterize, for instance, one can 'invert' a rasterization, burning in a new value on all pixels not covered by a vector feature, by adding the flag -i. With the equivalent R function, the same action would be triggered by setting i=TRUE.

  4. Repeatable arguments: Several GDAL utilities take one or a few flags that are potentially 'repeatable'. gdal_translate, for example, allows users to add ground control points to a raster layer, an operation accomplished by prepending each ground control point's coordinates with the flag -gcp. R, however, does not allow repeated arguments, so repeated instances of a flag need instead to be passed in as rows of a matrix. To add four ground control points to a raster using gdal_translate(), for example, you would do something like the following:

    ## Four column matrix supplying: column, row, x-coord, y-coord
    gcps <- matrix(c(0,  100, 174.761, -36.880,  ## lower-left
                     73,   0, 174.769, -36.871,  ## upper-right
                     73, 100, 174.769, -36.880,  ## lower-right
                      0,   0, 174.761, -36.871), ## upper-left
                   ncol = 4, byrow = TRUE)
    in_tif <- "maunga.tif"
    gcp_tif <- "maunga_gcp.tif"
    gdal_translate(in_tif, gcp_tif, gcp = gcps, a_srs = "EPSG:4326")
    
  5. Checking call using dryrun = TRUE: As a quick check that one's call to an R function corresponds to the desired command-line equivalent, one can set dryrun = TRUE.

    gdal_translate("in.tif", "out.tif", outsize = c("50%","50%"), dryrun = TRUE)
    ## [1] "gdal_translate in.tif out.tif -outsize 50% 50%"
    
    gdal_translate(in_tif, gcp_tif, gcp = gcps, a_srs = "EPSG:4326", dryrun=TRUE)
    ## gdal_translate maunga.tif maunga_gcp.tif -a_srs EPSG:4326 \
    ## -gcp 0 100 174.761 -36.88 -gcp 73 0 174.769 -36.871 \
    ## -gcp 73 100 174.769 -36.88 -gcp 0 0 174.761 -36.871
    

Supported GDAL utilities

At present, sf::gdal_utils() (and thus the gdalUtilities package) provides bindings for only a subset of the GDAL utilities. Lists of the supported and unsupported utilities are given below:

GDAL utilities provided by this package

## Supported 'Raster Programs'
gdal_grid
gdal_rasterize
gdal_translate
gdalbuildvrt
gdaldem
gdalinfo
gdalwarp
nearblack

## Supported 'Multidimensional Raster Programs'
gdalmdiminfo
gdalmdimtranslate

## Supported 'Vector Programs'
ogr2ogr

GDAL utilities not provided by this package

## Unsupported 'Raster Programs'
gdal_contour
gdaladdo
gdallocationinfo
gdalmanage
gdaltindex
gdaltransform

## Unsupported 'Vector Programs' 
ogrinfo
ogrlineref
ogrtindex
Metadata

Version

1.2.5

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows