Learn R Programming

gdalBindings (version 0.1.17)

formulaCalculate: Calculate raster values based on a formula

Description

Calculate raster values based on a formula

Usage

formulaCalculate(formula, data, updateBand)

Value

Nothing, it just updates the band of interest.

Arguments

formula

Formula. A formula to apply to the RasterBands from data

data

List. A named list with the used variables in the textual formula

updateBand

GDALRasterBand. The GDALRasterBand which will be updated with the calculated values.

Examples

Run this code
# Parameters
raster_path <- file.path(tempdir(), "output.tif")
ul_lat <- -15
ul_lon <- -45
lr_lat <- -25
lr_lon <- -35
res <- c(0.01, -0.01)
datatype <- GDALDataType$GDT_Int32
nbands <- 2
projstring <- "EPSG:4326"
nodata <- -1
co <- c("TILED=YES", "BLOCKXSIZE=512", "BLOCKYSIZE=512", "COMPRESSION=LZW")

# Create a new raster dataset
ds <- createDataset(
  raster_path = raster_path,
  nbands = nbands,
  datatype = datatype,
  projstring = projstring,
  lr_lat = lr_lat,
  ul_lat = ul_lat,
  ul_lon = ul_lon,
  lr_lon = lr_lon,
  res = res,
  nodata = nodata,
  co = co
)

# Get the GDALRasterBand for ds
band <- ds[[1]]

# The updateBand can be the same
# using a different one just for testing
updateBand <- ds[[2]]

# Set some dummy values
band[[0, 0]] <- 1:(512 * 512)

# Calculate the double - 10
formulaCalculate(
  formula = ~x * 2 - 10,
  data = list(x = band),
  updateBand = updateBand
)

ds$Close()

Run the code above in your browser using DataLab