经纬度坐标系之间相互转化工具(百度与WGS84、百度与国测局、国测局与WGS)

1.前言

刚刚想从百度坐标拾取工具里面找到一些地点的经纬度,存储到系统中使用,由于百度拾取系统给到的是百度(BD-09)坐标系统,系统统一用到的是WGS-84,所以需要进行一次转换,本来想从网上下载一个,结果花了仅剩不多的49积分,下载到了一个用起来很不方便,而且还报错的东西下来,真TM好气!所以无奈自己编写一个提供给大家使用!

2.实现

2.1.语言及框架说明

我这边是基于C#语言开发的winform桌面应用程序,使用的是.NET Framework 4.6.1

2.2.经纬度坐标系统转换类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoordinateConversionForm
{
    public class CoordinateUtil
    {
        //WGS-84坐标系:全球定位系统使用,GPS、北斗等
        //GCJ-02坐标系:中国地区使用,由WGS-84偏移而来
        //BD-09坐标系:百度专用,由GCJ-02偏移而来
        private static readonly double pi = 3.14159265358979324;
        private static readonly double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        //克拉索天斯基椭球体参数值
        private static readonly double a = 6378245.0;
        //第一偏心率
        private static readonly double ee = 0.00669342162296594323;

        /// <summary>
        /// BD-09转换GCJ-02
        /// </summary>
        /// <param name="bd_lat">纬度</param>
        /// <param name="bd_lon">经度</param>
        /// <returns></returns>
        public static GPSPoint BD09ToGCJ02(double bd_lat, double bd_lon)
        {
            GPSPoint point = new GPSPoint();
            double x = bd_lon - 0.0065, y = bd_lat - 0.006;
            double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
            double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
            double gg_lon = z * Math.Cos(theta);
            double gg_lat = z * Math.Sin(theta);
            point.lat = gg_lat;
            point.lon = gg_lon;
            return point;
        }

        /// <summary>
        /// GCJ-02转WGS84
        /// </summary>
        /// <param name="gcj_lat"></param>
        /// <param name="gcj_lon"></param>
        /// <returns></returns>
        public static GPSPoint GCJ02ToWGS84(double gcj_lat,double gcj_lon)
        {
            GPSPoint point = new GPSPoint();
            if (OutOfChina(gcj_lat, gcj_lon))
            {
                point.lon = gcj_lon;
                point.lat = gcj_lat;
            }
            else
            {
                double dlat = TransformLat(gcj_lon - 105.0, gcj_lat - 35.0);
                double dlon = TransformLon(gcj_lon - 105.0, gcj_lat - 35.0);
                double radlat = gcj_lat / 180.0 * pi;
                double magic = Math.Sin(radlat);
                magic = 1 - ee * magic * magic;
                double sqrtmagic = Math.Sqrt(magic);
                dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
                dlon = (dlon * 180.0) / (a / sqrtmagic * Math.Cos(radlat) * pi);
                double mglat = gcj_lat + dlat;
                double mglon = gcj_lon + dlon;
                point.lon = gcj_lon * 2 - mglon;
                point.lat = gcj_lat * 2 - mglat;
            }
            return point;
        }

        /// <summary>
        /// BD09转WGS84
        /// </summary>
        /// <param name="bd_lat"></param>
        /// <param name="bd_lon"></param>
        /// <returns></returns>
        public static GPSPoint BD09ToWGS84(double bd_lat, double bd_lon)
        {
            GPSPoint point = BD09ToGCJ02(bd_lat, bd_lon);
            return GCJ02ToWGS84(point.lat, point.lon);
        }

        /// <summary>
        /// GS-84转换BD09
        /// </summary>
        /// <param name="wgLat"></param>
        /// <param name="wgLon"></param>
        /// <returns></returns>
        public static GPSPoint WGS84ToBD09(double wgLat, double wgLon)
        {
            GPSPoint point = WGS84ToGCJ02(wgLat, wgLon);
            return GCJ02ToBD09(point.lat, point.lon);
        }

        /// <summary>
        /// WGS-84转换GCJ-02
        /// </summary>
        /// <param name="wgLat">纬度</param>
        /// <param name="wgLon">经度</param>
        /// <returns></returns>
        public static GPSPoint WGS84ToGCJ02(double wgLat, double wgLon)
        {
            GPSPoint point = new GPSPoint();
            if (OutOfChina(wgLat, wgLon))
            {
                point.lat = wgLat;
                point.lon = wgLon;
                return point;
            }
            double dLat = TransformLat(wgLon - 105.0, wgLat - 35.0);
            double dLon = TransformLon(wgLon - 105.0, wgLat - 35.0);
            double radLat = wgLat / 180.0 * pi;
            double magic = Math.Sin(radLat);
            magic = 1 - ee * magic * magic;
            double sqrtMagic = Math.Sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
            double lat = wgLat + dLat;
            double lon = wgLon + dLon;
            point.lat=lat;
            point.lon=lon;
            return point;
        }

        /// <summary>
        /// GCJ-02转换BD-09
        /// </summary>
        /// <param name="gg_lat">纬度</param>
        /// <param name="gg_lon">经度</param>
        /// <returns></returns>
        public static GPSPoint GCJ02ToBD09(double gg_lat, double gg_lon)
        {
            GPSPoint point = new GPSPoint();
            double x = gg_lon, y = gg_lat;
            double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
            double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
            double bd_lon = z * Math.Cos(theta) + 0.0065;
            double bd_lat = z * Math.Sin(theta) + 0.006;
            point.lat = bd_lat;
            point.lon = bd_lon;
            return point;
        }


        /// <summary>
        /// 经纬度点是否不再国内(这个方法精确度太差,可以使用GIS算法重写,我这边主要目的是为了做国内经纬度纠偏,国外的不需要纠偏)
        /// </summary>
        /// <param name="lat"></param>
        /// <param name="lon"></param>
        /// <returns></returns>
        private static bool OutOfChina(double lat, double lon)
        {
            return (lon < 72.004 || lon > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
        }
        private static 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 * Math.Sqrt(Math.Abs(x));
            ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
            ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
            return ret;
        }

        private static double TransformLon(double x, double y)
        {
            double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
            ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
            ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
            return ret;
        }
    }
}

 2.3.界面设计

