You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.5 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "stdafx.h"
#include "angle.h"
#include "math.h"
#define _USE_MATH_DEFINES // 使用math.h中的M_PI宏定义需要
const double EPSILON=1.0E-12;
const double PI = M_PI;
Angle::Angle(void)
{
}
Angle::~Angle(void)
{
}
//度 转 度分秒
void Angle::DegtoDms(double deg, int &d, int &m, double &s)
{
d = int(deg + EPSILON);//整数部分度
double dTmp = (deg - d) * 60;//小数部分转换成分
m = int(dTmp+ EPSILON);//取分的整数部分
s = (dTmp - m) * 60;//截取秒
}
//角度 转弧度
void Angle::DegtoRad(double deg, double &rad)
{
rad = deg * PI/180;
}
//角度 转 十进制度
void Angle::DegtodDeg(double deg, int &d, double &m)
{
d = int(deg + EPSILON);//整数部分度
m = (deg - d) * 60;//小数部分转换成分
}
// 度分秒 转 度
void Angle::DmstoDeg(int d, int m, double s, double &deg)
{
deg = d + (m / 60.0) + (s / 3600.0);
}
//度分秒 转 十进制度
void Angle::DmstodDeg(int d, int m, double s, int &dDeg, double &dm)
{
dDeg = d;
dm = m + s/60;
}
string Angle::DoubleToString(double num,int precision)
{
char tmp[10];
sprintf_s(tmp,"%d",precision);
string str_p = tmp;
string format = "%." + str_p + "f";
char buffer[20];
sprintf_s(buffer,format.data(),num);
string str = buffer;
return str;
}
string Angle::IntToString(int num)
{
char tmp[100];
sprintf_s(tmp,"%d",num);
string str = tmp;
return str;
}
//字符串分割函数
vector<string> Angle::split(string str, string pattern)
{
/*
string::size_type pos;
vector<string> result;
str += pattern;//扩展字符串以方便操作
int size = str.size();
for (int i = 0; i < size; i++)
{
pos = str.find(pattern, i);
if (pos < size)
{
string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;*/
char *ptr;
char *buf;
ptr = strtok_s((char*)str.data(),pattern.data(),&buf);
string tmp;
vector<string> vec_str;
while(ptr != NULL)
{
tmp = ptr;
vec_str.push_back(tmp);
ptr = strtok_s(NULL, pattern.data(), &buf);
}
return vec_str;
}
// wstring=>string
std::string Angle::WString2String(const std::wstring &ws)
{
std::string strLocale = setlocale(LC_ALL, "");
const wchar_t *wchSrc = ws.c_str();
size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1;
char *chDest = new char[nDestSize];
memset(chDest, 0, nDestSize);
wcstombs(chDest, wchSrc, nDestSize);
std::string strResult = chDest;
delete[] chDest;
setlocale(LC_ALL, strLocale.c_str());
return strResult;
}
// string => wstring
std::wstring Angle::String2WString(const std::string &s)
{
std::string strLocale = setlocale(LC_ALL, "");
const char *chSrc = s.c_str();
size_t nDestSize = mbstowcs(NULL, chSrc, 0) + 1;
wchar_t *wchDest = new wchar_t[nDestSize];
wmemset(wchDest, 0, nDestSize);
mbstowcs(wchDest, chSrc, nDestSize);
std::wstring wstrResult = wchDest;
delete[] wchDest;
setlocale(LC_ALL, strLocale.c_str());
return wstrResult;
}
void Angle::getDMSString(double lon,double lat,string &str_lon,string &str_lat)
{
std::string dSymbol = "°";
std::string mSymbol = "";
std::string sSymbol = "\"";
int lon_d,lon_m,lat_d,lat_m;
double lon_s,lat_s;
DegtoDms(lon,lon_d,lon_m,lon_s);
DegtoDms(lat,lat_d,lat_m,lat_s);
string str_lat_s = DoubleToString(lat_s,4);
string str_lon_s = DoubleToString(lon_s,4);
str_lon = IntToString(lon_d) + dSymbol + IntToString(lon_m) + mSymbol + str_lon_s + sSymbol;
str_lat = IntToString(lat_d) + dSymbol + IntToString(lat_m) + mSymbol + str_lat_s + sSymbol;
}