转自群友的博客:https://www.xiaofengyu.com/?p=108

群友的github地址:https://github.com/jfwangncs/GPSConvert

 各种坐标系的各种转换

 public class TempGps
{ public double Tlng;
public double Tlat;
} public class GPS
{
public string oLng;//经度 度分秒坐标
public string oLat;//纬度 度分秒坐标 public double lng;//经度 WGS-84
public double lat;//纬度 WGS-84 public double gLng;//经度 GCJ-02 中国坐标偏移标准 Google Map、高德、腾讯使用
public double gLat;//纬度 GCJ-02 中国坐标偏移标准 Google Map、高德、腾讯使用 public double bLng;//经度 BD-09 百度坐标偏移标准,Baidu Map使用
public double bLat;//纬度 BD-09 百度坐标偏移标准,Baidu Map使用 public double PI = Math.PI;
double xPI = Math.PI * 3000.0 / 180.0; public TempGps delta(TempGps t)
{
var a = 6378245.0; // a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
var ee = 0.00669342162296594323; // ee: 椭球的偏心率。
var dLat = this.transformLat(t.Tlng - 105.0, t.Tlat - 35.0);
var dLng = this.transformLng(t.Tlng - 105.0, t.Tlat - 35.0);
var radLat = t.Tlat / 180.0 * PI;
var magic = Math.Sin(radLat);
magic = 1 - ee * magic * magic;
var sqrtMagic = Math.Sqrt(magic);
return new TempGps() { Tlat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI), Tlng= (dLng * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * PI) };
}
//WGS-84 to GCJ-02
public void gcj_encrypt()
{
if (this.outOfChina(lng, lat))
{
gLng = lng;
gLat = lat;
}
var t = this.delta(new TempGps() { Tlng = lng, Tlat = lat });
gLng = t.Tlng+lng;
gLat = t.Tlat+lat;
} //GCJ-02 to WGS-84
public void gcj_decrypt()
{ if (this.outOfChina(gLng, gLat))
{
lng = gLng;
lat = gLat; }
var t = this.delta(new TempGps() { Tlng = gLng, Tlat = gLat });
lng = gLng-t.Tlng;
lat = gLat-t.Tlat;
} //GCJ-02 to BD-09
public void bd_encrypt()
{
double x = gLng;
double y = gLat;
double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * xPI);
double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * xPI);
bLng = z * Math.Cos(theta) + 0.0065;
bLat = z * Math.Sin(theta) + 0.006;
}
//BD-09 to GCJ-02
public void bd_decrypt()
{
double x = bLng - 0.0065;
double y = bLat - 0.006;
double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * xPI);
double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * xPI);
gLng = z * Math.Cos(theta);
gLat = z * Math.Sin(theta);
} //WGS-84 to 度分秒坐标
public void wgs_decrypt()
{
oLng = TranDegreeToDMs(lng);
oLat = TranDegreeToDMs(lat);
} //度分秒坐标 to WGS-84
public void wgs_encrypt()
{
lng = TranDMsToDegree(oLng);
lat = TranDMsToDegree(oLat);
} public double TranDMsToDegree(string _dms)
{
string[] dms = _dms.Split('.');
if (dms.Length > 2)
return double.Parse(dms[0]) + double.Parse(dms[1]) / 60 + double.Parse(dms[2] + "." + dms[3] ?? "0") / 3600;
else
return 0d; } private static string TranDegreeToDMs(double d)
{
int Degree = Convert.ToInt16(Math.Truncate(d));//度
d = d - Degree;
int M = Convert.ToInt16(Math.Truncate((d) * 60));//分
int S = Convert.ToInt16(Math.Round((d * 60 - M) * 60));
if (S == 60)
{
M = M + 1;
S = 0;
}
if (M == 60)
{
M = 0;
Degree = Degree + 1;
}
string rstr = Degree.ToString() + ".";
if (M < 10)
rstr = rstr + "0" + M.ToString();
else
rstr = rstr + M.ToString();
if (S < 10)
rstr = rstr + "0" + S.ToString();
else
rstr = rstr + S.ToString();
return rstr;
} private bool outOfChina(double _lng, double _lat)
{
if (lng < 72.004 || lng > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
} private 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 double transformLng(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;
}
}

调用

  GPS t = new GPS();
t.oLng = dt.Rows[i][1].ToString();
t.oLat = dt.Rows[i][2].ToString();
t.wgs_encrypt();
t.gcj_encrypt();
t.bd_encrypt();
cells[i+1, 3].PutValue(t.bLng);
cells[i+1, 4].PutValue(t.bLat);

c#坐标系互相转换的更多相关文章

  1. 获取全国市以及地理坐标,各大坐标系北斗,百度,WGS-84坐标系的转换,有图,有代码

    1 先上坐标取到的值: 获取到的坐标部分如下: '北京市':[116.39564503788,39.92998577808], '天津市':[117.21081309155,39.1439299033 ...

  2. 【PHP版】火星坐标系 (GCJ-02) 与百度坐标系 (BD-09ll)转换算法

    首先感谢java版作者@宋宋宋伟,java版我是看http://blog.csdn.net/coolypf/article/details/8569813 然后根据java代码修改成了php代码. & ...

  3. C#实现地图坐标系的转换(WGS-84、GCJ-02、BD-09)

     WGS-84坐标系:全球定位系统使用,GPS.北斗等 GCJ-02坐标系:中国地区使用,由WGS-84偏移而来 BD-09坐标系:百度专用,由GCJ-02偏移而来 (PS:源于项目需求,本来是想读图 ...

  4. Cesium中的坐标系及转换

    在我们开始学习Entity之前,我们首先需要先学习下Cesium中的坐标系,Cesium中有多个坐标系,在进行添加Entity时经常会使用到. 一.坐标系介绍 我们先来列举下Cesium中的坐标系:W ...

  5. NX二次开发-从一个坐标系到另一个坐标系的转换

    函数:UF_MTX4_csys_to_csys().UF_MTX4_vec3_multiply() 函数说明:从一个坐标系统到另一个坐标系统的转换.如下图红色坐标系下有个红色的点,将红色的点转到绿色的 ...

  6. js不同地图坐标系经纬度转换(天地图,高德地图,百度地图,腾讯地图)

    1.js转换代码 1 //转换常数 2 var x_pi = 3.14159265358979324 * 3000.0 / 180.0; 3 var pi = 3.14159265358979324; ...

  7. CAD输出的局部平面坐标数据配准转换到WGS84坐标系

             局部平面坐标                                             平移纠正到常用平面坐标系下的坐标            转换后的地理坐标 采用两 ...

  8. 关于Cocos2d-x中坐标系的种类和转换

    注意: 当一个节点有一个子节点的时候,如果移动父节点,子节点也会跟着做相应的移动变化,只要被添加到父节点中,子节点就被绑定了,所以子节点的位置,坐标就会被动地变化. 当一个节点有一个子节点的时候,如果 ...

  9. GIS中的坐标系定义与转换

    GIS中的坐标系定义与转换 青岛海洋地质研究所 戴勤奋 2002-3-27 14:22:47 ----------------------------------------------------- ...

随机推荐

  1. PAT-B-1020 月饼 (25)(25 分)

    题目描述: 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一 ...

  2. mysql 重置密码

    mysql 重置密码,跳过登录修改密码: # centos 上mysql 已经改名了,启动服务的时候注意是mariadb 了!!!!! # systemctl stop mariadb # syste ...

  3. 论文翻译:Neural Networks With Few Multiplications

    目录 Abstract 1. Introduction 2.Related Work 3.Binary And Ternary Connect 3.1 BINARY CONNECT REVISITED ...

  4. linux 查看ip地址

    1.先要打开linux服务器,然后在linux桌面的空白处点击右键 2.在弹出的选项里,点击[打开终端] 3.打开linux服务器的命令终端后,输入查询linux的ip地址的命令:ifconfig - ...

  5. hibernate核心类及常用方法

    Configuration configure = new Configuration().configure(); SessionFactory factory = configure.buildS ...

  6. 2-3、配置Filebeat

    配置Filebeat 提示:Filebeat modules为常见的日志格式提供了最快的入门操作. 如果要使用Filebeat模块,请跳过本节,包括剩余的入门步骤,并直接转到快速入门:Quick st ...

  7. python的numpy库的学习

    1.创建 array(序列类型).asarray.arange.ones.ones_like.zeros.zeros_like.empty.empty_like.eye.identity 2.运算 两 ...

  8. MySQL----下载安装

    MySQL 的官网下载地址:http://www.mysql.com/downloads/ 注意 1. MySQL Community Server 社区版本,开源免费,但不提供官方技术支持.2. M ...

  9. JS中this指向问题相关知识点及解析

    概括:this指向在函数定义的时候是无法确定的,只有在函数调用执行的时候才能确定this最终指向了谁,this最终指向的是调用它的对象(常见的说法,后面有小小的纠正): 例1: 图中的函数fn1其实是 ...

  10. centos7设置httpd

    1.httpd开机自启动 systemctl enable httpd.service 2.httpd开机不自启动 systemctl disable httpd.service 3.启动httpd ...