遥感影像生成tfw文件

9 篇文章 0 订阅

输入一个带仿射六参数的影像,生成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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值