通过GDAL获取影像数据的波段数、长和宽以及投影信息。
主要代码如下:
#include "gdal_priv.h"
#pragma comment(lib,"gdal_i.lib")
#include "ogr_geometry.h"
bool getImageInfo(const char* path, int& nBandnum, int& nImgWidth, int& nImgHeight, double *dGeoTrans)
{
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
CPLSetConfigOption("USE_RRD","YES");
GDALDataset * poDataset = (GDALDataset *) GDALOpen(path, GA_ReadOnly);
if(poDataset == NULL)
{
printf("open file fail.\n");
return false;
}
nImgWidth = poDataset -> GetRasterXSize();
nImgHeight = poDataset -> GetRasterYSize();
nBandnum = poDataset -> GetRasterCount();
poDataset -> GetGeoTransform(dGeoTrans);
GDALClose(poDataset);
return true;
}
在main函数中,需要定义路径以及相应的变量
int main(int argc, char* argv[])
{
const char* path = "data//test.tif";
int nBandnum, nImgWidth, nImgHeight;
double *dGeoTrans = new double [6];
if (getImageInfo(path, nBandnum, nImgWidth, nImgHeight, dGeoTrans) == false)
return -1;
printf("Bandnum: %d \n", nBandnum);
printf("ImgWidth: %d \n", nImgWidth);
printf("ImgHeight: %d \n", nImgHeight);
for (int i = 0; i < 6; ++i)
{
printf("GeoTrans %d : %.2f \n", i + 1, dGeoTrans[i]);
}
system("pause");
return 0;
}
运行结果如下:
欢迎大家批评指正。