-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECEFRef.cs
172 lines (153 loc) · 4.94 KB
/
ECEFRef.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
using DotNetCoords.Datum;
using DotNetCoords.Ellipsoid;
using System;
namespace DotNetCoords
{
/// <summary>
/// ECEF (earth-centred, earth-fixed) Cartesian co-ordinates are used to define a
/// point in three-dimensional space. ECEF co-ordinates are defined relative to
/// an x-axis (the intersection of the equatorial plane and the plane defined by
/// the prime meridian), a y-axis (at 90° to the x-axis and its intersection
/// with the equator) and a z-axis (intersecting the North Pole). All the axes
/// intersect at the point defined by the centre of mass of the Earth.
/// </summary>
public class ECEFRef : CoordinateSystem
{
/**
* x co-ordinate in metres.
*/
private double x;
/**
* y co-ordinate in metres.
*/
private double y;
/**
* z co-ordinate in metres.
*/
private double z;
/// <summary>
/// Create a new earth-centred, earth-fixed (ECEF) reference from the given
/// parameters using the WGS84 reference ellipsoid.
/// </summary>
/// <param name="x">The x co-ordinate.</param>
/// <param name="y">The y co-ordinate.</param>
/// <param name="z">The z co-ordinate.</param>
public ECEFRef(double x, double y, double z) : this(x, y, z, WGS84Datum.Instance)
{
}
/// <summary>
/// Create a new earth-centred, earth-fixed (ECEF) reference from the given
/// parameters and the given reference ellipsoid.
/// </summary>
/// <param name="x">The x co-ordinate.</param>
/// <param name="y">The y co-ordinate.</param>
/// <param name="z">The z co-ordinate.</param>
/// <param name="datum">The datum.</param>
public ECEFRef(double x, double y, double z, DotNetCoords.Datum.Datum datum) : base(datum)
{
X = x;
Y = y;
Z = z;
}
/// <summary>
/// Create a new earth-centred, earth-fixed reference from the given latitude
/// and longitude.
/// </summary>
/// <param name="ll">The latitude and longitude.</param>
public ECEFRef(LatLng ll) : base(ll.Datum)
{
DotNetCoords.Ellipsoid.Ellipsoid ellipsoid = Datum.ReferenceEllipsoid;
double phi = Util.ToRadians(ll.Latitude);
double lambda = Util.ToRadians(ll.Longitude);
double h = ll.Height;
double a = ellipsoid.SemiMajorAxis;
double f = ellipsoid.Flattening;
double eSquared = (2 * f) - (f * f);
double nphi = a / Math.Sqrt(1 - eSquared * Util.sinSquared(phi));
X = (nphi + h) * Math.Cos(phi) * Math.Cos(lambda);
Y = (nphi + h) * Math.Cos(phi) * Math.Sin(lambda);
Z = (nphi * (1 - eSquared) + h) * Math.Sin(phi);
}
/// <summary>
/// Convert this ECEFRef object to a point represented
/// by a latitude and longitude and a perpendicular height above (or below) a
/// reference ellipsoid.
/// </summary>
/// <returns>
/// The equivalent latitude and longitude.
/// </returns>
public override LatLng ToLatLng()
{
DotNetCoords.Ellipsoid.Ellipsoid ellipsoid = Datum.ReferenceEllipsoid;
double a = ellipsoid.SemiMajorAxis;
double b = ellipsoid.SemiMinorAxis;
double e2Squared = ((a * a) - (b * b)) / (b * b);
double f = ellipsoid.Flattening;
double eSquared = (2 * f) - (f * f);
double p = Math.Sqrt((x * x) + (y * y));
double theta = Math.Atan((z * a) / (p * b));
double phi = Math.Atan((z + (e2Squared * b * Util.sinCubed(theta)))
/ (p - eSquared * a * Util.cosCubed(theta)));
double lambda = Math.Atan2(y, x);
double nphi = a / Math.Sqrt(1 - eSquared * Util.sinSquared(phi));
double h = (p / Math.Cos(phi)) - nphi;
return new LatLng(Util.ToDegrees(phi), Util.ToDegrees(lambda), h,
WGS84Datum.Instance);
}
/// <summary>
/// Gets or sets the x co-ordinate.
/// </summary>
/// <value>The x co-ordinate.</value>
public double X
{
get
{
return x;
}
set
{
x = value;
}
}
/// <summary>
/// Gets or sets the y co-ordinate.
/// </summary>
/// <value>The y co-ordinate.</value>
public double Y
{
get
{
return y;
}
set
{
y = value;
}
}
/// <summary>
/// Gets or sets the the z co-ordinate.
/// </summary>
/// <value>The the z co-ordinate.</value>
public double Z
{
get
{
return z;
}
set
{
z = value;
}
}
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current ECEF reference.
/// </summary>
/// <returns>
/// A <see cref="T:System.String"/> that represents the current ECEF reference.
/// </returns>
public override string ToString()
{
return "(" + x + "," + y + "," + z + ")";
}
}
}