本文主要介绍:GDAL常用函数的应用,其中包含图像数据的读取、写入,地理坐标与行列坐标的相互转化,颜色表的读取和设置。
一、图像的读取与写入
//初始化GDAL库注册表
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
//打开图像
CString inPath("C:\\Users\\Administrator\\Desktop\\GF1.tif");
GDALDataset * pInDataset = (GDALDataset * )GDALOpen(inPath,GA_ReadOnly);
if(pInDataset==NULL)
{
AfxMessageBox("读取图像失败!");
return FALSE;
}
int Width = pInDataset->GetRasterXSize(); //获取图像宽
int Height = pInDataset->GetRasterYSize(); //获取图像高
int Count = pInDataset->GetRasterCount(); //波段数
//读取数据
GDALRasterBand *pInRasterBand = pInDataset->GetRasterBand(1);
float *inBuf;
inBuf = new float[Width*Height];
ZeroMemory(inBuf,sizeof(float)*Width*Height);
CPLErr err;
err=pInRasterBand->RasterIO(GF_Read,0,0,Width,Height,inBuf,Width,Height,GDT_Float32,0,0);
if(err==CE_Failure)
{
AfxMessageBox("读取输入图像数据失败!");
return FALSE;
}
//创建输出图像、写图像
CString outPath("C:\\Users\\Administrator\\Desktop\\GF1000.tif");
GDALDriver *poDriver =GetGDALDriverManager()->GetDriverByName("GTiff");
if( poDriver==NULL)
{
AfxMessageBox("获取创建输出图像指针poDriver失败!");
return FALSE;
}
CString OutFilename = CString(outPath);
Ou