//注册文件的格式
GDALAllRegister();
const char * pszfilename2 = "E:\\hdf2tif\\1.tif";
//获取数据集
GDALDataset *opDataset = (GDALDataset *)GDALOpen(pszfilename2,GA_ReadOnly);
if (opDataset == NULL)
{cout << "影像图为空" << endl; return 0;}
string des = opDataset->GetDriver()->GetDescription();//文件格式GTIFF
string des2 = opDataset->GetDescription();//路径名称
string datait = opDataset->GetDriver()->GetMetadataItem(GDAL_DMD_LONGNAME);//GEOtiff
int xsize = opDataset->GetRasterXSize();//得到X方向上的列数
int ysize = opDataset->GetRasterYSize();//得到Y方向上的行数
int num_bands = opDataset->GetRasterCount();//得到波段的个数
//得到投影坐标信息
if (opDataset->GetProjectionRef()!=NULL)
{cout << opDataset->GetProjectionRef()<<endl;}
// 获取仿射矩阵
double adfGeoTransform[6];
if (opDataset->GetGeoTransform(adfGeoTransform) == CE_None)//获得仿射矩阵
{
cout << fixed << setprecision(6) <<adfGeoTransform[0] << endl;//左上角x的坐标(由西向东方向)
//cout << fixed << setprecision(6) << adfGeoTransform[1] << endl;//X方向的分辨率
//cout << fixed << setprecision(6) << adfGeoTransform[2] << endl;//X方向的偏移量
cout << fixed << setprecision(6) << adfGeoTransform[3] << endl;//左上角Y的坐标
//cout << fixed << setprecision(6) << adfGeoTransform[4] << endl;//Y方向的偏移量
//cout << fixed << setprecision(6) << adfGeoTransform[5] << endl;//Y方向的分辨率
}
GDALRasterBand *opBand = opDataset->GetRasterBand(1);
// 最大最小值的获取
int isok[2];
double value_min = opBand->GetMinimum(&isok[0]);
double value_max = opBand->GetMaximum(&isok[1]);
if (isok[0] && isok[1])//判断最大与最小值是否提取成功
{
cout << value_min << endl;
cout << value_max << endl;
}
//获取波段的行与列数
int x_band = opBand->GetXSize();//得到该波段X方向的列数
int y_band = opBand->GetYSize();
//获取驱动
GDALDriver *myDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
if (myDriver == NULL)
{cout << "创建驱动失败"; return 0;}
// 创建输出文件
GDALDataset *outfile = myDriver->Create("E:\\my.tif", opBand->GetXSize(), opBand->GetYSize(), 1, GDT_Byte, NULL);
// 为输出文件导入仿射矩阵与坐标系统
outfile->SetGeoTransform(adfGeoTransform);
outfile->SetProjection(opDataset->GetProjectionRef());
float *pafscanline = new float[x_band * y_band];
outfile->RasterIO(GF_Write, 0, 0, x_band, y_band, pafscanline, x_band, y_band, GDT_Byte, 1,NULL,0,0,1);
cout << "数据的类型:" << opBand->GetRasterDataType() << endl;
for (int i = 0; i < x_band; i++)
{
//cout << pafscanline[i] << " ";
}
//cout << des << endl;
//cout << des2 << endl;
//cout << datait << endl;
//cout << xsize << endl;
//cout << ysize << endl;
//cout << ysize << endl;
//cout << num_bands << endl;
GDALClose(outfile);
delete[]pafscanline;
GDALClose((GDALDatasetH)opDataset);