C++gdal读取tif数据到一维数组

//获取基础数据,读取到的文件的个数,tif长宽信息、数据类型信息、投影信息、六参数信息、
void getbasicdata(vector<string>filepath,int &file_count,int &width,int &height,int &depth,int &bandNum,const char* p,double* ad, GDALDataType &gdal_datatype)
{
	file_count = filepath.size();
	const char* pszFile = filepath[0].c_str();
	GDALAllRegister(); 
	CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
	GDALDataset *poDataset = (GDALDataset*)GDALOpen(pszFile, GA_ReadOnly);
	GDALRasterBand *poBand;
	poBand= poDataset->GetRasterBand(1);
	p = poDataset->GetProjectionRef();//图像的投影信息
	poDataset->GetGeoTransform(ad);//影像六参数信息
	gdal_datatype = poBand->GetRasterDataType();//获取影像数据类型
	width = poBand->GetXSize();//宽
	height= poBand->GetYSize();//高
	depth = GDALGetDataTypeSize(poDataset->GetRasterBand(1)->GetRasterDataType()) / 8;//图像深度
	bandNum = poDataset->GetRasterCount();//获取波段数
	GDALClose(poDataset);
}
//打开单波段tif文件,存入一维数组
double *open_tif(vector<string>filepath, double* data, int file_count, int w, int h,GDALDataType gdal_datatype)
{
	int file_number = 0;
	for (vector<string>::iterator it = filepath.begin(); it != filepath.end(); it++)
	{
		const char* pszFile = it->c_str();
		GDALAllRegister();
		CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
		GDALDataset *poDataset = (GDALDataset*)GDALOpen(pszFile, GA_ReadOnly);
		GDALRasterBand * pRasterBand;
		pRasterBand = poDataset->GetRasterBand(1); // 第一个波段指针

		double noData = poDataset->GetRasterBand(1)->GetNoDataValue();//noData数值确定
		double  *mat = new double [w*h];
		pRasterBand->RasterIO(GF_Read, 0, 0, w, h, mat, w, h, GDT_Float64, 0, 0);
		for (int i = 0; i < w*h; i++)
		{
				data[file_number*w*h + i] = mat[i];
		}
		file_number++;
		GDALClose(poDataset);
		mat = NULL;
		delete mat;
	}
	return data;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值