GDAL C++ API learning path (1) GDALRasterBand code example translation self-study

GDALRasterBand  Class        <gdal_priv.h>

GDALRasterBandis a class used in GDAL to represent a band in a raster dataset. A raster dataset usually consists of multiple bands, and each band contains specific data information, such as elevation, red, green, blue, etc.

Used to represent different features of an image. A number of functions are provided to access and manipulate a band's pixel data and related information. It is one of the core classes for processing raster data in GDAL C++ API, which provides powerful functions and flexibility for raster data processing and analysis.

 

Public Functions​​​​​​​

GetXSize

int GetXSize()                

Get the XSize of the raster                

Returns: the width of this band in pixels

    // 获取第一个波段
    GDALRasterBand* band = dataset->GetRasterBand(1);

    // 获取 X 方向大小
    int xSize = band->GetXSize();

    // 输出 X 方向大小
    std::cout << "X Size: " << xSize << std::endl;

GetYSize

int GetYSize()

Get the YSize of the raster

Returns: the height of this band in pixels

same as above

GetBand

int GetBand()

Get the band number

Returns: band number (1+) or 0 (if band number is unknown)

    // 获取第一个波段
    GDALRasterBand* band = dataset->GetRasterBand(1);

    // 获取当前波段的频段编号
    int bandNumber = band->GetBand();

    // 输出频段编号
    std::cout << "Band Number: " << bandNumber << std::endl;

GetRasterDataType

GDALDataType GetRasterDataType(void)

Get the pixel data type of this band

Returns: the pixel data type of this band

    // 获取第一个波段
    GDALRasterBand* band = dataset->GetRasterBand(1);

    // 获取当前波段的像素数据类型
    GDALDataType dataType = band->GetRasterDataType();

    // 输出像素数据类型的字符串表示
    const char* dataTypeStr = GDALGetDataTypeName(dataType);
    std::cout << "Raster Data Type: " << dataTypeStr << std::endl

GDALDatasetIt is possible that different bands ( ) in a dataset ( GDALRasterBand) have different pixel data types. In practical applications, different bands in a multi-band dataset may represent different information, so their pixel data types can be different.

For example, for a color image, there are usually three bands: red, green, and blue. The red, green and blue bands may have different pixel data types, for example the red band may be an 8-bit unsigned integer (GDT_Byte), the green band may be a 16-bit unsigned integer (GDT_UInt16), and the blue band may be a 32-bit floating point number ( GDT_Float32).

Therefore, when working with multiband datasets, you need to be aware of the pixel data type for each band to ensure that the pixel values ​​are interpreted and handled correctly. You can obtain the pixel data type of each band through the functions GDALRasterBandof the class , and then process different types of pixel values.GetRasterDataType()

GetBlockSize

void GetBlockSize(int*, int*)

Get the "natural" block size for this band

parameter:

  • pnXSize  -- Integer to put X block size in or NULL.

  • pnYSize  – integer to put Y block size in or NULL 

    // 获取第一个波段
    GDALRasterBand* band = dataset->GetRasterBand(1);

    // 获取当前波段的数据块大小
    int blockWidth, blockHeight;
    band->GetBlockSize(&blockWidth, &blockHeight);

    // 输出数据块大小
    std::cout << "Block Width: " << blockWidth << std::endl;
    std::cout << "Block Height: " << blockHeight << std::endl;

GetActualBlockSize

CPLErr GetActualBlockSize(int, int, int*, int*)

Gets the actual block size for a given block offset, handles partial blocks at the edge of the raster and returns the actual number of pixels

parameter:

  • nXBlockOff  -- The horizontal block offset used to calculate the effective number of pixels, zero means the leftmost block, 1 means the next block, and so on.

  • nYBlockOff  -- The vertical block offset, zero means the leftmost block, 1 means the next block, and so on.

  • pnXValid  -- pointer to an integer in which the number of valid pixels in the x direction will be stored

  • pnYValid  -- pointer to an integer in which the number of valid pixels in the y direction will be stored

Return: CE_None whether the input parameter is valid, otherwise CE_Failure

GDALSuggestedBlockAccessPattern

virtual GDALSuggestedBlockAccessPattern GetSuggestedBlockAccessPattern() const

Return the recommended/most efficient access mode to the block (for read operations)

  1. GF_READ: Read-only mode. In this mode, data blocks can be read, but not written.

  2. GF_WRITE: Write-only mode. In this mode, data blocks can be written, but not read.

  3. GF_READWRITE: Read and write mode. In this mode, data blocks can be both read and written.

    // 获取第一个波段
    GDALRasterBand* band = dataset->GetRasterBand(1);

    // 获取数据块的访问模式
    GDALSuggestedBlockAccessPattern accessPattern = band->GetSuggestedBlockAccessPattern();

    // 根据获取的访问模式进行操作
    switch (accessPattern)
    {
    case GF_READ: // 仅读取模式
        std::cout << "Block Access Pattern: Read Only" << std::endl;
        break;
    case GF_READWRITE: // 读写模式
        std::cout << "Block Access Pattern: Read and Write" << std::endl;
        break;
    case GF_WRITE: // 仅写入模式
        std::cout << "Block Access Pattern: Write Only" << std::endl;
        break;
    default:
        std::cout << "Block Access Pattern: Unknown" << std::endl;
        break;
    }

GetAccess

GDALAccess GetAccess()

Find out if we have update rights for this band

Return: either GA_Update or GA_ReadOnly

    // 获取第一个波段
    GDALRasterBand* band = dataset->GetRasterBand(1);

    // 获取数据块的访问模式
    GDALAccess access = band->GetAccess();

    // 根据获取的访问模式进行操作
    switch (access)
    {
    case GA_ReadOnly:
        std::cout << "Band Access Mode: Read Only" << std::endl;
        break;
    case GA_Update:
        std::cout << "Band Access Mode: Read and Write" << std::endl;
        break;
    default:
        std::cout << "Band Access Mode: Unknown" << std::endl;
        break;
    }

RasterIO

CPLErr RasterIOGDALRWFlag, int, int, int, void*, int, int, GDALDataType, GSpacing , GSpacing, GDALRasterIOExtraArg *psExtraArg )

Read/write image data area for this band

parameter:

  • eRWFlag  -- GF_Read reads the data area, or GF_Write writes the data area.

  • nXOff  – Offset in pixels of the upper left corner of the band region to be accessed. Starting from the left will be zero.

  • nYOff  – The line offset to the upper left corner of the band region to be accessed. Starting at the top, this will be zero.

  • nXSize  – Width in pixels of the band region to be accessed.

  • nYSize  – The height of the band region to access in row units.

  • pData  --  [inout]  The buffer into which data should be read or written. This buffer must contain at least nBufXSize * nBufYSize words of type eBufType. It is organized in pixel order from left to right and top to bottom. Spacing is controlled by the nPixelSpace and nLineSpace parameters.

  • nBufXSize  -- The width of the buffer image to read from or write to the desired region.

  • nBufYSize  -- The height of the buffer image to read from or write to the desired region.

  • eBufType  -- The type of pixel values ​​in the pData data buffer. Pixel values ​​will be automatically converted to the GDALRasterBand data type as needed   .

  • nPixelSpace  -- The byte offset from the start of one pixel value in pData to the start of the next pixel value within the scanline. If default (0), the size of data type eBufType is used.

  • nLineSpace  -- The offset in bytes from the beginning of one scanline to the beginning of the next scanline in pData. If default (0), size of data type eBufType * nBufXSize is used.

  • psExtraArg  --  [in] (New in GDAL 2.0) Pointer to  a GDALRasterIOExtraArg  structure with additional parameters for specifying resampling and progress callbacks, or NULL for default behavior. It is also possible to define the GDAL_RASTERIO_RESAMPLING configuration option to override the default resampling to one of bilinear, cubic, cubic-like, lanzos, average or mode.

