【C++】【GADL】读取栅格数据获取信息

every blog every motto: Light tomorrow with today.
0. 前言 简单记录下用c++读取栅格数据
1. 正文

#include #include "gdal_priv.h"using namespace std; // 读取栅格数据 void loadRasterData() { const char * fileName = "D:\\Programme\\Viual Studio\\C++\\learning\\point_to_line\\Project1_test\\images\\grid.tiff"; GDALAllRegister(); // 注册驱动 // 支持中文路径 CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); // 读取数据 GDALDataset * pDataset = (GDALDataset *)GDALOpen(fileName, GA_ReadOnly); // 判断是否读取成功 if (NULL == pDataset) { cout << "未能成功读取!" << endl; return; } // 输出图像格式信息 CPLString strDrive = pDataset->GetDriver()->GetDescription(); CPLString strInfo = pDataset->GetDriver()->GetMetadataItem(GDAL_DMD_LONGNAME); cout << "文件格式:" << strDrive << " " << strInfo << endl; // 获取波段,长宽和波段数 int x = pDataset->GetRasterXSize(); int y = pDataset->GetRasterYSize(); int band = pDataset->GetRasterCount(); // 高 cout << "影像x: " << x << endl; cout << "影像y: " << y << endl; cout << "波段数:" << band << endl; cout << "-------------------------------------------------------" << endl; // 输出投影方式 CPLStringstrProjection = pDataset->GetProjectionRef(); if (strProjection != "") // 如果没有投影信息,返回一个空字符串,不是NULL; { cout << "投影方式:" << strProjection << endl; cout << "-------------------------------------------------------" << endl; } //输出几何信息 double GeoTransform[6]; if (pDataset->GetGeoTransform(GeoTransform) == CE_None) // CE_None 表示成功获取 { cout << "几何信息为:" << endl; for (int i = 0; i < 6; i++) { cout << GeoTransform[i] << " "; } cout << endl; cout << "-------------------------------------------------------" << endl; } // 图像波段信息 GDALRasterBand * pRasterBand; pRasterBand = pDataset->GetRasterBand(1); // 第一个波段指针 int nxBlock, nyBlock; pRasterBand->GetBlockSize(&nxBlock, &nyBlock); // TODO CPLString DataType = GDALGetDataTypeName(pRasterBand->GetRasterDataType()); // 获取数据类型名字 CPLString ColorType = GDALGetColorInterpretationName(pRasterBand->GetColorInterpretation()); // 获取颜色类型名称 cout << "分块大小为:" << nxBlock << " / " << nyBlock << endl; cout << "数据类型:" << DataType << endl; cout << "颜色类型:" << ColorType << endl; cout << "-------------------------------------------------------" << endl; // 统计像素的最值 int arrMaxMin[2]; arrMaxMin[0] = pRasterBand->GetMaximum(); arrMaxMin[1] = pRasterBand->GetMinimum(); cout << "像素的最大值:" << arrMaxMin[0] << endl; cout << "像素的最小值" << arrMaxMin[1] << endl; // 图像金字塔信息 if(pRasterBand->GetOverviewCount()!=0) // 获取当前波段的金字塔层数,如果没有金字塔返回0 { cout << "金字塔数目:" << pRasterBand->GetOverviewCount() << endl; cout << "-------------------------------------------------------" << endl; } return; }int main() { // 读取数据 loadRasterData(); system("pause"); return 0; }

结果:
【C++】【GADL】读取栅格数据获取信息
文章图片

参考文献 【【C++】【GADL】读取栅格数据获取信息】[1] https://blog.csdn.net/weixin_42310154/article/details/102930499?utm_medium=distribute.pc_relevant_download.none-task-blog-baidujs-4.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-baidujs-4.nonecase
[2] https://blog.csdn.net/qq_36382357/article/details/83933209

    推荐阅读