C++代码,将多个文件中的GPS经纬度转化成百度经纬度

在处理数据的时候,由于需要将很多个文件的GPS经纬度转化成百度地图的经纬度,在网上找了很多的资料没找到,只找到了处理单个文件的代码,所以,自己将处理单个文件的代码改了改,现在可以处理多个文件。代码很粗糙,但是可以用。

处理单个文件作者的地址:https://blog.csdn.net/u012967763/article/details/51317363

#include<stdio.h>  

#include<io.h>  
#include<vector>
#include<iostream>
#include<string>
#include <fstream>
#include <iomanip>
#include <cmath> 
using namespace std;
vector<string> res;
vector<string> res1;
const double pi = 3.14159265358979324;
const double a = 6378245.0;
const double ee = 0.00669342162296594323;
const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;


vector<string> listdir(const string &path)
{


string dir = path;
vector<string> s;
_finddata_t fileDir;
long lfDir;


if ((lfDir = _findfirst(dir.c_str(), &fileDir)) == -1l)
printf("No file is found\n");
else{
do{


string str(fileDir.name);
if (str.find('.') == -1)
s.push_back(str);




} while (_findnext(lfDir, &fileDir) == 0);
}
_findclose(lfDir);
return s;


}


void findfile(const string &str,const string &str1)
{
string s = str;
string b = str1;
vector<string> tmp = listdir(s + "\\*");
vector<string> tmp1 = listdir(b + "\\*");
for (unsigned int i = 0; i<tmp.size(); i++)
{


string temp1 = b + "\\" + tmp1[i];
string temp = s + "\\" + tmp[i];
res.push_back(temp);
res1.push_back(temp1);
findfile(temp,temp1);
}
}


struct Loc
{
double Lon;         //坐标的经度  
double Lat;         //坐标的纬度  
};


double transformLat(double x, double y);     //将GPS坐标转换为google纬度坐标辅助函数  
double transformLon(double x, double y);     //将GPS坐标转换为google经度坐标辅助函数  
Loc bd_encrypt(Loc gg);//将谷歌坐标转换为百度坐标  
Loc transform(Loc gps);//将GPS坐标转换为google地图 


int main()
{



string s = "E:\\data\\last";
string k = "E:\\data1\\last1";
findfile(s, k);
//findfile(s);


for (unsigned int i = 0; i < res.size(); i++)
{
cout << res.size() << endl;
cout << res1[i] << endl;
cout << res[i] << endl;
ifstream infile1;
infile1.open(res[i]+"\\all.data", std::ifstream::in);//此处默认的文件打开方式为“以输出的方式打开”

Loc demo, gg, bd;


double ac, b;

int aa = 0;


double Pattern[4508][2];
//char s;
ofstream mycout;
mycout.open(string(res1[i])+"\\all.data", std::ofstream::out);
while (infile1 >> Pattern[aa][0] >> Pattern[aa][1])
{
ac = Pattern[aa][0];
b = Pattern[aa][1];
cout << ac << " " << b << endl;
demo.Lat = b;
demo.Lon = ac;
gg = transform(demo);
cout << gg.Lat << "     " << gg.Lon << endl;




bd = bd_encrypt(gg);
cout << bd.Lat << "     " << bd.Lon << endl;
mycout << setprecision(10) << "[" << bd.Lon << "," << bd.Lat << "]" << "," << endl;
aa++;








}

infile1.close();
mycout.close();


}
getchar();
}
double transformLat(double x, double y)
{
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 *sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
return ret;
}


double transformLon(double x, double y)
{
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}


Loc bd_encrypt(Loc gg)//将谷歌坐标转换为百度坐标  
{
Loc bd;
double x = gg.Lon, y = gg.Lat;
double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
bd.Lon = z * cos(theta) + 0.0065;
bd.Lat = z * sin(theta) + 0.006;
return bd;
}


Loc transform(Loc gps)
{
Loc gg;
double dLat = transformLat(gps.Lon - 105.0, gps.Lat - 35.0);
double dLon = transformLon(gps.Lon - 105.0, gps.Lat - 35.0);
double radLat = gps.Lat / 180.0 * pi;
double magic = sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
gg.Lat = gps.Lat + dLat;
gg.Lon = gps.Lon + dLon;
return gg;
}

猜你喜欢

转载自blog.csdn.net/ytuchenkai/article/details/80734258