输入一个带仿射六参数的影像,生成tfw文件。
#include <iostream>
#include <gdal.h>
#include <gdal_priv.h>
#include <streambuf>
#include <fstream>
#include <iomanip>
bool createTFW(char* imagePath);//创建tfw文件程序
int main(int argc,char*argv[])
{
cout << "程序功能:传入一张影像的绝对路径,生成这张影像的tfw文件!" << endl;
cout << "V1.0 zph 2021.6.9" << endl;
char* imagePath = argv[1];
createTFW(imagePath);
return 0;
}
bool createTFW(char* imagePath)
{
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); // 支持中文路径
GDALAllRegister(); //注册所有的驱动
GDALDataset *poDataset; //GDAL数据集
poDataset = (GDALDataset *)GDALOpen(imagePath, GA_ReadOnly);
if (poDataset == NULL)
{
cout << "fail in open files!!!" << endl;
return false;
}
double gt[6];
GDALGetGeoTransform(poDataset, gt);
cout << "gt:";//预览
for (auto value : gt)
cout << value << " ";
cout << endl;
fstream tfw;
string tmpname = imagePath;
tmpname = tmpname.substr(0, tmpname.find_last_of('.') + 1) + "tfw";
tfw.open(tmpname, ios::out);
tfw << fixed;
tfw << setprecision(10);//保留5位小数
tfw << gt[1] << endl;
tfw << gt[2] << endl;
tfw << gt[4] << endl;
tfw << gt[5] << endl;
tfw << gt[0] << endl;
tfw << gt[3] << endl;
tfw.close();
// 清空资源
delete poDataset;
printf("Success create %s!\n", tmpname.c_str());
return true;
}