#include <iostream>
#include "gdal_priv.h"
//#include "cpl_conv.h"
int main()
{
// 注册文件格式
GDALAllRegister();
//影像路径
const char* img_dir = "E:\\beijing_landsat\\LC8_123032_2013244_LGN02\\LC08_L1TP_123032_20130901_20180523_01_T1_B1.TIF";
//使用读取方式打开影像
GDALDataset* poDataset = (GDALDataset *)GDALOpen(img_dir, GA_ReadOnly);
if (poDataset == NULL)
{
//printf("No such file or directory");
std::cout << "No such file or directory" << std::endl;
return 0;
};
//输出图像格式
std::cout << "Driver:\n"
<< poDataset->GetDriver()->GetDescription()<<"\n"
<< poDataset->GetDriver()->GetMetadataItem(GDAL_DMD_LONGNAME) // Long name of the driver
<< std::endl;
//图像大小及波段数
std::cout << "Size is: "
<< poDataset->GetRasterXSize() << " "
<< poDataset->GetRasterYSize() << " "
<< poDataset->GetRasterCount()
<< std::endl;
//输出图像投影信息
if (poDataset->GetProjectionRef() != NULL)
{
std::cout << "Projection: \n"
<< poDataset->GetProjectionRef()
<< std::endl;
}
//输出图像坐标及分辨率信息
double adfGeoTransform[6];
if (poDataset->GetGeoTransform(adfGeoTransform) == CE_None)
{
std::cout << "Origin:\n"
<< adfGeoTransform[0] << " "
<< adfGeoTransform[3] << std::endl;
std::cout << "PixelSize:\n"
<< adfGeoTransform[1] << " "
<< adfGeoTransform[5] << std::endl;
}
//查看像元最大值与最小值
GDALRasterBand* poBand = poDataset->GetRasterBand(1);
int bGotMin, bGotMax;
double adfMinMax[2];
adfMinMax[0] = poBand->GetMinimum(&bGotMin);
adfMinMax[1] = poBand->GetMaximum(&bGotMax);
std::cout << "Min: " << adfMinMax[0] <<"\n"
<< "Max: " << adfMinMax[1] << std::endl;
//在堆区开辟内存,存储图像第一行数据
int nXSize = poBand->GetXSize();
float* pafScanline = new float[nXSize];
//读取图像第一行数据
poBand->RasterIO(GF_Read, 0, 0, nXSize, 1, pafScanline, nXSize, 1, GDT_Float32, 0, 0);
//释放内存,并关闭文件
delete []pafScanline;
GDALClose((GDALDatasetH)poDataset);
}
结果:
Driver:
GTiff
GeoTIFF
Size is: 7761 7881 1
Warning 1: PROJ: proj_create_from_database: Cannot find proj.db
Warning 1: The definition of projected CRS EPSG:32650 got from GeoTIFF keys is not the same as the one from the EPSG registry, which may cause issues during reprojection operations. Set GTIFF_SRS_SOURCE configuration option to EPSG to use official parameters (overriding the ones from GeoTIFF keys), or to GEOKEYS to use custom values from GeoTIFF keys and drop the EPSG code.
Projection:
PROJCS["WGS 84 / UTM zone 50N",GEOGCS["WGS 84",DATUM["World Geodetic System 1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",117],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
Origin:
357285 4.58252e+06
PixelSize:
30 -30
Min: 0
Max: 47113
参考资料:
《GDAL源码剖析与开发指南》