c++ 读取 .tfw 文件数据

c++ 读取 .tfw 文件数据,第1张

1、按读取 .txt 方法读取
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include "opencv2\imgproc\types_c.h"

using namespace std;
using namespace cv;


int main() 
{
	ifstream ifs;							//创建流对象

	ifs.open(".\\wgs.tfw", ios::in);		//打开文件

	if (!ifs.is_open())						//判断文件是否打开
	{
		cout << "打开文件失败!!!";
		return 0;
	}

	//读取正确匹配特征点

	vector item;				//用于存放文件中的一行数据

	string temp;						//把文件中的一行数据作为字符串放入容器中


	while (getline(ifs, temp))          //利用getline()读取每一行,并放入到 item 中
	{
		item.push_back(temp);
	}

	vector res;

	for (int i = 0; i < item.size(); i++)
	{
		//数据类型转换,将string类型转换成float,如果字符串不是有数字组成,q则字符被转化为 0
		res.emplace_back(atof(item[i].c_str()));
	}

	for (double num : res)
		cout << num << endl;

	system("pause");
	return 0;
}
2、使用 GDAL 进行读取 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include"gdal_priv.h"

using namespace std;



int main() 
{
	GDALAllRegister();

	GDALDataset* podataset = (GDALDataset*)GDALOpen("..\\WGS84\\wgs.tif", GA_ReadOnly);
    
    //保存 .tif 文件的投影信息' x=geo[0]、y=geo[3]、像素大小=geo[1]
	double geo[6];

	podataset->GetGeoTransform(geo);

	for (int i = 0; i < 6; i++)
		cout << geo[i] << endl;

	system("pause");
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/562894.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-02
下一篇 2022-04-02

发表评论

登录后才能评论

评论列表(0条)

保存