BJ54坐标或者XA80坐标到WGS84坐标的转换(小工具附代码)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/bit_kaki/article/details/79374238

前几天自我总结时候发现了之前给我姐写的一个winform的小程序,用来简单进行BJ54坐标到WGS84坐标的转换。

觉得挺有意思的,就贴出来分享一下。

PS:只用了简单的高斯变换,没有四、七参数的变化,所以转出来还是有较大误差。不过可以判断出大概位置

具体说明如下:

1.打开程序,主界面如下,可以进行BJ54坐标系的XY坐标到WGS84经纬度坐标的单点或者多点转换。

2.输入带号和X,Y坐标,点击单点转换,即可进行坐标的单点转换。

3.新建一个低版本(.xls格式)的excel表,将X,Y坐标输入excel表,保存。

  

4.在程序里先输入带号,然后点击多点转换,选择对应的excel表点击打开。

5.程序会自动计算坐标,完成会提示另存为,此时选择将计算后新生成表保存即可。

6.找到新生成的表,里面就是对应的经纬度坐标了。


具体的代码如下:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            double a = double.Parse(textBox1.Text);
            double b = double.Parse(textBox2.Text);
            double f = double.Parse(textBox5.Text);
            double c=0;
            double d = 0;
            GaussProjInvCal(a, b, f,ref c, ref d);
            textBox3.Text = c.ToString();
            textBox4.Text = d.ToString();

        }
        void GaussProjInvCal(double X, double Y, double daihao,ref double longitude, ref double latitude)
        {
            int ProjNo; ////带宽
            double longitude1, latitude1, longitude0, X0, Y0, xval, yval;
            double e1, e2, f, a, ee, NN, T, C, M, D, R, u, fai, iPI;
            iPI = 0.0174532925199433; ////3.1415926535898/180.0;
            a = 6378245.0; f = 1.0 / 298.3; //54年北京坐标系参数
            ////a=6378140.0; f=1/298.257; //80年西安坐标系参数
            ProjNo = (int)(X / 1000000L); //查找带号
            longitude0 = daihao*3;
            longitude0 = longitude0 * iPI; //中央经线
            X0 = ProjNo * 1000000L + 500000L;
            Y0 = 0;
            xval = X - X0; yval = Y - Y0; //带内大地坐标
            e2 = 2 * f - f * f;
            e1 = (1.0 - Math.Sqrt(1 - e2)) / (1.0 + Math.Sqrt(1 - e2));
            ee = e2 / (1 - e2);
            M = yval;
            u = M / (a * (1 - e2 / 4 - 3 * e2 * e2 / 64 - 5 * e2 * e2 * e2 / 256));
            fai = u + (3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * Math.Sin(2 * u) + (21 * e1 * e1 / 16 - 55 * e1 * e1 * e1 * e1 / 32) * Math.Sin(
            4 * u)
            + (151 * e1 * e1 * e1 / 96) * Math.Sin(6 * u) + (1097 * e1 * e1 * e1 * e1 / 512) * Math.Sin(8 * u);
            C = ee * Math.Cos(fai) * Math.Cos(fai);
            T = Math.Tan(fai) * Math.Tan(fai);
            NN = a / Math.Sqrt(1.0 - e2 * Math.Sin(fai) * Math.Sin(fai));
            R = a * (1 - e2) / Math.Sqrt((1 - e2 * Math.Sin(fai) * Math.Sin(fai)) * (1 - e2 * Math.Sin(fai) * Math.Sin(fai)) * (1 - e2 * Math.Sin
            (fai) * Math.Sin(fai)));
            D = xval / NN;
            //计算经度(Longitude) 纬度(Latitude)
            longitude1 = longitude0 + (D - (1 + 2 * T + C) * D * D * D / 6 + (5 - 2 * C + 28 * T - 3 * C * C + 8 * ee + 24 * T * T) * D
            * D * D * D * D / 120) / Math.Cos(fai);
            latitude1 = fai - (NN * Math.Tan(fai) / R) * (D * D / 2 - (5 + 3 * T + 10 * C - 4 * C * C - 9 * ee) * D * D * D * D / 24
            + (61 + 90 * T + 298 * C + 45 * T * T - 256 * ee - 3 * C * C) * D * D * D * D * D * D / 720);
            //转换为度 DD
            longitude = longitude1 / iPI;
            latitude = latitude1 / iPI;
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string filepath = "";
                OpenFileDialog opf = new OpenFileDialog();
                if (opf.ShowDialog() == DialogResult.OK)
                    filepath = opf.FileName;
                DataSet ds = new DataSet();
                ds = ExcelToDS(filepath);
                DataTable dt = ds.Tables["table1"];
                foreach (DataRow dr in dt.Rows)
                {
                    double a = double.Parse(dr[0].ToString());
                    double b = double.Parse(dr[1].ToString());
                    double f = double.Parse(textBox5.Text);
                    double c = 0;
                    double d = 0;
                    GaussProjInvCal(a, b, f, ref c, ref d);
                    dr[0] = c.ToString();
                    dr[1] = d.ToString();

                }
                DataGridViewExportToExcel(ds, "坐标excel");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
            }
        }
        public DataSet ExcelToDS(string Path)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            DataSet ds = null;
            strExcel = "select * from [sheet1$]";
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            ds = new DataSet();
            myCommand.Fill(ds, "table1");
            return ds;
        } 
        public void DataGridViewExportToExcel(DataSet ds, string strTitle)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = false;
            saveFileDialog.FileName = strTitle + ".xls";
            if (saveFileDialog.ShowDialog() == DialogResult.Cancel) //导出时,点击【取消】按钮
            {
                return;
            }
            Stream myStream = saveFileDialog.OpenFile();
            StreamWriter sw = new StreamWriter(myStream,       System.Text.Encoding.GetEncoding(-0));
            string strHeaderText = "";
            try
            {
                //写标题
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    if (i > 0)
                    {
                        strHeaderText += "\t";
                    }
                    strHeaderText += ds.Tables[0].Columns[i].ToString();
                }
                sw.WriteLine(strHeaderText);
                //写内容
                string strItemValue = "";
                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    strItemValue = "";
                    for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
                    {
                        if (k > 0)
                        {
                            strItemValue += "\t";
                        }
                        strItemValue += ds.Tables[0].Rows[j][k].ToString();
                    }
            sw.WriteLine(strItemValue); //把dgv的每一行的信息写为sw的每一行
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
            }
            finally
            {
                sw.Close();
                myStream.Close();
            }
        }
    }
    }


展开阅读全文

没有更多推荐了,返回首页