GDAL拼接高程tif文件

//path 图片保存路径, bandnum 波段数, longitude 经度, latitude 纬度, pixel 像元分辨率(度/像素)
bool createImage(const char* path, int bandnum, double longitude, double latitude, double  pixelX, double pixelY, const QMap> &elevations)
{
	GDALAllRegister();
	CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
	const char* type = ("GTiff");
	GDALDriver *poDriver = GetGDALDriverManager()->GetDriverByName(type);

	int nImgWidth = elevations[0].size();
	int nImgHeight = elevations.size();

	GDALDataset *poDS = poDriver->Create(path, nImgWidth, nImgHeight, bandnum, GDT_Float32, NULL);
	if (!poDS) return false;

	//几何参数
	//0:左上角x坐标,3:左上角y坐标
	//1:东西方向一个像素距离(像元宽度,影像在宽度上的分辨率) 5:南北方向一个像素距离
	//2:行旋转 4:列旋转,如果影像是指北的, 2 和 4为0
	double adfGeoTransform[6] = { 0, 1, 0, 0, 0, 1 };
	adfGeoTransform[0] = longitude;
	adfGeoTransform[3] = latitude;
	adfGeoTransform[1] = pixelX;// pixel;
	adfGeoTransform[5] = -pixelY;// -pixel;
	adfGeoTransform[2] = 0;
	adfGeoTransform[4] = 0;
	poDS->SetGeoTransform(adfGeoTransform);

	float *band1 = new float[nImgWidth * nImgHeight];
	memset(band1, 0.0, nImgWidth * nImgHeight * sizeof(float));
	
	for (int i = 0; i GetRasterBand(i + 1)->RasterIO(GF_Write, 0, 0, nImgWidth, nImgHeight, band1, nImgWidth, nImgHeight, GDT_Float32, 0, 0);

	OGRSpatialReference oSRS;
	char *pszSRS_WKT = NULL;
	//oSRS.SetUTM(35, TRUE);
	oSRS.SetWellKnownGeogCS("WGS84");
	oSRS.exportToWkt(&pszSRS_WKT);
	poDS->SetProjection(pszSRS_WKT);
	CPLFree(pszSRS_WKT);//释放

	GDALClose(poDS);
	delete[] band1;

	return true;
};

你可能感兴趣的:(笔记)