forked from googollee/eviltransform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvilTransform.cs
146 lines (136 loc) · 4.94 KB
/
EvilTransform.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
using System;
namespace EvilTransform
{
public struct PointLatLng
{
public double Lat;
public double Lng;
public PointLatLng(double lat, double lng)
{
this.Lat = lat;
this.Lng = lng;
}
}
class Transform
{
bool OutOfChina(double lat, double lng)
{
if ((lng < 72.004) || (lng > 137.8347))
{
return true;
}
if ((lat < 0.8293) || (lat > 55.8271))
{
return true;
}
return false;
}
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 * Math.PI) + 20.0 * Math.Sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(y * Math.PI) + 40.0 * Math.Sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
ret += (160.0 * Math.Sin(y / 12.0 * Math.PI) + 320 * Math.Sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
return ret;
}
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 * Math.PI) + 20.0 * Math.Sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(x * Math.PI) + 40.0 * Math.Sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
ret += (150 * Math.Sin(x / 12.0 * Math.PI) + 300.0 * Math.Sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
return ret;
}
PointLatLng Delta(double lat, double lng)
{
PointLatLng ret = new PointLatLng();
double a = 6378137.0;
double ee = 0.00669342162296594323;
double dLat = TransformLat(lng - 105.0, lat - 35.0);
double dLng = TransformLon(lng - 105.0, lat - 35.0);
double radLat = lat / 180.0 * Math.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) * Math.PI);
dLng = (dLng * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * Math.PI);
ret.Lat = dLat;
ret.Lng = dLng;
return ret;
}
public PointLatLng WGS2GCJ(double wgsLat, double wgsLng)
{
if (OutOfChina(wgsLat, wgsLng))
{
return new PointLatLng(wgsLat, wgsLng);
}
PointLatLng d = Delta(wgsLat, wgsLng);
return new PointLatLng(wgsLat + d.Lat, wgsLng + d.Lng);
}
public PointLatLng GCJ2WGS(double gcjLat, double gcjLng)
{
if (OutOfChina(gcjLat, gcjLng))
{
return new PointLatLng(gcjLat, gcjLng);
}
PointLatLng d = Delta(gcjLat, gcjLng);
return new PointLatLng(gcjLat - d.Lat, gcjLng - d.Lng);
}
public PointLatLng GCJ2WGSExact(double gcjLat, double gcjLng)
{
double initDelta = 0.01;
double threshold = 0.000001;
double dLat = initDelta;
double dLng = initDelta;
double mLat = gcjLat - dLat;
double mLng = gcjLng - dLng;
double pLat = gcjLat + dLat;
double pLng = gcjLng + dLng;
double wgsLat = 0;
double wgsLng = 0;
for (int i = 0; i < 30; i++)
{
wgsLat = (mLat + pLat) / 2;
wgsLng = (mLng + pLng) / 2;
PointLatLng tmp = WGS2GCJ(wgsLat, wgsLng);
dLat = tmp.Lat - gcjLat;
dLng = tmp.Lng - gcjLng;
if ((Math.Abs(dLat) < threshold) && (Math.Abs(dLng) < threshold))
{
return new PointLatLng(wgsLat, wgsLng);
}
if (dLat > 0)
{
pLat = wgsLat;
}
else
{
mLat = wgsLat;
}
if (dLng > 0)
{
pLng = wgsLng;
}
else
{
mLng = wgsLng;
}
}
return new PointLatLng(wgsLat, wgsLng);
}
public double Distance(double latA, double lngA, double latB, double lngB)
{
double earthR = 6371000;
double x = Math.Cos(latA * Math.PI / 180) * Math.Cos(latB * Math.PI / 180) * Math.Cos((lngA - lngB) * Math.PI / 180);
double y = Math.Sin(latA * Math.PI / 180) * Math.Sin(latB * Math.PI / 180);
double s = x + y;
if (s > 1)
s = 1;
if (s < -1)
s = -1;
double alpha = Math.Acos(s);
var distance = alpha * earthR;
return distance;
}
}
}