sgsPy
structurally guided sampling
Loading...
Searching...
No Matches
sgs::raster::GDALRasterWrapper

#include <raster.h>

Public Member Functions

 GDALRasterWrapper (std::string filename, std::string projDBPath)
 GDALRasterWrapper (GDALDataset *p_dataset, std::vector< void * > bands)
 GDALRasterWrapper (GDALDataset *p_dataset)
 GDALRasterWrapper (py::buffer buffer, std::vector< double > geotransform, std::string projection, std::vector< double > nanVals, std::vector< std::string > names, std::string projDBPath)
 ~GDALRasterWrapper ()
void close (void)
GDALDataset * getDataset ()
std::string getDriver ()
std::string getFullProjectionInfo ()
std::string getCRS ()
int getWidth ()
int getHeight ()
int getBandCount ()
double getXMax ()
double getXMin ()
double getYMax ()
double getYMin ()
double getPixelWidth ()
double getPixelHeight ()
std::vector< std::string > getBands ()
double * getGeotransform ()
double getBandNoDataValue (int band)
py::buffer getRasterBandAsMemView (int width, int height, int band)
void releaseBandBuffers (void)
GDALRasterBand * getRasterBand (int band)
void * getRasterBandBuffer (int band)
GDALDataType getRasterBandType (int band)
size_t getRasterBandTypeSize (int band)
void write (std::string filename)
void setTempDir (std::string tempDir)
std::string getTempDir ()
std::vector< double > getGeotransformArray ()
std::string getDataType ()

Detailed Description

Wrapper class for a GDAL dataset containing a raster image.

This class provides getter methods for important raster data and metadata, as well as a way to access the raster as an array.

Memory is controlled using either a smart pointer (in the case of the dataset) or CPLMalloc and CPLFree (in the case of the raster image). The function GDALDataset::RasterIO() function should do the windowing for us behind the scenes, meaning we don't have to worry about physical memory size when allocating for large raster images.

The raster buffer contains all of the raster bands and can be indexed in Python with: [band][y][x] and in C++ with: [band * size * width * height + y * size * width + x * size]

The buffer is exposed to the Python side of the application using a py::buffer, and it's exposed to the C++ side of the application using a void *. The expectation is that this void pointer will be cast to another data type pointer as required.

Constructor & Destructor Documentation

◆ GDALRasterWrapper() [1/4]

sgs::raster::GDALRasterWrapper::GDALRasterWrapper ( std::string filename,
std::string projDBPath )
inline

Constructor for GDALRasterWrapper class. Creates GDALDataset using drivers and given file, then calls createFromDataset() passing the created object. Set the search path for the proj.db file, because this function may be the first called from the Python side of the application, meaning this instance of GDAL may not have found a proj.db file yet.

Parameters
std::stringfilename
std::stringprojDBPath

◆ GDALRasterWrapper() [2/4]

sgs::raster::GDALRasterWrapper::GDALRasterWrapper ( GDALDataset * p_dataset,
std::vector< void * > bands )
inline

Constructor for GDALRasterWrapper class if an in-memory dataset has already been created. The bands (already read and allocated) are passed as the second parameter. Calls createFromDataset() passing p_dataset parameter, and sets internal raster band parameters accordingly.

Parameters
GDALDataset*p_dataset GDAL raster dataset
std::vector<void*> raster bands

◆ GDALRasterWrapper() [3/4]

sgs::raster::GDALRasterWrapper::GDALRasterWrapper ( GDALDataset * p_dataset)
inline

Constructor for GDALRasterWrapper class using just a GDAL dataset pointer, by calling createFromDataset().

Parameters
GDALDataset*p_dataset GDAL raster dataset

◆ GDALRasterWrapper() [4/4]

sgs::raster::GDALRasterWrapper::GDALRasterWrapper ( py::buffer buffer,
std::vector< double > geotransform,
std::string projection,
std::vector< double > nanVals,
std::vector< std::string > names,
std::string projDBPath )
inline