几个RadioButton,一个Button,两个RichTextBox

界面代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void btn_change_Click(object sender, EventArgs e)
        {
            rtb_conversion.Clear();
            foreach (var item in rtb_previous.Lines)
            {
                string[] lonlatArr = item.Split(',');
                GPSPoint point = new GPSPoint();
                if (rb_bd09Togcj02.Checked)
                {
                    point = CoordinateUtil.BD09ToGCJ02(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
                }
                else if (rb_bd09Towgs84.Checked)
                {
                    point = CoordinateUtil.BD09ToWGS84(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
                }
                else if (rb_wgs84Tobd09.Checked)
                {
                    point = CoordinateUtil.WGS84ToBD09(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
                }
                else if (rb_gcj02Tobd09.Checked)
                {
                    point = CoordinateUtil.GCJ02ToBD09(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
                }
                else if (rb_wgs84Togcj02.Checked)
                {
                    point = CoordinateUtil.WGS84ToGCJ02(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
                }
                else if (rb_gcj02Towgs84.Checked)
                {
                    point = CoordinateUtil.GCJ02ToWGS84(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
                }

                rtb_conversion.Text += point.lon + "," + point.lat + System.Environment.NewLine;
            }
        }
    }
}

 另外还有一个经纬度点的封装实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoordinateConversionForm
{
    public class GPSPoint
    {
        public double lat;
        public double lon;
    }
}

3.实现效果

4.源码资源 

下载地址

猜你喜欢

转载自blog.csdn.net/qq_17486399/article/details/120488703