GPS數據解析 數據拆分 座標轉換 顯示線路圖源代碼

 

  1. GPS數據提取解析源碼GPS source data extraction analysis, we can refer to learn from  
  2.   
  3.   
  4. GPS數據解析   
  5.   
  6. 數據拆分 \座標轉換 \顯示線路圖\源代碼  
  7.   
  8. 逐條讀取gps數據 然後進行拆分 解析,座標轉換,繪製線路。。。很好的示例多多交流學習。。  
  9.   
  10.    
  11.   
  12.  本程序是基於VC++ 建立的單文檔工程。  
  13.   
  14.    
  15.   
  16. 廢話少說,直接上代碼  
  17.   
  18.    
  19.   
  20. //獲取子字符串個數  
  21.   
  22. int GetSubStrCount(CString str,char cFlag)  
  23.   
  24. {  
  25.   
  26.        cFlag = ',';  
  27.   
  28.        int i = 0;  
  29.   
  30.        BOOL isHas = FALSE;  
  31.   
  32.    
  33.   
  34.        for (int iStart = -1; -1 != (iStart = str.Find(cFlag,iStart+1)) ; i++)  
  35.   
  36.        {  
  37.   
  38.               isHas = TRUE;  
  39.   
  40.        }  
  41.   
  42.          
  43.   
  44.        if (!isHas)  
  45.   
  46.        {  
  47.   
  48.               return 0;  
  49.   
  50.        }  
  51.   
  52.        else  
  53.   
  54.        {  
  55.   
  56.               return i+1;  
  57.   
  58.        }  
  59.   
  60. }  
  61.   
  62.    
  63.   
  64. //獲取子字符串  
  65.   
  66. // i 序號 0  
  67.   
  68. CString GetSubStr(CString str,int i,char cFlag)  
  69.   
  70. {  
  71.   
  72.        cFlag = ',';  
  73.   
  74.        int iStart = -1;  
  75.   
  76.        int iEnd = 0;  
  77.   
  78.        int j = 0;  
  79.   
  80.        int iStrCount;  
  81.   
  82.    
  83.   
  84.        iStrCount = GetSubStrCount(str,cFlag);  
  85.   
  86.    
  87.   
  88.        if (i>iStrCount -1 || i<0)  
  89.   
  90.        {  
  91.   
  92.               str = "";  
  93.   
  94.               return str;  
  95.   
  96.        }  
  97.   
  98.        else  
  99.   
  100.        {  
  101.   
  102.               //do nothing  
  103.   
  104.        }  
  105.   
  106.    
  107.   
  108.        if (i == iStrCount-1)  
  109.   
  110.        {  
  111.   
  112.               i = iStrCount;  
  113.   
  114.    
  115.   
  116.               for (;j<i-1;j++)  
  117.   
  118.               {  
  119.   
  120.                      iStart = str.Find(cFlag , iStart+1);  
  121.   
  122.               }  
  123.   
  124.    
  125.   
  126.               return   str.Mid(iStart+1 , str.GetLength()-iStart-1);   
  127.   
  128.        }  
  129.   
  130.        else  
  131.   
  132.        {  
  133.   
  134.               //do nothing  
  135.   
  136.        }  
  137.   
  138.    
  139.   
  140.        for (; j<i; j++)  
  141.   
  142.        {  
  143.   
  144.               iStart = str.Find(cFlag , iStart+1);  
  145.   
  146.        }  
  147.   
  148.    
  149.   
  150.        iEnd = str.Find(cFlag , iStart+1);    
  151.   
  152.        return str.Mid(iStart+1 , iEnd-iStart-1);   
  153.   
  154. }  
  155.   
  156.    
  157.   
  158. //數據解析  
  159.   
  160. CString CGpsDataView::Analyzing(CString str)  
  161.   
  162. {  
  163.   
  164.        CString subStr[20];  
  165.   
  166.        char cFlag = ',';  
  167.   
  168.        int j = GetSubStrCount(str,cFlag);          //得到該行的子字符串個數  
  169.   
  170.        CStdioFile wFile;  
  171.   
  172.        wFile.Open("save.txt",CFile::modeCreate | CFile::modeWrite | CFile::typeText);//將數據寫入文件  
  173.   
  174.    
  175.   
  176.        for (int i=0;i<j;i++)  
  177.   
  178.        {  
  179.   
  180.               subStr[i] = GetSubStr(str,i,cFlag);                                              
  181.   
  182.        }  
  183.   
  184.    
  185.   
  186.        //GPGGA數據  
  187.   
  188.        if (subStr[0] == "$GPGGA")  
  189.   
  190.        {  
  191.   
  192.               CoordCovert(subStr[2],subStr[4]);  
  193.   
  194.               //提取時間  
  195.   
  196.               subStr[1].Insert(2,':');  
  197.   
  198.               subStr[1].Insert(5,':');  
  199.   
  200.               subStr[1].Insert(0," UTC時間:");  
  201.   
  202.    
  203.   
  204.               //提取緯度  
  205.   
  206.               if (subStr[3] == 'N')  
  207.   
  208.               {  
  209.   
  210.                      subStr[2].Insert(11,"分");  
  211.   
  212.                      subStr[2].Insert(2,"度");  
  213.   
  214.                      subStr[2].Insert(0,"  北緯");  
  215.   
  216.               }  
  217.   
  218.               else if (subStr[3] == 'S')  
  219.   
  220.               {  
  221.   
  222.                      subStr[2].Insert(11,"分");  
  223.   
  224.                      subStr[2].Insert(2,"度");  
  225.   
  226.                      subStr[2].Insert(0,"  南緯");  
  227.   
  228.               }  
  229.   
  230.    
  231.   
  232.               //提取經度  
  233.   
  234.               if (subStr[5] == 'E')  
  235.   
  236.               {  
  237.   
  238.                      subStr[4].Insert(12,"分");  
  239.   
  240.                      subStr[4].Insert(3,"度");  
  241.   
  242.                      subStr[4].Insert(0,"  東經");  
  243.   
  244.               }  
  245.   
  246.               else if (subStr[5] == 'W')  
  247.   
  248.               {  
  249.   
  250.                      subStr[4].Insert(12,"分");  
  251.   
  252.                      subStr[4].Insert(3,"度");  
  253.   
  254.                      subStr[4].Insert(0,"  西經");  
  255.   
  256.               }  
  257.   
  258.                 
  259.   
  260.               //判斷GPS狀態  
  261.   
  262.               CString GpsState;  
  263.   
  264.    
  265.   
  266.               if (subStr[6] == '0')  
  267.   
  268.               {  
  269.   
  270.                      GpsState = "  GPS狀態:無定位.";  
  271.   
  272.               }  
  273.   
  274.               else if (subStr[6] == '1')  
  275.   
  276.               {  
  277.   
  278.                      GpsState = "  GPS狀態:無差分校正定位.";  
  279.   
  280.               }  
  281.   
  282.               else if (subStr[6] == '2')  
  283.   
  284.               {  
  285.   
  286.                      GpsState = "  GPS狀態:差分校正定位.";  
  287.   
  288.               }  
  289.   
  290.               else if (subStr[6] == '9')  
  291.   
  292.               {  
  293.   
  294.                      GpsState = "  GPS狀態:用星曆計算定位.";  
  295.   
  296.               }  
  297.   
  298.    
  299.   
  300.               //提取衛星數  
  301.   
  302.               subStr[7].Insert(0," 衛星數:");         
  303.   
  304.    
  305.   
  306.               //提取平面位置精度因子  
  307.   
  308.               subStr[8].Insert(0,"  平面位置精度因子:");             
  309.   
  310.    
  311.   
  312.               //天線海拔高度  
  313.   
  314.               subStr[9].Insert(strlen(subStr[9]),subStr[10]);  
  315.   
  316.               subStr[9].Insert(0," 天線海拔高度:");  
  317.   
  318.    
  319.   
  320.               //海平面分離度  
  321.   
  322.               subStr[11].Insert(strlen(subStr[11]),subStr[12]);  
  323.   
  324.               subStr[11].Insert(0," 海平面分離度:");  
  325.   
  326.    
  327.   
  328.               subStr[0] += subStr[1];  
  329.   
  330.               subStr[0] += subStr[2];  
  331.   
  332.               subStr[0] += subStr[4];  
  333.   
  334.               subStr[0] += GpsState;  
  335.   
  336.               subStr[0] += subStr[7];  
  337.   
  338.               subStr[0] += subStr[8];  
  339.   
  340.               subStr[0] += subStr[9];  
  341.   
  342.               subStr[0] += subStr[11];  
  343.   
  344.               //////////////////////////////////////MessageBox(subStr[0]);  
  345.   
  346.               wFile.WriteString(subStr[0]);//將數據寫入文件  
  347.   
  348.    
  349.   
  350.        }  
  351.   
  352.    
  353.   
  354.        //GPZDA數據  
  355.   
  356.        else if (subStr[0] == "$GPZDA")  
  357.   
  358.        {  
  359.   
  360.               //提取時間  
  361.   
  362.               subStr[1].Insert(2,':');  
  363.   
  364.               subStr[1].Insert(5,':');  
  365.   
  366.               subStr[1].Insert(0," UTC時間:");  
  367.   
  368.    
  369.   
  370.               //提取日期  
  371.   
  372.               subStr[2].Insert(strlen(subStr[2]),"日");  
  373.   
  374.               subStr[2].Insert(0,"月");  
  375.   
  376.               subStr[2].Insert(0,subStr[3]);  
  377.   
  378.               subStr[2].Insert(0,"年");  
  379.   
  380.               subStr[2].Insert(0,subStr[4]);    
  381.   
  382.               subStr[2].Insert(0,' ');  
  383.   
  384.    
  385.   
  386.               //當地時域描述  
  387.   
  388.               subStr[5].Insert(strlen(subStr[5]),"小時");  
  389.   
  390.    
  391.   
  392.               if (strlen(subStr[6]) > 3)  
  393.   
  394.               {  
  395.   
  396.                      subStr[6] = subStr[6].Left(2);  
  397.   
  398.               }  
  399.   
  400.               else  
  401.   
  402.               {  
  403.   
  404.                      subStr[6] = '0';  
  405.   
  406.               }  
  407.   
  408.    
  409.   
  410.               subStr[6] += "分";  
  411.   
  412.               subStr[6].Insert(0,subStr[5]);  
  413.   
  414.               subStr[6].Insert(0," 當地時域:");  
  415.   
  416.    
  417.   
  418.               subStr[0] += subStr[1];  
  419.   
  420.               subStr[0] += subStr[2];  
  421.   
  422.               subStr[0] += subStr[6];  
  423.   
  424.               //////////////////////////////MessageBox(subStr[0]);  
  425.   
  426.               wFile.WriteString(subStr[0]);//將數據寫入文件  
  427.   
  428.        }  
  429.   
  430.    
  431.   
  432.        //GPGSA數據  
  433.   
  434.        else if (subStr[0] == "$GPGSA")  
  435.   
  436.        {  
  437.   
  438.               //衛星捕獲模式,以及定位模式  
  439.   
  440.               CString CatchLocation;  
  441.   
  442.    
  443.   
  444.               if (subStr[1] == 'M')  
  445.   
  446.               {  
  447.   
  448.                      if (subStr[2] == '1')  
  449.   
  450.                      {  
  451.   
  452.                             CatchLocation = " 手動捕獲衛星,未定位!";  
  453.   
  454.                      }  
  455.   
  456.                      else if (subStr[2] == '2')  
  457.   
  458.                      {  
  459.   
  460.                             CatchLocation = "  手動捕獲衛星,2D定位!";  
  461.   
  462.                      }  
  463.   
  464.                      else if (subStr[2] == '3')  
  465.   
  466.                      {  
  467.   
  468.                             CatchLocation = "  手動捕獲衛星,3D定位!";  
  469.   
  470.                      }  
  471.   
  472.               }  
  473.   
  474.               else if (subStr[1] == 'A')  
  475.   
  476.               {  
  477.   
  478.                      if (subStr[2] == '1')  
  479.   
  480.                      {  
  481.   
  482.                             CatchLocation ="  自動捕獲衛星,未定位!";  
  483.   
  484.                      }  
  485.   
  486.                      else if (subStr[2] == '2')  
  487.   
  488.                      {  
  489.   
  490.                             CatchLocation ="  自動捕獲衛星,2D定位!";  
  491.   
  492.                      }  
  493.   
  494.                      else if (subStr[2] == '3')  
  495.   
  496.                      {  
  497.   
  498.                             CatchLocation ="  自動捕獲衛星,3D定位!";  
  499.   
  500.                      }  
  501.   
  502.               }  
  503.   
  504.    
  505.   
  506.               //各衛星定位結果  
  507.   
  508.               subStr[3].Insert(0,"  各衛星定位結果:");  
  509.   
  510.               subStr[3] += ' ';  
  511.   
  512.               subStr[4].Insert(0,subStr[3]);  
  513.   
  514.               subStr[4] += ' ';  
  515.   
  516.               subStr[5].Insert(0,subStr[4]);  
  517.   
  518.               subStr[5] += ' ';  
  519.   
  520.               subStr[6].Insert(0,subStr[5]);  
  521.   
  522.               subStr[6] += ' ';  
  523.   
  524.               subStr[7].Insert(0,subStr[6]);  
  525.   
  526.               subStr[7] += ' ';  
  527.   
  528.               subStr[8].Insert(0,subStr[7]);  
  529.   
  530.               subStr[8] += ' ';  
  531.   
  532.               subStr[9].Insert(0,subStr[8]);  
  533.   
  534.               subStr[9] += ' ';  
  535.   
  536.               subStr[10].Insert(0,subStr[9]);  
  537.   
  538.               subStr[10] += ' ';  
  539.   
  540.               subStr[11].Insert(0,subStr[10]);  
  541.   
  542.               subStr[11] += ' ';  
  543.   
  544.               subStr[12].Insert(0,subStr[11]);  
  545.   
  546.               subStr[12] += ' ';  
  547.   
  548.               subStr[13].Insert(0,subStr[12]);  
  549.   
  550.               subStr[13] += ' ';  
  551.   
  552.               subStr[14].Insert(0,subStr[13]);  
  553.   
  554.               subStr[14] += ' ';  
  555.   
  556.    
  557.   
  558.               //空間(三維)位置精度因子  
  559.   
  560.               subStr[15].Insert(0,"  空間(三維)位置精度因子:");  
  561.   
  562.    
  563.   
  564.               //平面位置精度因子  
  565.   
  566.               subStr[16].Insert(0,"   平面位置精度因子:");  
  567.   
  568.    
  569.   
  570.               //高度位置精度因子  
  571.   
  572.               subStr[17] = subStr[17].Left(3);  
  573.   
  574.               subStr[17].Insert(0,"  高度位置精度因子:");  
  575.   
  576.    
  577.   
  578.               subStr[0] += CatchLocation;  
  579.   
  580.               subStr[0] += subStr[14];  
  581.   
  582.               subStr[0] += subStr[15];  
  583.   
  584.               subStr[0] += subStr[16];  
  585.   
  586.               subStr[0] += subStr[17];  
  587.   
  588.               /////////////////////////////MessageBox(subStr[0]);  
  589.   
  590.               wFile.WriteString(subStr[0]);//將數據寫入文件                       
  591.   
  592.        }  
  593.   
  594.    
  595.   
  596.        //GPGSV數據  
  597.   
  598.        else if (subStr[0] == "$GPGSV")  
  599.   
  600.        {  
  601.   
  602.               ///////////////////////////MessageBox(subStr[0]);  
  603.   
  604.    
  605.   
  606.               //衛星編號、衛星仰角(0~90度)、衛星方位角(0~359度)、信噪比  
  607.   
  608.               subStr[4].Insert(0,"衛星編號:");  
  609.   
  610.               subStr[5].Insert(0," 仰角:");  
  611.   
  612.               subStr[6].Insert(0," 方位角:");  
  613.   
  614.               subStr[7].Insert(0," 信噪比:");  
  615.   
  616.               subStr[4] += subStr[5];  
  617.   
  618.               subStr[4] += subStr[6];  
  619.   
  620.               subStr[4] += subStr[7];  
  621.   
  622.               ///////////////////MessageBox(subStr[4]);  
  623.   
  624.               subStr[8].Insert(0,"衛星編號:");  
  625.   
  626.               subStr[9].Insert(0," 仰角:");  
  627.   
  628.               subStr[10].Insert(0," 方位角:");  
  629.   
  630.               subStr[11].Insert(0," 信噪比:");  
  631.   
  632.               subStr[8] += subStr[9];  
  633.   
  634.               subStr[8] += subStr[10];  
  635.   
  636.               subStr[8] += subStr[11];  
  637.   
  638.               ////////////////////////MessageBox(subStr[8]);  
  639.   
  640.               subStr[12].Insert(0,"衛星編號:");  
  641.   
  642.               subStr[13].Insert(0," 仰角:");  
  643.   
  644.               subStr[14].Insert(0," 方位角:");  
  645.   
  646.               subStr[15].Insert(0," 信噪比:");  
  647.   
  648.               subStr[12] += subStr[13];  
  649.   
  650.               subStr[12] += subStr[14];  
  651.   
  652.               subStr[12] += subStr[15];  
  653.   
  654.               /////////////////////MessageBox(subStr[12]);  
  655.   
  656.               subStr[16].Insert(0,"衛星編號:");  
  657.   
  658.               subStr[17].Insert(0," 仰角:");  
  659.   
  660.               subStr[18].Insert(0," 方位角:");  
  661.   
  662.    
  663.   
  664.               if (strlen(subStr[19]) > 3)  
  665.   
  666.               {  
  667.   
  668.                      subStr[19] = subStr[19].Left(2);  
  669.   
  670.               }  
  671.   
  672.               else  
  673.   
  674.               {  
  675.   
  676.                      subStr[19] = '0';  
  677.   
  678.               }  
  679.   
  680.    
  681.   
  682.               subStr[19].Insert(0," 信噪比:");  
  683.   
  684.               subStr[16] += subStr[17];  
  685.   
  686.               subStr[16] += subStr[18];       
  687.   
  688.               subStr[16] += subStr[19];  
  689.   
  690.               /////////////////////////////////MessageBox(subStr[16]);  
  691.   
  692.               wFile.WriteString(subStr[16]);//將數據寫入文件  
  693.   
  694.        }  
  695.   
  696.        return str;  
  697.   
  698. }  
  699.   
  700.    
  701.   
  702.    
  703.   
  704. //讀取文件數據並解析  
  705.   
  706. void CGpsDataView::OnFileRead()  
  707.   
  708. {  
  709.   
  710.        // TODO: 在此添加命令處理程序代碼   
  711.   
  712.        CStdioFile myFile;  
  713.   
  714.        CString oneLine;       
  715.   
  716.        char cFlag = ',';  
  717.   
  718.        CString subStr[20];  
  719.   
  720.          
  721.   
  722.        //讀取GPS數據文件  
  723.   
  724.        if(!myFile.Open(("gps.txt"),CFile::modeRead | CFile::typeText))  
  725.   
  726.        {  
  727.   
  728.               AfxMessageBox(_T("打開文件錯誤!"));  
  729.   
  730.               return;  
  731.   
  732.        }  
  733.   
  734.        else  
  735.   
  736.        {  
  737.   
  738.               /*do nothing*/  
  739.   
  740.        }  
  741.   
  742.    
  743.   
  744.        while (myFile.ReadString(oneLine))//讀一行  
  745.   
  746.        {               
  747.   
  748.               //////////MessageBox(oneLine);  
  749.   
  750.               int j = GetSubStrCount(oneLine,cFlag);         //得到該行的子字符串個數  
  751.   
  752.                 
  753.   
  754.               //校驗  
  755.   
  756.               if(CheckNum(oneLine))  
  757.   
  758.               {  
  759.   
  760.                      ////////////MessageBox(_T("數據校驗...接收正確!..."));  
  761.   
  762.    
  763.   
  764.                      for (int i=0;i<j;i++)  
  765.   
  766.                      {  
  767.   
  768.                             subStr[i] = GetSubStr(oneLine,i,cFlag);  
  769.   
  770.                             //MessageBox(subStr[i]);                             
  771.   
  772.                      }  
  773.   
  774.    
  775.   
  776.                      Analyzing(oneLine);        //解析  
  777.   
  778.               }  
  779.   
  780.               else  
  781.   
  782.               {  
  783.   
  784.                      AfxMessageBox(_T("數據校驗..接收錯誤!..."));  
  785.   
  786.               }  
  787.   
  788.        }  
  789.   
  790.    
  791.   
  792.        myFile.Close();  
  793.   
  794. }  
  795.   
  796.    
  797.   
  798. //***********************************************************************************************  
  799.   
  800. //座標轉換  
  801.   
  802.    
  803.   
  804. //度分秒--弧度  
  805.   
  806. double Dms2Rad(double Dms)   
  807.   
  808. {   
  809.   
  810.        double Degree, Miniute;   
  811.   
  812.        double Second;   
  813.   
  814.        int   Sign;   
  815.   
  816.        double Rad;   
  817.   
  818.    
  819.   
  820.        if(Dms >= 0)   
  821.   
  822.        {  
  823.   
  824.               Sign = 1;   
  825.   
  826.        }  
  827.   
  828.        else   
  829.   
  830.        {  
  831.   
  832.               Sign = -1;   
  833.   
  834.        }  
  835.   
  836.    
  837.   
  838.        Dms = fabs(Dms); //絕對值  
  839.   
  840.        Degree = floor(Dms); // 取度 floor(2.800) = 2.0000  
  841.   
  842.        Miniute = floor(fmod(Dms * 100.0, 100.0)); //fmod 計算餘數  
  843.   
  844.        Second  = fmod(Dms * 10000.0, 100.0);   
  845.   
  846.        Rad = Sign * (Degree + Miniute / 60.0 + Second / 3600.0) * PI / 180.0;   
  847.   
  848.        return Rad;   
  849.   
  850. }  
  851.   
  852.    
  853.   
  854. double Rad2Dms(double Rad)  
  855.   
  856. {  
  857.   
  858.        double Degree, Miniute;   
  859.   
  860.        double Second;   
  861.   
  862.        int   Sign;   
  863.   
  864.        double Dms;  
  865.   
  866.    
  867.   
  868.        if(Rad >= 0)   
  869.   
  870.        {  
  871.   
  872.               Sign = 1;   
  873.   
  874.        }  
  875.   
  876.        else   
  877.   
  878.        {  
  879.   
  880.               Sign = -1;   
  881.   
  882.        }  
  883.   
  884.    
  885.   
  886.        Rad   = fabs(Rad * 180.0 / PI);   
  887.   
  888.        Degree   = floor(Rad);   
  889.   
  890.        Miniute  = floor(fmod(Rad * 60.0, 60.0));   
  891.   
  892.        Second   = fmod(Rad * 3600.0, 60.0);   
  893.   
  894.        Dms   = Sign * (Degree + Miniute / 100.0 + Second / 10000.0);   
  895.   
  896.        return  Dms;   
  897.   
  898. }  
  899.   
  900.    
  901.   
  902. //正算公式  
  903.   
  904. bool   GpsPoint::BL2xy()   
  905.   
  906. {   
  907.   
  908.        //大地測量學基礎 (呂志平 喬書波 北京:測繪出版社 2010.03)  
  909.   
  910.    
  911.   
  912.        double X; //由赤道至緯度爲B的子午線弧長   (P106   5-41)  
  913.   
  914.        double N; //橢球的卯酉圈曲率半徑  
  915.   
  916.        double t;  
  917.   
  918.        double t2;  
  919.   
  920.        double m;  
  921.   
  922.        double m2;  
  923.   
  924.        double ng2;  
  925.   
  926.        double cosB;  
  927.   
  928.        double sinB;     
  929.   
  930.          
  931.   
  932.        X   = A1 * B * 180.0 / PI + A2 * sin(2 * B)    
  933.   
  934.                 + A3 * sin(4 * B) + A4 * sin(6 * B);   
  935.   
  936.    
  937.   
  938.        sinB = sin(B);   
  939.   
  940.        cosB = cos(B);   
  941.   
  942.        t   =  tan(B);   
  943.   
  944.        t2  =  t * t;   
  945.   
  946.    
  947.   
  948.        N   = a /sqrt(1 - e2 * sinB * sinB);   
  949.   
  950.        m   =  cosB * (L - L0);   
  951.   
  952.        m2  = m * m;   
  953.   
  954.        ng2 = cosB * cosB * e2 / (1 - e2);   
  955.   
  956.    
  957.   
  958.        //P156  (6-63公式)  
  959.   
  960.        x   = X + N * t *(( 0.5 + (    (5 - t2 + 9 * ng2 + 4 * ng2 * ng2)   
  961.   
  962.                      / 24.0 + (61 - 58 * t2 + t2 * t2) * m2 / 720.0) * m2)* m2);  
  963.   
  964.    
  965.   
  966.        y   = N * m * ( 1 + m2 * ( (1 - t2 + ng2) / 6.0 + m2 * ( 5 - 18 * t2 + t2 * t2  
  967.   
  968.                      + 14 * ng2 - 58 *  ng2 * t2 ) / 120.0));   
  969.   
  970.    
  971.   
  972.        //y   += 500000;   
  973.   
  974.    
  975.   
  976.        return   true;   
  977.   
  978. }   
  979.   
  980.    
  981.   
  982. //反算公式  
  983.   
  984. bool   GpsPoint::xy2BL()   
  985.   
  986. {   
  987.   
  988.        double sinB;   
  989.   
  990.        double cosB;  
  991.   
  992.        double t;  
  993.   
  994.        double t2;  
  995.   
  996.        double N; //橢球的卯酉圈曲率半徑  
  997.   
  998.        double ng2;  
  999.   
  1000.        double V;  
  1001.   
  1002.        double yN;  
  1003.   
  1004.        double preB0;  
  1005.   
  1006.        double B0;  
  1007.   
  1008.        double eta;              
  1009.   
  1010.        //y   -=  500000;   
  1011.   
  1012.        B0   = x / A1;   
  1013.   
  1014.    
  1015.   
  1016.        do   
  1017.   
  1018.        {   
  1019.   
  1020.               preB0 =  B0;   
  1021.   
  1022.               B0   = B0 * PI / 180.0;   
  1023.   
  1024.               B0   = (x - (A2 * sin(2 * B0) + A3 * sin(4 * B0) + A4 * sin(6 * B0))) / A1;  
  1025.   
  1026.               eta   = fabs(B0 - preB0);   
  1027.   
  1028.        }while(eta > 0.000000001);   
  1029.   
  1030.    
  1031.   
  1032.        B0  = B0 * PI / 180.0;   
  1033.   
  1034.        B   = Rad2Dms(B0);   
  1035.   
  1036.        sinB = sin(B0);   
  1037.   
  1038.        cosB = cos(B0);   
  1039.   
  1040.        t   =  tan(B0);   
  1041.   
  1042.        t2   = t * t;   
  1043.   
  1044.        N   = a / sqrt(1 - e2 * sinB * sinB);   
  1045.   
  1046.        ng2   = cosB * cosB * e2 / (1 - e2);   
  1047.   
  1048.        V   =   sqrt(1 + ng2);   
  1049.   
  1050.        yN   = y / N;   
  1051.   
  1052.    
  1053.   
  1054.        B   = B0 - (yN * yN - (5 + 3 * t2 + ng2 - 9 * ng2 * t2) * yN * yN * yN * yN  
  1055.   
  1056.                      / 12.0 + (61 + 90 * t2 + 45 * t2 * t2) * yN * yN * yN * yN * yN * yN / 360.0)  
  1057.   
  1058.                      * V * V * t / 2;   
  1059.   
  1060.    
  1061.   
  1062.        L   = L0 + (yN - (1 + 2 * t2 + ng2) * yN * yN * yN / 6.0 + (5 + 28 * t2 + 24  
  1063.   
  1064.                      * t2 * t2 + 6 * ng2 + 8 * ng2 * t2) * yN * yN * yN * yN * yN / 120.0) / cosB;  
  1065.   
  1066.        return   true;   
  1067.   
  1068. }   
  1069.   
  1070.    
  1071.   
  1072. //設置中央子午線  
  1073.   
  1074. bool   GpsPoint::SetL0(double dL0)   
  1075.   
  1076. {   
  1077.   
  1078.        L0   =   Dms2Rad(dL0);   
  1079.   
  1080.        return   true;   
  1081.   
  1082. }   
  1083.   
  1084.    
  1085.   
  1086. //將度分秒經緯度轉換爲弧度後再轉換爲平面座標  
  1087.   
  1088. bool   GpsPoint::SetBL(double dB, double dL)   
  1089.   
  1090. {   
  1091.   
  1092.        B   =   Dms2Rad(dB);   
  1093.   
  1094.        L   =   Dms2Rad(dL);    
  1095.   
  1096.        BL2xy();   
  1097.   
  1098.        return   true;   
  1099.   
  1100. }   
  1101.   
  1102.    
  1103.   
  1104. bool   GpsPoint::GetBL(double *dB, double *dL)   
  1105.   
  1106. {   
  1107.   
  1108.        *dB   =   Rad2Dms(B);   
  1109.   
  1110.        *dL   =   Rad2Dms(L);   
  1111.   
  1112.        return   true;   
  1113.   
  1114. }   
  1115.   
  1116.    
  1117.   
  1118. //將平面座標轉換爲(弧度)經緯度  
  1119.   
  1120. bool   GpsPoint::Setxy(double dx, double dy)   
  1121.   
  1122. {   
  1123.   
  1124.        x   =   dx;   
  1125.   
  1126.        y   =   dy;   
  1127.   
  1128.        xy2BL();   
  1129.   
  1130.        return   true;   
  1131.   
  1132. }   
  1133.   
  1134.    
  1135.   
  1136. bool   GpsPoint::Getxy(double *dx, double *dy)   
  1137.   
  1138. {   
  1139.   
  1140.        *dx   =   x;   
  1141.   
  1142.        *dy   =   y;   
  1143.   
  1144.        return   true;   
  1145.   
  1146. }   
  1147.   
  1148.    
  1149.   
  1150. GpsPoint_Krasovsky::GpsPoint_Krasovsky()   
  1151.   
  1152. {   
  1153.   
  1154.        a   =   6378245;                                                   //長半徑  
  1155.   
  1156.        f   =   298.3;                                                               //扁率的倒數 (扁率:(a-b)/a)      
  1157.   
  1158.        e2   =   1 - ((f - 1) / f) * ((f - 1) / f);        //第一偏心率的平方  
  1159.   
  1160.        e12   =   (f / (f - 1)) * (f / (f - 1)) - 1;       //第二偏心率的平方  
  1161.   
  1162.          
  1163.   
  1164.        // 克拉索夫斯基橢球  
  1165.   
  1166.        A1   =   111134.8611;                                             
  1167.   
  1168.        A2   =   -16036.4803;   
  1169.   
  1170.        A3   =   16.8281;   
  1171.   
  1172.        A4   =   -0.0220;   
  1173.   
  1174. }  
  1175.   
  1176.    
  1177.   
  1178. //*************座標轉換  
  1179.   
  1180. bool CGpsDataView::CoordCovert(CString latitude, CString longitude)  
  1181.   
  1182. {  
  1183.   
  1184.        double bbb = atof(latitude);  
  1185.   
  1186.        double lll = atof(longitude);  
  1187.   
  1188.    
  1189.   
  1190.        //度分格式轉換爲度分秒格式  
  1191.   
  1192.        bbb = Dm2Dms(bbb);  
  1193.   
  1194.        lll = Dm2Dms(lll);  
  1195.   
  1196.    
  1197.   
  1198.        double   MyL0 ;   //中央子午線   
  1199.   
  1200.        double   MyB   =   bbb ;    //33 d 44 m 55.6666 s   
  1201.   
  1202.        double   MyL   =   lll ;  //3度帶,109 d 22 m 33.4444 s   
  1203.   
  1204.    
  1205.   
  1206.        //計算當地中央子午線 ,3度帶  
  1207.   
  1208.        MyL0 = fabs(MyL);  
  1209.   
  1210.        MyL0 = floor(MyL);  
  1211.   
  1212.        MyL0 = 3 * floor(MyL0 / 3 );  
  1213.   
  1214.    
  1215.   
  1216.        GpsPoint_Krasovsky  MyPrj;   
  1217.   
  1218.        MyPrj.SetL0(MyL0);   
  1219.   
  1220.        MyPrj.SetBL(MyB,   MyL);                  
  1221.   
  1222.        double   OutMyX;                  
  1223.   
  1224.        double   OutMyY;   
  1225.   
  1226.        OutMyX   =   MyPrj.x;           //正算結果:座標x   
  1227.   
  1228.        OutMyY   =   MyPrj.y;           //結果:座標y   
  1229.   
  1230.    
  1231.   
  1232.        CString strTemp1;  
  1233.   
  1234.        CString strTemp2;  
  1235.   
  1236.        CString strTemp3;  
  1237.   
  1238.        CString strTemp4;  
  1239.   
  1240.    
  1241.   
  1242.        strTemp1.Format("%f",OutMyX);      
  1243.   
  1244.        strTemp2.Format("%f",OutMyY);  
  1245.   
  1246.        strTemp1.Insert(0,"x = ");  
  1247.   
  1248.        strTemp2.Insert(0," , y = ");  
  1249.   
  1250.        strTemp2.Insert(0,strTemp1);  
  1251.   
  1252.        strTemp2.Insert(0," 座標轉換: ");  
  1253.   
  1254.    
  1255.   
  1256.        strTemp3.Format("%f12",MyB);  
  1257.   
  1258.        strTemp4.Format("%f12",MyL);  
  1259.   
  1260.        strTemp3.Insert(0,"B = ");  
  1261.   
  1262.        strTemp4.Insert(0,"  L = ");  
  1263.   
  1264.        strTemp4.Insert(0,strTemp3);  
  1265.   
  1266.        strTemp2.Insert(0,strTemp4);  
  1267.   
  1268.        //MessageBox(strTemp2);  
  1269.   
  1270.    
  1271.   
  1272.        DrawPoint(MyPrj.x,MyPrj.y);  
  1273.   
  1274.    
  1275.   
  1276.        return true;  
  1277.   
  1278. }  
  1279.   
  1280.    
  1281.   
  1282. //==================================  
  1283.   
  1284. //度分格式轉換爲度分秒格式  
  1285.   
  1286. double CGpsDataView::Dm2Dms(double Dm)  
  1287.   
  1288. {  
  1289.   
  1290.        double Dms;  
  1291.   
  1292.        double temp;    
  1293.   
  1294.        temp = Dm - floor(Dm);   
  1295.   
  1296.        temp = (temp * 60) / 100;      
  1297.   
  1298.        Dm   =  floor(Dm);  
  1299.   
  1300.        Dm   += temp;       
  1301.   
  1302.        Dm   =  Dm /100;         
  1303.   
  1304.        Dms  = Dm;  
  1305.   
  1306.        return Dms;  
  1307.   
  1308. }  
  1309.   
  1310.    
  1311.   
  1312.    
  1313.   
  1314.    
  1315.   
  1316.    
  1317.   
  1318. //*************繪製線路  顯示出路線  
  1319.   
  1320. int count1=0;  
  1321.   
  1322. bool bFirst = true;  
  1323.   
  1324. double xTemp;  
  1325.   
  1326. double yTemp;  
  1327.   
  1328. void CGpsDataView::DrawPoint(double X, double Y)  
  1329.   
  1330. {        
  1331.   
  1332.        Sleep(100);  
  1333.   
  1334.    
  1335.   
  1336.        //X = (X - floor(X))*100;  
  1337.   
  1338.        //Y = (Y - floor(Y))*100;  
  1339.   
  1340.    
  1341.   
  1342.        if (bFirst)   
  1343.   
  1344.        {  
  1345.   
  1346.               xTemp=X;  
  1347.   
  1348.               yTemp=Y;  
  1349.   
  1350.               bFirst=false;  
  1351.   
  1352.        }  
  1353.   
  1354.    
  1355.   
  1356.        CDC *pDC=GetDC();  
  1357.   
  1358.        CPen pen(PS_SOLID,3,RGB(255,20,20));  
  1359.   
  1360.        CPen *pOldPen;  
  1361.   
  1362.        CBrush *pOldBrush;  
  1363.   
  1364.        CBrush *pBrush=CBrush::FromHandle( (HBRUSH)GetStockObject(NULL_BRUSH) );  
  1365.   
  1366.    
  1367.   
  1368.        pOldPen=pDC->SelectObject(&pen);  
  1369.   
  1370.        pOldBrush=pDC->SelectObject(pBrush);  
  1371.   
  1372.    
  1373.   
  1374.        int a=(int)(100.0-(X-xTemp)*800.0);  
  1375.   
  1376.        int b=(int)(100.0+(Y-yTemp)*800.0);  
  1377.   
  1378.    
  1379.   
  1380.        //繪製點顯示路徑  
  1381.   
  1382.        pDC->Ellipse(a,b,a+5,b+5);  
  1383.   
  1384.    
  1385.   
  1386.        //計數  
  1387.   
  1388.        count1=count1+1;  
  1389.   
  1390.        pDC->SelectObject( pOldBrush );  
  1391.   
  1392.        pDC->SelectObject( pOldPen );  
  1393.   
  1394.    
  1395.   
  1396.        CString str;  
  1397.   
  1398.        str.Format("%.1f,%.1f,%d,%d,%d",X-xTemp,Y-yTemp,a,b,count1);  
  1399.   
  1400.        pDC->TextOut(10,10,str);  
  1401.   
  1402. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章