码迷,mamicode.com
首页 > 其他好文 > 详细

坐标转换——GCJ-02

时间:2016-05-21 21:42:10      阅读:1567      评论:0      收藏:0      [点我收藏+]

标签:

WGS84(World Geodetic System 1984),是为GPS 全球定位系统 使用而建立的坐标系统
GCJ-02,我国在WGS84的基础上加密得到
BD-09,百度坐标在GCJ-02基础上,进行了BD-09二次加密
火星坐标与百度坐标(GCJ-02----BD-09)转换
国际经纬度坐标标准为WGS-84,国内必须至少使用国测局制定的GCJ-02,对地理位置进行首次加密。
百度坐标在此基础上,进行了BD-09二次加密措施,更加保护了个人隐私。
百度对外接口的坐标系并不是GPS采集的真实经纬度,需要通过坐标转换接口进行转换。

百度坐标与火星坐标的相互转换:

        public class Coor
        {
            const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;//定义常量
            /// <summary>
            /// 将 GCJ-02 坐标转换成 BD-09 坐标
            /// </summary>
            /// <param name="gg_lat">火星X坐标</param>
            /// <param name="gg_lon">火星Y坐标</param>
            /// <param name="XX">百度X坐标</param>
            /// <param name="YY">百度Y坐标</param>
            public void bd_encrypt(double gg_lat, double gg_lon, out double XX, out double YY)
            {
                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);
                XX = z * Math.Cos(theta) + 0.0065;
                YY = z * Math.Sin(theta) + 0.006;
            }
            /// <summary>
            /// 将 BD-09 坐标转换成  GCJ-02坐标
            /// </summary>
            /// <param name="bd_lat">百度X坐标</param>
            /// <param name="bd_lon">百度Y坐标</param>
            /// <param name="XX">火星X坐标</param>
            /// <param name="YY">火星Y坐标</param>
            public void bd_decrypt(double bd_lat, double bd_lon, out double XX, out double YY)
            {
                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);
                XX = z * Math.Cos(theta);
                YY = z * Math.Sin(theta);
            }
        }

火星坐标到WGS-84的转换:

        //GCJ-02 到 WGS-84 的转换(即 GPS 纠偏)
        public class EvilTransform
        {
            const double pi = 3.14159265358979324;
            const double a = 6378245.0;
            const double ee = 0.00669342162296594323;
            public void transform(double wgLat, double wgLon, out double mgLat, out double mgLon)
            {
                if (outOfChina(wgLat, wgLon))
                {
                    mgLat = wgLat;
                    mgLon = wgLon;
                    return;
                }
                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);
                mgLat = wgLat + dLat;
                mgLon = wgLon + dLon;
            }

            static bool outOfChina(double lat, double lon)
            {
                if (lon < 72.004 || lon > 137.8347)
                    return true;
                if (lat < 0.8293 || lat > 55.8271)
                    return true;
                return false;
            }

            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;
            }

            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;
            }
        }

 源码下载

坐标转换——GCJ-02

标签:

原文地址:http://www.cnblogs.com/ytwy/p/5515553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!