火星坐标转换为GPS坐标

     中国的地图和卫星图都存在一个偏移量,我们在Google Map上看到的坐标与实际的GPS坐标其实是有着一定范围的偏移的,而在Google Earth上看到的坐标与GPS坐标是完全一致的,也就是说,同一张地图图片,切换到Google Map和Google Earth模式下的地图实际是上不匹配的,为什么会有这个偏移这里就不作叙述了,那么当我们拿到地图上的座标以后,要怎样转成GPS坐标呢?

     在google中找到一段C的代码,主要是借助一组码表,加上一个计算公式而得到GPS坐标,我用C#翻译了一下,下面贴上代码:

 

 

        double[] TableY = new double[660 * 450];
        bool InitTable = false;

        public Form1()
        {
            InitializeComponent();

            LoadText();
        }

        private int GetID(int I, int J)
        {
            return I + 660 * J;
        }

        private void LoadText()
        {
            using (StreamReader sr = new StreamReader("Mars2Wgs.txt"))
            {
                string s = sr.ReadToEnd();

                Match MP = Regex.Match(s, "(\\d+)");

                int i = 0;
                while (MP.Success)
                {
                    //MessageBox.Show(MP.Value);
                    if (i % 2 == 0)
                    {
                        TableX[i / 2] = Convert.ToDouble(MP.Value) / 100000.0;
                    }
                    else
                    {
                        TableY[(i - 1) / 2] = Convert.ToDouble(MP.Value) / 100000.0;
                    }
                    i++;
                    MP = MP.NextMatch();
                }
                InitTable = true;
                //MessageBox.Show((i / 2).ToString());
            }
        }

        /// <summary>
        /// x是117左右,y是31左右
        /// </summary>
        /// <param name="xMars"></param>
        /// <param name="yMars"></param>
        /// <param name="xWgs"></param>
        /// <param name="yWgs"></param>
        private void Parse(double xMars, double yMars, ref double xWgs, ref double yWgs)
        {
            int i, j, k;
            double x1, y1, x2, y2, x3, y3, x4, y4, xtry, ytry, dx, dy;
            double t, u;

            if (!InitTable)
                return;

            xtry = xMars;
            ytry = yMars;

            for (k = 0; k < 10; ++k)
            {
                // 只对中国国境内数据转换
                if (xtry < 72 || xtry > 137.9 || ytry < 10 || ytry > 54.9)
                {
                    return;
                }

                i = (int)((xtry - 72.0) * 10.0);
                j = (int)((ytry - 10.0) * 10.0);

                x1 = TableX[GetID(i, j)];
                y1 = TableY[GetID(i, j)];
                x2 = TableX[GetID(i + 1, j)];
                y2 = TableY[GetID(i + 1, j)];
                x3 = TableX[GetID(i + 1, j + 1)];
                y3 = TableY[GetID(i + 1, j + 1)];
                x4 = TableX[GetID(i, j + 1)];
                y4 = TableY[GetID(i, j + 1)];

                t = (xtry - 72.0 - 0.1 * i) * 10.0;
                u = (ytry - 10.0 - 0.1 * j) * 10.0;

                dx = (1.0 - t) * (1.0 - u) * x1 + t * (1.0 - u) * x2 + t * u * x3 + (1.0 - t) * u * x4 - xtry;
                dy = (1.0 - t) * (1.0 - u) * y1 + t * (1.0 - u) * y2 + t * u * y3 + (1.0 - t) * u * y4 - ytry;

                xtry = (xtry + xMars - dx) / 2.0;
                ytry = (ytry + yMars - dy) / 2.0;
            }

            xWgs = xtry;
            yWgs = ytry;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double x = Convert.ToDouble(txbX.Text);
            double y = Convert.ToDouble(txbY.Text);

            double xWgs = x;
            double yWgs = y;

            Parse(x, y, ref xWgs, ref yWgs);

            ltrResult.Text = "X:" + xWgs.ToString() + ",Y:" + yWgs.ToString();
        }

来源:http://www.cnblogs.com/ahhi/archive/2010/04/27/Wars2Wgs.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.map; public class GPSConverterUtils { public static final String BAIDU_LBS_TYPE = "bd09ll"; public static double pi = 3.1415926535897932384626; public static double a = 6378245.0; public static double ee = 0.00669342162296594323; /** * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System * 天地图火星 * @param lat * @param lon */ public static GPS gps84_To_Gcj02(double lat, double lon) { if (outOfChina(lat, lon)) { return null; } 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); } /** * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return *火星转天地图 */ public static GPS gcj_To_Gps84(double lat, double lon) { GPS gps = transform(lat, lon); double lontitude = lon * 2 - gps.getLon(); double latitude = lat * 2 - gps.getLat(); return new GPS(latitude, lontitude); } /** * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标 *火星转百度 * @param gg_lat * @param gg_lon */ public 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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值