1. 程式人生 > >LBS——座標型別轉換(C#)

LBS——座標型別轉換(C#)

座標型別轉換


WGS84座標系:即地球座標系,國際上通用的座標系。裝置一般包含GPS晶片或者北斗晶片獲取的經緯度為WGS84地理座標系,谷歌地圖採用的是WGS84地理座標系(中國範圍除外);
GCJ02座標系:即火星座標系,是由中國國家測繪局制訂的地理資訊系統的座標系統。由WGS84座標系經加密後的座標系。谷歌中國地圖和高德地圖採用的是GCJ02地理座標系;
BD09座標系:即百度座標系,GCJ02座標系經加密後的座標系;搜狗座標系、圖吧座標系等,估計也是在GCJ02基礎上加密而成的

座標轉化

    public
class GpsCoordinate { #region 地球引數 private static readonly double pi = 3.1415926535897932384626; private static readonly double a = 6378245.0; private static readonly double ee = 0.00669342162296594323; #endregion #region 座標轉換公開方法 /// <summary>
/// 座標型別轉換 /// </summary> public enum LBS_TYPE { Wgs84_To_Gcj02 = 1, Gcj02_To_Wgs84 = 2, Gcj02_To_Bd09 = 3, Bd09_To_Gcj02=4, Bd09_To_Wgs84=5, Wgs84_To_Bd09=6 } /// <summary> /// 座標型別
/// </summary> public enum GPS_TYPE { [Description("地球座標系:標準GPS")] Wgs84 =1, [Description("火星座標系:高德")] Gcj02 =2, [Description("高德座標轉地名:百度")] Bd09 =3 } /// <summary> /// 座標轉換 /// </summary> /// <param name="lat">緯度</param> /// <param name="lon">經度</param> /// <param name="OldTyle">舊座標系</param> /// <param name="NewType">新座標系</param> /// <returns></returns> public static Gps GpsToNewGps(double lat, double lon, GPS_TYPE OldTyle, GPS_TYPE NewType) { int OldTyle_1 = (int)OldTyle; int NewType_1 = (int)NewType; Gps gs = null; switch (OldTyle_1 + ""+ NewType_1) { case "12": gs = Wgs84_To_Gcj02(lat, lon); break; case "21": gs = Gcj02_To_Wgs84(lat, lon); break; case "23": gs = Gcj02_To_Bd09(lat, lon); break; case "32": gs = Bd09_To_Gcj02(lat, lon); break; case "31": gs = Bd09_To_Wgs84(lat, lon); break; case "13": gs = Wgs84_To_Bd09(lat, lon); break; default: gs = new Gps(lat, lon); break; } return gs; } /// <summary> /// 座標轉換 /// </summary> /// <param name="lat">緯度</param> /// <param name="lon">經度</param> /// <param name="type">轉換型別(enum LBS_TYPE)</param> /// <returns></returns> public static Gps GpsToNewGps(double lat, double lon, LBS_TYPE type) { /** * type=1;Wgs84 to 火星座標系 (GCJ-02) * type=2;火星座標系 (GCJ-02) to Wgs84 * type=3;火星座標系 (GCJ-02) 與百度座標系 (BD-09) * type=4;(BD-09)-->火星座標系 (GCJ-02) * type=5;(BD-09)-->Wgs84 * **/ Gps gs = null; switch (type) { case LBS_TYPE.Wgs84_To_Gcj02: gs = Wgs84_To_Gcj02(lat, lon); break; case LBS_TYPE.Gcj02_To_Wgs84: gs = Gcj02_To_Wgs84(lat, lon); break; case LBS_TYPE.Gcj02_To_Bd09: gs = Gcj02_To_Bd09(lat, lon); break; case LBS_TYPE.Bd09_To_Gcj02: gs = Bd09_To_Gcj02(lat, lon); break; case LBS_TYPE.Bd09_To_Wgs84: gs = Bd09_To_Wgs84(lat, lon); break; case LBS_TYPE.Wgs84_To_Bd09: gs = Wgs84_To_Bd09(lat, lon); break; default: gs = new Gps(lat, lon); break; } return gs; } /// <summary> /// 座標轉換 /// </summary> /// <param name="lat">緯度</param> /// <param name="lon">經度</param> /// <param name="type">[1;Wgs84 to 火星座標系 (GCJ-02)][2;火星座標系 (GCJ-02) to Wgs84 ][3;火星座標系 (GCJ-02) 與百度座標系 (BD-09)][4;(BD-09)-->火星座標系 (GCJ-02)][5;(BD-09)-->Wgs84]</param> /// <returns>Gps</returns> public static Gps GpsToNewGps(double lat, double lon, string type) { /** * type=1;Wgs84 to 火星座標系 (GCJ-02) * type=2;火星座標系 (GCJ-02) to Wgs84 * type=3;火星座標系 (GCJ-02) 與百度座標系 (BD-09) * type=4;(BD-09)-->火星座標系 (GCJ-02) * type=5;(BD-09)-->Wgs84 * **/ Gps gs = null; switch (type) { case "1": gs = Wgs84_To_Gcj02(lat, lon); break; case "2": gs = Gcj02_To_Wgs84(lat, lon); break; case "3": gs = Gcj02_To_Bd09(lat, lon); break; case "4": gs = Bd09_To_Gcj02(lat, lon); break; case "5": gs = Bd09_To_Wgs84(lat, lon); break; case "6": gs = Wgs84_To_Bd09(lat, lon); break; default: gs = new Gps(lat, lon); break; } return gs; } /// <summary> /// 座標轉換 /// Wgs84 to 火星座標系 (GCJ-02):當返回null是標識座標 /// </summary> /// <param name="lat">緯度</param> /// <param name="lon">經度</param> /// <param name="type">[1;Wgs84 to 火星座標系 (GCJ-02)][2;火星座標系 (GCJ-02) to Wgs84 ][3;火星座標系 (GCJ-02) 與百度座標系 (BD-09)][4;(BD-09)-->火星座標系 (GCJ-02)][5;(BD-09)-->Wgs84]</param> /// <returns>Gps</returns> public static Gps GpsToNewGps(double lat, double lon, int type) { /** * type=1;Wgs84 to 火星座標系 (GCJ-02) * type=2;火星座標系 (GCJ-02) to Wgs84 * type=3;火星座標系 (GCJ-02) 與百度座標系 (BD-09) * type=4;(BD-09)-->火星座標系 (GCJ-02) * type=5;(BD-09)-->Wgs84 * **/ Gps gs = null; switch (type) { case 1: gs = Wgs84_To_Gcj02(lat, lon); break; case 2: gs = Gcj02_To_Wgs84(lat, lon); break; case 3: gs = Gcj02_To_Bd09(lat, lon); break; case 4: gs = Bd09_To_Gcj02(lat, lon); break; case 5: gs = Bd09_To_Wgs84(lat, lon); break; case 6: gs= Wgs84_To_Bd09(lat, lon); break; default: gs = new Gps(lat, lon); break; } return gs; } #endregion #region 座標轉化 /// <summary> ///Wgs84==>>GCJ-02 /// </summary> private static Gps Wgs84_To_Gcj02(double lat, double lon) { //if (outOfChina(lat, lon)) //{ // return new Gps(lat, lon); //} double dLat = transformLat(lon - 105.0, lat - 35.0); double dLon = transformLon(lon - 105.0, lat - 35.0); double radLat = 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 = lat + dLat; double mgLon = lon + dLon; return new Gps(mgLat, mgLon); } /// <summary> /// GCJ-02==>>Wgs84 /// </summary> private static Gps Gcj02_To_Wgs84(double lat, double lon) { Gps gps = transform(lat, lon); double lontitude = lon * 2 - gps.getWgLon(); double latitude = lat * 2 - gps.getWgLat(); return new Gps(latitude, lontitude); } /// <summary> /// GCJ-02==>>BD-09 /// </summary> private static Gps Gcj02_To_Bd09(double gg_lat, double gg_lon) { double x = gg_lon, y = gg_lat; double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * pi); double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * pi); double bd_lon = z * Math.Cos(theta) + 0.0065; double bd_lat = z * Math.Sin(theta) + 0.006; return new Gps(bd_lat, bd_lon); } /// <summary> /// BD-09==>>GCJ-02 /// </summary> private static Gps Bd09_To_Gcj02(double bd_lat, double bd_lon) { double x = bd_lon - 0.0065, y = bd_lat - 0.006; double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * pi); double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * pi); double gg_lon = z * Math.Cos(theta); double gg_lat = z * Math.Sin(theta); return new Gps(gg_lat, gg_lon); } /// <summary> /// (BD-09)==>>Wgs84 /// </summary> private static Gps Bd09_To_Wgs84(double bd_lat, double bd_lon) { Gps gcj02 = Bd09_To_Gcj02(bd_lat, bd_lon); Gps map84 = Gcj02_To_Wgs84(gcj02.getWgLat(), gcj02.getWgLon()); return map84; } /// <summary> /// Wgs84==>>(BD-09) /// </summary> /// <param name="bd_lat"></param> /// <param name="bd_lon"></param> /// <returns></returns> private static Gps Wgs84_To_Bd09(double bd_lat, double bd_lon) { Gps gcj02 = Wgs84_To_Gcj02(bd_lat, bd_lon); Gps map84 = Gcj02_To_Bd09(gcj02.getWgLat(), gcj02.getWgLon()); return map84; } #endregion #region 公共方法 /// <summary> /// 是否在中國內 /// </summary> /// <param name="lat"></param> /// <param name="lon"></param> /// <returns></returns> public 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; } private static Gps transform(double lat, double lon) { //if (outOfChina(lat, lon)) //{ // return new Gps(lat, lon); //} double dLat = transformLat(lon - 105.0, lat - 35.0); double dLon = transformLon(lon - 105.0, lat - 35.0); double radLat = 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 = lat + dLat; double mgLon = lon + dLon; return new Gps(mgLat, mgLon); } 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; } #endregion }

座標模型

  public class Gps
    {
        /// <summary>
        /// 緯度
        /// </summary>
        private double wgLat;
        /// <summary>
        /// 經度
        /// </summary>
        private double wgLon;

        public Gps()
        {
            wgLat = 0;
            wgLon = 0;
        }
        public Gps(double wgLat, double wgLon)
        {
            setWgLat(wgLat);
            setWgLon(wgLon);
        }
        public double getWgLat()
        {
            return wgLat;
        }
        public void setWgLat(double wgLat)
        {
            this.wgLat = wgLat;
        }
        public double getWgLon()
        {
            return wgLon;
        }
        public void setWgLon(double wgLon)
        {
            this.wgLon = wgLon;
        }
        public String toString()
        {
            return wgLat + "," + wgLon;
        }
    }