程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> [C#]地球坐標系 (WGS-84) 到火星坐標系 (GCJ-02) 的轉換 幫助類,

[C#]地球坐標系 (WGS-84) 到火星坐標系 (GCJ-02) 的轉換 幫助類,

編輯:C#入門知識

[C#]地球坐標系 (WGS-84) 到火星坐標系 (GCJ-02) 的轉換 幫助類,


關鍵代碼:

using System;
using YanZhiwei.DotNet2.Utilities.Models;

namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    /// 地球坐標系 (WGS-84) 到火星坐標系 (GCJ-02) 的轉換 幫助類 
    /// </summary>
    public class WGSGCJLatLonHelper
    {
        /*
         *參考:
         *1.http://blog.csdn.net/yorling/article/details/9175913
         *2.http://kongxz.com/2013/10/wgs-cgj/
         *3.https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#EvilTransform.cs
         *4.https://github.com/googollee/eviltransform
         *5.https://github.com/shenqiliang/WGS2Mars
         *
         *WGS84:World Geodetic System 1984,是為GPS全球定位系統使用而建立的坐標系統。通過遍布世界的衛星觀測站觀測到
         *的坐標建立,其初次WGS84的精度為1-2m,在1994年1月2號,通過10個觀測站在GPS測量方法上改正,得到了WGS84
         *(G730),G表示由GPS測量得到,730表示為GPS時間第730個周。1996年,National Imagery and Mapping Agency (NIMA) 
         *為美國國防部 (U.S.Departemt of Defense, DoD)做了一個新的坐標系統。這樣實現了新的WGS版本:WGS(G873)。其因為
         *加入了USNO站和北京站的改正,其東部方向加入了31-39cm 的改正。所有的其他坐標都有在1分米之內的修正。
         *
         *GCJ-02:國家保密插件,也叫做加密插件或者加偏或者SM模組,其實就是對真實坐標系統進行人為的加偏處理,按照幾行代碼的算
         *法,將真實的坐標加密成虛假的坐標,而這個加偏並不是線性的加偏,所以各地的偏移情況都會有所不同。而加密後的坐標也常
         *被人稱為火星坐標系統。GCJ-02 意味“國測局-2002”,也就是說,這是國家測繪局於2002年弄出的標准。
         */
        #region 常量以及私有方法
        const double pi = 3.14159265358979324;//Math.PI;//;
        const double a = 6378245.0;
        const double ee = 0.00669342162296594323;
        private static double TransformLat(double lonX, double latY)
        {
            double _ret = -100.0 + 2.0 * lonX + 3.0 * latY + 0.2 * latY * latY + 0.1 * lonX * latY + 0.2 * Math.Sqrt(Math.Abs(lonX));
            _ret += (20.0 * Math.Sin(6.0 * lonX * pi) + 20.0 * Math.Sin(2.0 * lonX * pi)) * 2.0 / 3.0;
            _ret += (20.0 * Math.Sin(latY * pi) + 40.0 * Math.Sin(latY / 3.0 * pi)) * 2.0 / 3.0;
            _ret += (160.0 * Math.Sin(latY / 12.0 * pi) + 320 * Math.Sin(latY * pi / 30.0)) * 2.0 / 3.0;
            return _ret;
        }
        private static double TransformLon(double lonX, double latY)
        {
            double _ret = 300.0 + lonX + 2.0 * latY + 0.1 * lonX * lonX + 0.1 * lonX * latY + 0.1 * Math.Sqrt(Math.Abs(lonX));
            _ret += (20.0 * Math.Sin(6.0 * lonX * pi) + 20.0 * Math.Sin(2.0 * lonX * pi)) * 2.0 / 3.0;
            _ret += (20.0 * Math.Sin(lonX * pi) + 40.0 * Math.Sin(lonX / 3.0 * pi)) * 2.0 / 3.0;
            _ret += (150.0 * Math.Sin(lonX / 12.0 * pi) + 300.0 * Math.Sin(lonX / 30.0 * pi)) * 2.0 / 3.0;
            return _ret;
        }
        private static LatLngPoint Transform(LatLngPoint point)
        {
            LatLngPoint _transPoint = new LatLngPoint();
            double _lat = TransformLat(point.LonX - 105.0, point.LatY - 35.0);
            double _lon = TransformLon(point.LonX - 105.0, point.LatY - 35.0);
            double _radLat = point.LatY / 180.0 * pi;
            double _magic = Math.Sin(_radLat);
            _magic = 1 - ee * _magic * _magic;
            double _sqrtMagic = Math.Sqrt(_magic);
            _lat = (_lat * 180.0) / ((a * (1 - ee)) / (_magic * _sqrtMagic) * pi);
            _lon = (_lon * 180.0) / (a / _sqrtMagic * Math.Cos(_radLat) * pi);
            _transPoint.LatY = _lat;
            _transPoint.LonX = _lon;

            return _transPoint;
        }
        #endregion
        #region 火星坐標轉 (GCJ-02)地球坐標(WGS-84)
        /// <summary>
        /// 火星坐標轉 (GCJ-02)地球坐標(WGS-84)
        /// </summary>
        /// <param name="gcjPoint">火星坐標轉 (GCJ-02)</param>
        /// <returns>地球坐標(WGS-84)</returns>
        public static LatLngPoint GCJ02ToWGS84(LatLngPoint gcjPoint)
        {
            if (MapHelper.OutOfChina(gcjPoint))
            {
                return gcjPoint;
            }
            LatLngPoint _transPoint = Transform(gcjPoint);
            return new LatLngPoint(gcjPoint.LatY - _transPoint.LatY, gcjPoint.LonX - _transPoint.LonX);
        }
        #endregion
        #region 地球坐標(WGS-84)轉火星坐標 (GCJ-02)
        /// <summary>
        /// 地球坐標(WGS-84)轉火星坐標 (GCJ-02)
        /// </summary>
        /// <param name="wgsPoint">地球坐標(WGS-84)</param>
        /// <returns>火星坐標 (GCJ-02)</returns>
        public static LatLngPoint WGS84ToGCJ02(LatLngPoint wgsPoint)
        {
            if (MapHelper.OutOfChina(wgsPoint))
            {
                return wgsPoint;
            }
            LatLngPoint _transPoint = Transform(wgsPoint);
            return new LatLngPoint(wgsPoint.LatY + _transPoint.LatY, wgsPoint.LonX + _transPoint.LonX);
        }
        #endregion

    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved