在C++中使用GDAL库来读写栅格数据,可以按照以下步骤进行:
#include "gdal/gdal.h"
GDALAllRegister();
GDALDataset *poDataset = (GDALDataset *)GDALOpen("path/to/raster.tif", GA_ReadOnly);
if(poDataset == NULL) {
// 处理打开失败的情况
}
int nXSize = poDataset->GetRasterXSize();
int nYSize = poDataset->GetRasterYSize();
int nBands = poDataset->GetRasterCount();
GDALRasterBand *poBand = poDataset->GetRasterBand(1);
GDALDataType eType = poBand->GetRasterDataType();
int nXOff = 0, nYOff = 0, nXSize = 100, nYSize = 100; // 读取的区域大小
int *pData = new int[nXSize * nYSize];
poBand->RasterIO(GF_Read, nXOff, nYOff, nXSize, nYSize, pData, nXSize, nYSize, eType, 0, 0);
int *pData = new int[nXSize * nYSize]; // 要写入的数据
poBand->RasterIO(GF_Write, nXOff, nYOff, nXSize, nYSize, pData, nXSize, nYSize, eType, 0, 0);
GDALClose(poDataset);
以上是使用GDAL库读写栅格数据的基本步骤,开发者可以根据具体需求进行定制化操作。