Returns: CE_Failure if the access fails, otherwise CE_None

    // 获取第一个波段
    GDALRasterBand* band = dataset->GetRasterBand(1);

    // 定义读取的数据范围和数据类型
    int xOff = 0;            // 读取数据的起始列
    int yOff = 0;            // 读取数据的起始行
    int xSize = 100;         // 读取数据的列数
    int ySize = 100;         // 读取数据的行数
    int nbufXSize = xSize;    // 输出缓冲区的列数(设置与 xSize 相同)
    int nbufYSize = ySize;    // 输出缓冲区的行数(设置与 ySize 相同)
    GDALDataType dataType = GDT_Float32;  // 读取的数据类型

    // 分配输出缓冲区
    float* buffer = new float[xSize * ySize];

    // 定义读取选项
    GDALRasterIOExtraArg options;
    options.eResampleAlg = GRIORA_NearestNeighbour; // 设置重采样算法

    // 执行数据读取
    CPLErr err = band->RasterIO(GF_Read, xOff, yOff, xSize, ySize, buffer, nbufXSize, nbufYSize, dataType, 0, 0, &options);

    // 检查读取是否成功
    if (err != CE_None)
    {
        std::cout << "Error reading raster data." << std::endl;
    }
    else
    {
        // 在此处处理读取的数据
        // 例如,可以输出前 10 个像素的值
        for (int i = 0; i < 10; i++)
        {
            std::cout << "Pixel " << i << ": " << buffer[i] << std::endl;
        }
    }

    // 释放资源并关闭数据集
    delete[] buffer;

ReadBlock

CPLErr ReadBlock(int, int, void *)

Efficient reading of image data blocks

parameter:

  • nXBlockOff  -- The horizontal block offset, zero means the leftmost block, 1 means the next block, and so on.

  • nYBlockOff  -- Vertical block offset, zero means the topmost block, 1 means the next block, and so on.

  • pImage  -- The buffer into which the data will be read. The buffer must be large enough to hold  GetBlockXSize()*GetBlockYSize() words of type GetRasterDataType() .

Return: CE_None success or CE_Failure error

    // 获取第一个波段
    GDALRasterBand* band = dataset->GetRasterBand(1);

    // 定义数据块的左上角坐标
    int blockXOff = 0;  // 数据块的起始列
    int blockYOff = 0;  // 数据块的起始行

    // 获取数据块的大小
    int blockXSize = band->GetBlockXSize();  // 数据块的列数
    int blockYSize = band->GetBlockYSize();  // 数据块的行数

    // 分配输出缓冲区
    float* buffer = new float[blockXSize * blockYSize];

    // 执行数据读取
    CPLErr err = band->ReadBlock(blockXOff, blockYOff, buffer);
    
    //执行某些操作

    // 释放资源并关闭数据集
    delete[] buffer;

WriteBlock

CPLErr WriteBlock(int, int, void *)

Efficient writing of image data blocks

parameter:

  • nXBlockOff  -- The horizontal block offset, zero means the leftmost block, 1 means the next block, and so on.

  • nYBlockOff  -- The vertical block offset, zero means the leftmost block, 1 means the next block, and so on.

  • pImage  -- The buffer from which the data will be written. The buffer must be large enough to hold  GetBlockXSize()*GetBlockYSize() words of type GetRasterDataType() .

Return: CE_None success or CE_Failure error

    // 获取第一个波段
    GDALRasterBand* band = dataset->GetRasterBand(1);

    // 定义数据块的左上角坐标
    int blockXOff = 0;  // 数据块的起始列
    int blockYOff = 0;  // 数据块的起始行

    // 获取数据块的大小
    int blockXSize = band->GetBlockXSize();  // 数据块的列数
    int blockYSize = band->GetBlockYSize();  // 数据块的行数

    // 分配输入缓冲区,用于存储要写入数据块的栅格数据
    float* buffer = new float[blockXSize * blockYSize];

    // 在此处填充输入缓冲区,例如,将缓冲区中的数据设置为一定值

    // 执行数据写入
    CPLErr err = band->WriteBlock(blockXOff, blockYOff, buffer);

    //执行某些操作

    // 释放资源并关闭数据集
    delete[] buffer;

​​​​​​​

Guess you like

Origin blog.csdn.net/qq_69574549/article/details/132042221