Constructor for generating a GDALRasterWrapper from a numpy array and metadata. The numpy array is passed in the form of a py::buffer. Set the search path for the proj.db file, because this function may be the first called from the Python side of the application, meaning this instance of GDAL may not have found a proj.db file yet.

Parameters
py::bufferbuffer
std::vector<double>geotransform
std::stringprojection
std::vector<double>nanVals
std::vector<std::string>names
std::stringprojDBPath

◆ ~GDALRasterWrapper()

sgs::raster::GDALRasterWrapper::~GDALRasterWrapper ( )
inline

Deconstructor for GDALRasterWrapper class. This method calls CPLFree() on any allocated raster buffers.

Member Function Documentation

◆ close()

void sgs::raster::GDALRasterWrapper::close ( void )
inline

This is a copy of the deconstructor, it cleans up the all of the heap allocations which the class has made. This is meant to be called by the Python side of the application.

In the case where the used tries to continue to use the object after it has been cleaned up, there will be undefined behavior.

For this reason, there is a check to ensure no members of this object are accessed after it has been deleted from within the Pyhton code.

◆ getBandCount()

int sgs::raster::GDALRasterWrapper::getBandCount ( )
inline

Getter method for the number of raster bands.

Returns
int

◆ getBandNoDataValue()

double sgs::raster::GDALRasterWrapper::getBandNoDataValue ( int band)
inline

Getter method for a specific (0-indexed) bands nodata value.

Returns
double

◆ getBands()

std::vector< std::string > sgs::raster::GDALRasterWrapper::getBands ( )
inline

Getter method for raster band names. Bands occur in order, meaning bands[0] corresponds to band 1, bands[1] to band 2, etc.

Returns
std::vector<std::string>

◆ getCRS()

std::string sgs::raster::GDALRasterWrapper::getCRS ( )
inline

Get the CRS name from the OGRSpatialReference object

Returns
std::string

◆ getDataset()

GDALDataset * sgs::raster::GDALRasterWrapper::getDataset ( )
inline

Getter method for wrapped dataset.

Returns
GDALDataset *

◆ getDataType()

std::string sgs::raster::GDALRasterWrapper::getDataType ( )
inline

Gets the data type of the whole raster. If different bands have different types, returns "".

This is meant to be used by the Python side of the applications, specifically when converting from an sgs object to another Python geospatial library object.

Returns
std::string

◆ getDriver()

std::string sgs::raster::GDALRasterWrapper::getDriver ( )
inline

Getter method for the raster driver.

Returns
std::string

◆ getFullProjectionInfo()

std::string sgs::raster::GDALRasterWrapper::getFullProjectionInfo ( )
inline

Getter method for the full projection information as wkt.

Returns
std::string

◆ getGeotransform()

double * sgs::raster::GDALRasterWrapper::getGeotransform ( )
inline

Getter method for geotransform.

Returns
double *

◆ getGeotransformArray()

std::vector< double > sgs::raster::GDALRasterWrapper::getGeotransformArray ( )
inline

Getter method for the geotransform. Meant to be used by the python side of the application. Specifically, used when converting from an sgs object to another Python geospatial library object.

Returns
std::vector<double>

◆ getHeight()

int sgs::raster::GDALRasterWrapper::getHeight ( )
inline

Getter method for the raster height.

Returns
int

◆ getPixelHeight()

double sgs::raster::GDALRasterWrapper::getPixelHeight ( )
inline

Getter method for the pixel height. Scalar (absolute) value is given. see https://gdal.org/en/stable/tutorials/geotransforms_tut.html

Returns
double

◆ getPixelWidth()

double sgs::raster::GDALRasterWrapper::getPixelWidth ( )
inline

Getter method for the pixel width. Scalar (absolute) value is given. see https://gdal.org/en/stable/tutorials/geotransforms_tut.html

Returns
double

◆ getRasterBand()

GDALRasterBand * sgs::raster::GDALRasterWrapper::getRasterBand ( int band)
inline

