地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02)百度坐标系 (BD-09) 的转换算法 .

地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换算法

 


WGS-84 到 GCJ-02 的转换(即 GPS 加偏)算法是一个普通青年轻易无法接触到的“公开”的秘密。这个算法的代码在互联网上是公开的,详情请使用 Google 搜索 "wgtochina_lb" 。

整理后的算法代码请参考 https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936 。知道了这个算法之后,就可以离线进行 Google 地图偏移校正,不必像之前那么麻烦。

至于 GCJ-02 到 WGS-84 的转换(即 GPS 纠偏),可以使用二分法。

  1. using System;  
  2.   
  3. namespace Navi  
  4. {  
  5.     class EvilTransform  
  6.     {  
  7.         const double pi = 3.14159265358979324;  
  8.   
  9.         //   
  10.         // Krasovsky 1940   
  11.         //   
  12.         // a = 6378245.0, 1/f = 298.3   
  13.         // b = a * (1 - f)   
  14.         // ee = (a^2 - b^2) / a^2;   
  15.         const double a = 6378245.0;  
  16.         const double ee = 0.00669342162296594323;  
  17.   
  18.         //   
  19.         // World Geodetic System ==> Mars Geodetic System   
  20.         public static void transform(double wgLat, double wgLon, out double mgLat, out double mgLon)  
  21.         {  
  22.             if (outOfChina(wgLat, wgLon))  
  23.             {  
  24.                 mgLat = wgLat;  
  25.                 mgLon = wgLon;  
  26.                 return;  
  27.             }  
  28.             double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);  
  29.             double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);  
  30.             double radLat = wgLat / 180.0 * pi;  
  31.             double magic = Math.Sin(radLat);  
  32.             magic = 1 - ee * magic * magic;  
  33.             double sqrtMagic = Math.Sqrt(magic);  
  34.             dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);  
  35.             dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);  
  36.             mgLat = wgLat + dLat;  
  37.             mgLon = wgLon + dLon;  
  38.         }  
  39.   
  40.         static bool outOfChina(double lat, double lon)  
  41.         {  
  42.             if (lon < 72.004 || lon > 137.8347)  
  43.                 return true;  
  44.             if (lat < 0.8293 || lat > 55.8271)  
  45.                 return true;  
  46.             return false;  
  47.         }  
  48.   
  49.         static double transformLat(double x, double y)  
  50.         {  
  51.             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));  
  52.             ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;  
  53.             ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;  
  54.             ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;  
  55.             return ret;  
  56.         }  
  57.   
  58.         static double transformLon(double x, double y)  
  59.         {  
  60.             double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));  
  61.             ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;  
  62.             ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;  
  63.             ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;  
  64.             return ret;  
  65.         }  
  66.     }  
  67. }  
using System;

namespace Navi
{
    class EvilTransform
    {
        const double pi = 3.14159265358979324;

        //
        // Krasovsky 1940
        //
        // a = 6378245.0, 1/f = 298.3
        // b = a * (1 - f)
        // ee = (a^2 - b^2) / a^2;
        const double a = 6378245.0;
        const double ee = 0.00669342162296594323;

        //
        // World Geodetic System ==> Mars Geodetic System
        public static 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;
        }
    }
}


参考:

http://blog.csdn.net/coolypf/article/details/8686588

http://blog.csdn.net/giswens/article/details/8775283

https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936



火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法

关于 GCJ-02 和 BD-09 ,请参考 http://developer.baidu.com/map/question.htm#qa0043 。

算法代码如下,其中 bd_encrypt 将 GCJ-02 坐标转换成 BD-09 坐标, bd_decrypt 反之。


  1. #include <math.h>   
  2.   
  3. const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;  
  4.   
  5. void bd_encrypt(double gg_lat, double gg_lon, double &bd_lat, double &bd_lon)  
  6. {  
  7.     double x = gg_lon, y = gg_lat;  
  8.     double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);  
  9.     double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);  
  10.     bd_lon = z * cos(theta) + 0.0065;  
  11.     bd_lat = z * sin(theta) + 0.006;  
  12. }  
  13.   
  14. void bd_decrypt(double bd_lat, double bd_lon, double &gg_lat, double &gg_lon)  
  15. {  
  16.     double x = bd_lon - 0.0065, y = bd_lat - 0.006;  
  17.     double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);  
  18.     double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);  
  19.     gg_lon = z * cos(theta);  
  20.     gg_lat = z * sin(theta);  
  21. }  
#include <math.h>

const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

void bd_encrypt(double gg_lat, double gg_lon, double &bd_lat, double &bd_lon)
{
    double x = gg_lon, y = gg_lat;
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
    double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
    bd_lon = z * cos(theta) + 0.0065;
    bd_lat = z * sin(theta) + 0.006;
}

void bd_decrypt(double bd_lat, double bd_lon, double &gg_lat, double &gg_lon)
{
    double x = bd_lon - 0.0065, y = bd_lat - 0.006;
    double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
    double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
    gg_lon = z * cos(theta);
    gg_lat = z * sin(theta);
}



http://blog.csdn.net/giswens/article/details/8775275




其他参考

http://www.zuidaima.com/code/file/1665077235663872.htm?dir=/correct_lat_lon/src/com/hgq/correct/MapFix.java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值