Title: | GDAL Classes Wrapper for Reading and Writing Raster Blocks |
---|---|
Description: | Wraps around 'Geospatial' Data Abstraction Library (GDAL) raster and band classes for reading and writing directly from RasterBlock in R semantic `[[]]` and familiar syntax for accessing RasterBand and reading/writing to blocks (see <https://gdal.org/>). |
Authors: | Caio Hamamura [aut, cph, cre], Carlos Alberto Silva [aut] |
Maintainer: | Caio Hamamura <[email protected]> |
License: | GPL-3 |
Version: | 0.1.33 |
Built: | 2025-03-02 04:49:10 UTC |
Source: | https://github.com/caiohamamura/gdalBindings-R |
This function gives access to the GDALRasterBand using [[i]], where i is the band index to return.
## S3 method for class 'GDALDataset' x[[slice]]
## S3 method for class 'GDALDataset' x[[slice]]
x |
GDALDatset. Automatically obtained from GDALDataset[[]] call. |
slice |
Integer. The index for the band to access. |
An object of GDALRasterBand R6 class.
This function gives access to the GDALRasterBand using [[i]], where i is the band index to return.
## S3 method for class 'GDALRasterBand' x[[blockX, blockY]]
## S3 method for class 'GDALRasterBand' x[[blockX, blockY]]
x |
GDALRasterBand. Automatically obtained from GDALDataset[[]] call. |
blockX |
Integer. The x index for block to access. |
blockY |
Integer. The y index for block to access. |
Nothing, this is a setter
This function gives access to the GDALRasterBand using [[i]], where i is the band index to return.
## S3 replacement method for class 'GDALRasterBand' x[[blockX, blockY]] <- value
## S3 replacement method for class 'GDALRasterBand' x[[blockX, blockY]] <- value
x |
GDALRasterBand. Automatically obtained from GDALDataset[[]] call. |
blockX |
Integer. The x index for block to access. |
blockY |
Integer. The y index for block to access. |
value |
Integer. The value buffer to write |
Nothing, this is a setter
Closing ensures the data is actually flushed (saved) to the dataset also it releases the lock from the file.
## S3 method for class 'GDALDataset' close(con, ...)
## S3 method for class 'GDALDataset' close(con, ...)
con |
GDALDataset to be closed. |
... |
not used, inherited from generic close function. |
Create a new raster file based on specified data. It will output a *.tif file.
createDataset( raster_path, nbands, datatype, projstring, lr_lat, ul_lat, ul_lon, lr_lon, res, nodata, co = c("TILED=YES", "BLOCKXSIZE=512", "BLOCKYSIZE=512", "COMPRESSION=LZW") )
createDataset( raster_path, nbands, datatype, projstring, lr_lat, ul_lat, ul_lon, lr_lon, res, nodata, co = c("TILED=YES", "BLOCKXSIZE=512", "BLOCKYSIZE=512", "COMPRESSION=LZW") )
raster_path |
Character. The output path for the raster data |
nbands |
Integer. Number of bands. Default 1. |
datatype |
GDALDataType. The GDALDataType to use for the raster, use (GDALDataType$) to find the options. Default GDALDataType$GDT_Float64 |
projstring |
The projection string, either proj or WKT is accepted. |
lr_lat |
Numeric. The lower right latitude. |
ul_lat |
Numeric. The upper left latitude. |
ul_lon |
Numeric. The upper left longitude. |
lr_lon |
Numeric. The lower right longitude. |
res |
Numeric. The resolution of the output raster |
nodata |
Numeric. The no data value for the raster. Default 0. |
co |
CharacterVector. A CharacterVector of creation options for GDAL. Default NULL |
An object from GDALDataset R6 class.
# Parameters raster_path <- tempfile(fileext = ".tif") ul_lat <- -15 ul_lon <- -45 lr_lat <- -25 lr_lon <- -35 res <- c(0.01, -0.01) datatype <- GDALDataType$GDT_Int32 nbands <- 1 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]] # Set some dummy values band[[0, 0]] <- 1:(512 * 512) ds$Close()
# Parameters raster_path <- tempfile(fileext = ".tif") ul_lat <- -15 ul_lon <- -45 lr_lat <- -25 lr_lon <- -35 res <- c(0.01, -0.01) datatype <- GDALDataType$GDT_Int32 nbands <- 1 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]] # Set some dummy values band[[0, 0]] <- 1:(512 * 512) ds$Close()
Calculate raster values based on a formula
formulaCalculate(formula, data, updateBand)
formulaCalculate(formula, data, updateBand)
formula |
Formula. A formula to apply to the RasterBands from |
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. |
Nothing, it just updates the band of interest.
# 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()
# 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()
Wrapping class for GDALDataset C++ API exporting GetRasterBand, GetRasterXSize, GetRasterYSize
new()
Create a new raster file based on specified data. It will output a *.tif file.
GDALDataset$new(ds)
ds
GDALDatasetR pointer. Should not be used
datatype
GDALDataType. The GDALDataType to use for the raster, use (GDALDataType$) to find the options. Default GDALDataType$GDT_Float64
An object from GDALDataset R6 class.
GetRasterBand()
Function to retrieve the GDALRasterBand R6 Object.
GDALDataset$GetRasterBand(x)
x
Integer. The band index, starting from 1 to number of bands.
An object of GDALRasterBand R6 class.
GetRasterXSize()
Get the width for the raster
GDALDataset$GetRasterXSize()
An integer indicating the raster width
GetRasterYSize()
Get the height for the raster
GDALDataset$GetRasterYSize()
An integer indicating the raster height
Close()
Closes the GDALDataset
GDALDataset$Close()
An integer indicating the raster width
clone()
The objects of this class are cloneable with this method.
GDALDataset$clone(deep = FALSE)
deep
Whether to make a deep clone.
List of datatypes supported by the GDALDataset R6 class
GDALDataType
GDALDataType
An object of class list
of length 7.
Function to open GDAL Dataset
GDALOpen(filename, readonly = TRUE)
GDALOpen(filename, readonly = TRUE)
filename |
Character. The path to a GDAL dataset. |
readonly |
Logical. Flag to open a read only GDALDataset with GA_ReadOnly or GA_Update. Default TRUE. |
An R6 object of GDALDataset class.
ds_path <- system.file("extdata", "example.tif", package="gdalBindings") ds <- GDALOpen(ds_path) ds$Close()
ds_path <- system.file("extdata", "example.tif", package="gdalBindings") ds <- GDALOpen(ds_path) ds$Close()
Wrapping class for GDALRasterBand C++ API exporting GetBlockXSize, GetBlockYSize, ReadBlock, WriteBlock for better IO speed.
new()
Creates a new GDALRasterBand
GDALRasterBand$new(band)
band
The C++ pointer to the GDALRasterBandR object.
datatype
The GDALDataType for this band
An object of GDALRasterBand R6 class
ReadBlock()
Efficiently reads a raster block
GDALRasterBand$ReadBlock(iXBlock, iYBlock)
iXBlock
Integer. The i-th column block to access. The iXBlock
will be offset
from the origin.
iYBlock
Integer. The i-th row block to access. The iYBlock
will be offset
from the origin.
The returned Vector will be single dimensional with the length
. If you use matrix(, ncol=BLOCKXSIZE)
the matrix returned will actually be transposed. You should either transpose it or you can
calculate the indices using
RawVector for GDALDataType$GDT_Byte, IntegerVector for int types and NumericVector for floating point types.
WriteBlock()
Efficiently writes a raster block
GDALRasterBand$WriteBlock(iXBlock, iYBlock, buffer)
iXBlock
Integer. The i-th column block to write. The iXBlock
will be
offset from the origin.
iYBlock
Integer. The i-th row block to write. The iYBlock
will be offset
from the origin.
buffer
RawVector/IntegerVector/NumericVector depending on the GDALDataType.
This should be a 1D vector with size equal to raster
.
The returned Vector will be single dimensional with the length
.
If you use matrix(, ncol=BLOCKXSIZE) the matrix returned will actually be transposed.
You should either transpose it or you can calculate the indices using
.
Nothing
GetBlockXSize()
Get the block width
GDALRasterBand$GetBlockXSize()
An integer indicating block width
GetBlockYSize()
Get the block height
GDALRasterBand$GetBlockYSize()
An integer indicating block height
GetXSize()
Get the band width
GDALRasterBand$GetXSize()
An integer indicating band width
CalculateStatistics()
Calculate statistics for the GDALRasterBand
GDALRasterBand$CalculateStatistics()
nothing
GetYSize()
Get the band height
GDALRasterBand$GetYSize()
An integer indicating band height
GetNoDataValue()
Get band no data value
GDALRasterBand$GetNoDataValue()
Numeric indicating no data value
GetRasterDataType()
Get band datatype
GDALRasterBand$GetRasterDataType()
Numeric indicating the datatype
clone()
The objects of this class are cloneable with this method.
GDALRasterBand$clone(deep = FALSE)
deep
Whether to make a deep clone.
This constructor must not be called at all, this is automatically called from GDALDataset$GetRasterBand function.