GPSParam.cpp
资源名称:GPS_test.rar [点击查看]
上传用户:fudaml
上传日期:2013-05-28
资源大小:211k
文件大小:6k
源码类别:
GPS编程
开发平台:
Visual C++
- // GPSParam.cpp: implementation of the GPSParam class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "GPSParam.h"
- #include "math.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- GPSParam::GPSParam(char str[],int length)
- {
- // char str[200];
- // memset( str,0,200 );
- SetParam(str,length);
- /*
- m_IsOK=false;
- if( length<30 ) //长度不够直接退出
- {
- m_IsOK=false;
- return;
- }
- int sectionID=0;
- for( int i=0; i<length; i++)
- {
- switch(str[i])
- {
- case '$':
- sectionID = 0;
- break;
- case ',':
- sectionID ++;
- break;
- case 10: //指令读完
- i=length;
- break;
- default:
- Data[ sectionID ] += str[i];
- }
- }
- */
- }
- GPSParam::~GPSParam()
- {
- }
- bool GPSParam::DataTransform()
- {
- m_name=Data[0];
- if(m_name == "GPRMC" && Data[2] == "A")
- {
- //下面提取出时间 经纬度 日期等
- char buff[20];
- CString str;
- //抽取时分秒
- if(Data[1].GetLength() !=9) //时间错误,
- return false;
- strcpy( buff,Data[1] );
- str.Format( "%c%c",buff[0],buff[1] ); //小时
- m_time.hour=atoi( str );
- str.Format( "%c%c",buff[2],buff[3] ); //分
- m_time.mimute=atoi( str );
- str.Format( "%c%c%c%c%c",buff[4],buff[5],buff[6],buff[7],buff[8] ); //秒
- m_time.second = atof( str );
- //抽取日期
- if( Data[9].GetLength() != 6) //日期错误
- return false;
- strcpy( buff,Data[9] );
- str.Format( "%c%c",buff[0],buff[1] );
- m_date.day = atoi( str );
- str.Format("%c%c",buff[2],buff[3] );
- m_date.month = atoi( str );
- str.Format("%c%c",buff[4],buff[5] );
- m_date.year = atoi( str );
- //抽取纬度
- if( Data[3].GetLength() != 10) //纬度错误
- return false;
- strcpy( buff,Data[3] );
- str.Format( "%c%c",buff[0],buff[1] );
- m_latitude = atoi( str );
- str.Format("%c%c%c%c%c%c%c%c",buff[2],buff[3],buff[4],buff[5],buff[6],buff[7],buff[8],buff[9]);
- m_latitude = m_latitude +atof( str )/60.0;
- if( Data[4] == "S" )
- m_latitude *=-1;
- //抽取经度
- strcpy( buff , Data[5] );
- if( Data[5].GetLength() == 10 )
- {
- str.Format("%c%c",buff[0], buff[1] );
- m_longtitude = atoi( str );
- str.Format("%c%c%c%c%c%c%c%c",buff[2],buff[3],buff[4],buff[5],buff[6],buff[7],buff[8],buff[9]);
- m_longtitude = m_longtitude +atof( str )/60.0;
- }
- else if( Data[5].GetLength() == 11 )
- {
- str.Format("%c%c%c",buff[0], buff[1] ,buff[2] );
- m_longtitude = atoi( str );
- str.Format("%c%c%c%c%c%c%c%c",buff[3],buff[4],buff[5],buff[6],buff[7],buff[8],buff[9],buff[10] );
- m_longtitude = m_longtitude +atof( str )/60.0;
- }
- else //经度错误
- return false;
- if( Data[6] == "W" )
- m_longtitude *= -1;
- GpsPoint p;
- p.SetBL(m_latitude,m_longtitude);
- p.BLtoXY();
- X=p.X;
- Y=p.Y;
- m_IsOK=true;
- return true;
- }
- else
- return false;
- return false;
- }
- GPSParam::GPSParam()
- {
- m_IsOK=false;
- }
- GpsPoint::GpsPoint()
- {
- L0=117+20.0/60.0;
- }
- GpsPoint::~GpsPoint()
- {
- }
- bool GpsPoint::SetXY(double x0, double y0)
- {
- if (true) //判断合法性
- {
- X=x0;
- Y=y0;
- return true;
- }
- }
- bool GpsPoint::SetBL(double b0, double l0)
- {
- if( b0>-90 && b0<90 && l0>-180 && l0<180 ) //判断经纬度的合法性
- {
- B=b0;
- L=l0;
- return true;
- }
- else
- return false;
- }
- bool GpsPoint::BLtoXY()
- {
- double l,t,ita2,m0,N;
- double Temp1,Temp2,Temp3,Temp4,Temp5,Temp6,Temp7,Temp8;
- double PAI=3.1415926535898;
- double a=6378245.0;
- double e2=0.00669342162297;
- double el2=0.00673852541468;
- double p2=3600.0*180.0/PAI;
- double P0=PAI/180.0;
- //54坐标系常熟
- double C0=6367558.49686;
- double C1=32005.79642;
- double C2=133.86115;
- double C3=0.7031;
- double temp1,temp2,temp3,temp4;//temp5;
- //高斯变换、
- l=(L-L0)*3600; //??????????
- t=tan(B*P0);
- temp1=t*t;
- ita2=el2*cos(B*P0)*cos(B*P0);
- temp2=ita2*ita2;
- N=a/sqrt(1-e2*sin(B*P0) *sin(B*P0));
- m0=l*cos(B*P0)/p2; //????????
- temp3=m0*m0;
- temp4=temp3*temp3;
- Temp1=N*m0;
- Temp2=C0*B*P0;
- Temp5=sin(B*P0) * sin(B*P0);
- Temp3=cos(B*P0)*sin(B*P0)*(C1+C2*Temp5+C3*Temp5*Temp5);
- Temp4=1.0/2.0*N*t*temp3;
- Temp5=1/24.0*(5.0-(temp1*temp1)+9*ita2+4*temp2*temp2 )*N*t*temp4;
- Temp6=1/720.0*(61-58*temp1+temp1*temp1)*N*t*temp3*temp4;
- Temp7=1/6.0*( 1-temp1+ita2 )*N*m0*temp3;
- Temp8=1/120.0*(5-18*temp1+temp1*temp1+14*temp2-58*ita2*temp1)*N*m0*temp4;
- X=Temp2+Temp3+Temp4+Temp5+Temp6;
- Y=500000+Temp1+Temp7+Temp8;
- return true;
- }
- bool GpsPoint::XYtoBL()
- {
- return true;
- }
- bool GpsPoint::BLtoXY_2()
- {
- double l,t,ita2,m0,N;
- double Temp1,Temp2,Temp3,Temp4,Temp5,Temp6;
- double PAI=3.1415926535898;
- double a=6378245.0;
- double e2=0.00669342162297;
- double el2=0.00673852541468;
- double p2=3600.0*180.0/PAI;
- double P0=PAI/180.0;
- //54坐标系常熟
- double C0=6367558.49686;
- double C1=32005.79642;
- double C2=133.86115;
- double C3=0.7031;
- //高斯变换、
- l=(L-L0); //??????????
- t=tan(B*P0);
- double t2=t*t;
- ita2=el2*cos(B*P0)*cos(B*P0);
- double ita4=ita2*ita2;
- N=a/sqrt(1-e2*sin(B*P0) *sin(B*P0));
- m0=l*cos(B*P0)/p2; //????????
- double m02=m0*m0;
- double m04=m02*m02;
- Temp1=sin(B*P0) * sin(B*P0);
- double X0
- =C0*B*P0+cos(B*P0)*sin(B*P0)*(C1+C2*Temp1+C3*Temp1*Temp1);
- Temp2=1.0/2.0*N*t*m02;
- Temp3=1/24.0*(5.0-(t2*t2)+9*ita2+4*ita4 )*N*t*m04;
- Temp4=1/720.0*(61-58*t2+t2*t2)*N*t*m02*m04;
- Temp5=1/6.0*( 1-t2+ita2 )*N*m0*m02;
- Temp6=1/120.0*(5-18*t2+t2*t2+14*ita2-58*ita2*t2)*N*m0*m04;
- X=X0+Temp1+Temp3+Temp4;
- Y=N*m0+Temp5+Temp6+500000;
- return true;
- }
- bool GPSParam::SetParam(char str[], int length)
- {
- m_IsOK=false;
- if( length<30 ) //长度不够直接退出
- {
- m_IsOK=false;
- return false;
- }
- int sectionID=0;
- for( int i=0; i<length; i++)
- {
- switch(str[i])
- {
- case '$':
- sectionID = 0;
- break;
- case ',':
- sectionID ++;
- break;
- case 10: //指令读完
- i=length;
- break;
- default:
- Data[ sectionID ] += str[i];
- }
- }
- return true;
- }
- bool GPSParam::IsOk()
- {
- return m_IsOK;
- }