Getter method for a GDALRasterBand in the raster, used by the C++ side of the application.

Parameters
intband
Returns
GDALRasterBand *

◆ getRasterBandAsMemView()

py::buffer sgs::raster::GDALRasterWrapper::getRasterBandAsMemView ( int width,
int height,
int band )
inline

Getter method for the raster image, used by the Python side of the application. This function allocates and reads a raster band if necessary, and uses py::memoryview::from_buffer() to create the buffer of the correct size/dimensions without copying data unecessarily.

This function requires that width and height be defined according to GDAL target_downscaling_factor rules. Otherwise, the incorrect amount of memory will be allocated. Information on target_downsampling_factor can be found here: https://gdal.org/en/stable/api/gdaldataset_cpp.html#classGDALDataset_1ae66e21b09000133a0f4d99baabf7a0ec

for py::memoryview::from_buffer() information see: https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html#memory-view

Parameters
intwidth
intheight
intband
Returns
py::buffer python memoryview of the raster

◆ getRasterBandBuffer()

void * sgs::raster::GDALRasterWrapper::getRasterBandBuffer ( int band)
inline

Getter method for the whole GDALRasterBand data buffer.

Parameters
int
Returns
void *

◆ getRasterBandType()

GDALDataType sgs::raster::GDALRasterWrapper::getRasterBandType ( int band)
inline

Getter method for the pixel / raster data type.

Parameters
intband
Returns
GDALDataType

◆ getRasterBandTypeSize()

size_t sgs::raster::GDALRasterWrapper::getRasterBandTypeSize ( int band)
inline

Getter method for the pixel /raster data type size.

Parameters
intband
Returns
size_t

◆ getTempDir()

std::string sgs::raster::GDALRasterWrapper::getTempDir ( )
inline

Getter function for the rasters temporary directory.

Returns
std::string

◆ getWidth()

int sgs::raster::GDALRasterWrapper::getWidth ( )
inline

Getter method for the raster width.

Returns
int

◆ getXMax()

double sgs::raster::GDALRasterWrapper::getXMax ( )
inline

Getter method for the maximum x value in georeferenced coordinate space. see https://gdal.org/en/stable/tutorials/geotransforms_tut.html

Returns
double

◆ getXMin()

double sgs::raster::GDALRasterWrapper::getXMin ( )
inline

Getter method for the minimum x value in georeferenced coordinate space. see https://gdal.org/en/stable/tutorials/geotransforms_tut.html

Returns
double

◆ getYMax()

double sgs::raster::GDALRasterWrapper::getYMax ( )
inline

Getter method for the maximum y value in georeferenced coordinate space. see https://gdal.org/en/stable/tutorials/geotransforms_tut.html

Returns
double

◆ getYMin()

double sgs::raster::GDALRasterWrapper::getYMin ( )
inline

Getter method for the minimum y value in georeferenced coordinate space. see https://gdal.org/en/stable/tutorials/geotransforms_tut.html

Returns
double

◆ releaseBandBuffers()

void sgs::raster::GDALRasterWrapper::releaseBandBuffers ( void )
inline

This function is used when converting the sgs SpatialRaster Python class (which wraps this GDALRasterWrapper class) to a different data type, typically for a different geospatial package.

If the user desires the array as a numpy array, then the data must be passed as a buffer AND the ownership of the data must be released from the C++ object, so it is not cleaned up by the deconstructor when the C++ object is freed.

This is essentially a memory leak as far as the C++ code is concerned, however the numpy array will retain ownership and delete when required.

◆ setTempDir()

void sgs::raster::GDALRasterWrapper::setTempDir ( std::string tempDir)
inline

Give the GDALRasterWrapper ownership of a temporary directory, for it to remove when deconstructed.

Parameters
std::stringtempDir

◆ write()

void sgs::raster::GDALRasterWrapper::write ( std::string filename)
inline

Writes the raster to a specific file given by filename, by creating a copy of the GDALDatset.

Parameters
std::stringfilename

The documentation for this class was generated from the following file: