-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPS_To_Baidu.cs
192 lines (182 loc) · 8.09 KB
/
GPS_To_Baidu.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFrameWork.Server.Helper
{
public class GPS_To_Baidu
{
// 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;
// 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;
// }
// /*
// 参数
// wgLat:WGS-84纬度wgLon:WGS-84经度
// 返回值:
// mgLat:GCJ-02纬度mgLon:GCJ-02经度
// */
//public static double[] gps_transform(double wgLat, double wgLon, double mgLat=0.0, double mgLon=0.0)
// {
// if (outOfChina(wgLat, wgLon))
// {
// mgLat = wgLat;
// mgLon = wgLon;
// //return [0.0];
// }
// 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;
// return bd_encrypt(mgLat, mgLon);
// }
// const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
// //将 GCJ-02 坐标转换成 BD-09 坐标
//public static double[] bd_encrypt(double gg_lat, double gg_lon, double bd_lat=0.0, double bd_lon=0.0)
// {
// double x = gg_lon, y = gg_lat;
// double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
// double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
// bd_lon = z *Math.Cos(theta) + 0.0065;
// bd_lat = z * Math.Sin(theta) + 0.006;
// double[] point = new double[] { bd_lon, bd_lat };
// return point;
// }
// 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 =Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
// double theta =Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
// gg_lon = z * Math.Cos(theta);
// gg_lat = z * Math.Sin(theta);
// }
static double pi = 3.14159265358979324;
static double a = 6378245.0;
static double ee = 0.00669342162296594323;
public const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
/// <summary>
/// gps坐标转换成百度坐标,小数点前4位为准确坐标
/// </summary>
/// <param name="lat">纬度</param>
/// <param name="lon">经度</param>
/// <returns></returns>
public static double[] wgs2bd(double lat, double lon)
{
double[] dwgs2gcj = wgs2gcj(lat, lon);
double[] dgcj2bd = gcj2bd(dwgs2gcj[0], dwgs2gcj[1]);
return dgcj2bd;
}
/// <summary>
/// 火星坐标转百度坐标
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
public static double[] gcj2bd(double lat, double lon)
{
double x = lon, y = lat;
double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
double bd_lon = z * Math.Cos(theta) + 0.0065;
double bd_lat = z * Math.Sin(theta) + 0.006;
return new double[] { bd_lat, bd_lon };
}
/// <summary>
/// 百度坐标转火星坐标
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
public static double[] bd2gcj(double lat, double lon)
{
double x = lon - 0.0065, y = lat - 0.006;
double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
double gg_lon = z * Math.Cos(theta);
double gg_lat = z * Math.Sin(theta);
return new double[] { gg_lat, gg_lon };
}
/// <summary>
/// GPS 转 火星坐标
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
public static double[] wgs2gcj(double lat, double 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;
double[] loc = { mgLat, mgLon };
return loc;
}
/// <summary>
/// 转纬度
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
private static double transformLat(double lat, double lon)
{
double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.Sqrt(Math.Abs(lat));
ret += (20.0 * Math.Sin(6.0 * lat * pi) + 20.0 * Math.Sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(lon * pi) + 40.0 * Math.Sin(lon / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.Sin(lon / 12.0 * pi) + 320 * Math.Sin(lon * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
/// <summary>
/// 转精度
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
private static double transformLon(double lat, double lon)
{
double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.Sqrt(Math.Abs(lat));
ret += (20.0 * Math.Sin(6.0 * lat * pi) + 20.0 * Math.Sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(lat * pi) + 40.0 * Math.Sin(lat / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.Sin(lat / 12.0 * pi) + 300.0 * Math.Sin(lat / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
}
}