/
GeoCoordinate.cs
341 lines (313 loc) · 13.9 KB
/
GeoCoordinate.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Globalization;
namespace GeoCoordinatePortable
{
/// <summary>
/// Represents a geographical location that is determined by latitude and longitude
/// coordinates. May also include altitude, accuracy, speed, and course information.
/// </summary>
public class GeoCoordinate : IEquatable<GeoCoordinate>
{
/// <summary>
/// Represents a <see cref="GeoCoordinate"/> object that has unknown latitude and longitude fields.
/// </summary>
public static readonly GeoCoordinate Unknown = new GeoCoordinate();
private double _course;
private double _horizontalAccuracy;
private double _latitude;
private double _longitude;
private double _speed;
private double _verticalAccuracy;
/// <summary>
/// Initializes a new instance of GeoCoordinate that has no data fields set.
/// </summary>
public GeoCoordinate()
: this(double.NaN, double.NaN)
{
}
/// <summary>
/// Initializes a new instance of the GeoCoordinate class from latitude and longitude data.
/// </summary>
/// <param name="latitude">The latitude of the location. May range from -90.0 to 90.0. </param>
/// <param name="longitude">The longitude of the location. May range from -180.0 to 180.0.</param>
/// <exception cref="T:System.ArgumentOutOfRangeException">Latitude or longitude is out of range.</exception>
public GeoCoordinate(double latitude, double longitude)
: this(latitude, longitude, double.NaN)
{
}
/// <summary>
/// Initializes a new instance of the GeoCoordinate class from latitude, longitude, and altitude data.
/// </summary>
/// <param name="latitude">Latitude. May range from -90.0 to 90.0.</param>
/// <param name="longitude">Longitude. May range from -180.0 to 180.0</param>
/// <param name="altitude">The altitude in meters. May be negative, 0, positive, or Double.NaN, if unknown.</param>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// latitude, longitude or altitude is out of range.
/// </exception>
public GeoCoordinate(double latitude, double longitude, double altitude)
: this(latitude, longitude, altitude, double.NaN, double.NaN, double.NaN, double.NaN)
{
}
/// <summary>
/// Initializes a new instance of the GeoCoordinate class from latitude, longitude, altitude, horizontal accuracy,
/// vertical accuracy, speed, and course.
/// </summary>
/// <param name="latitude">The latitude of the location. May range from -90.0 to 90.0.</param>
/// <param name="longitude">The longitude of the location. May range from -180.0 to 180.0.</param>
/// <param name="altitude">The altitude in meters. May be negative, 0, positive, or Double.NaN, if unknown.</param>
/// <param name="horizontalAccuracy">
/// The accuracy of the latitude and longitude coordinates, in meters. Must be greater
/// than or equal to 0. If a value of 0 is supplied to this constructor, the HorizontalAccuracy property will be set to
/// Double.NaN.
/// </param>
/// <param name="verticalAccuracy">
/// The accuracy of the altitude, in meters. Must be greater than or equal to 0. If a value
/// of 0 is supplied to this constructor, the VerticalAccuracy property will be set to Double.NaN.
/// </param>
/// <param name="speed">
/// The speed measured in meters per second. May be negative, 0, positive, or Double.NaN, if unknown.
/// A negative speed can indicate moving in reverse.
/// </param>
/// <param name="course">
/// The direction of travel, rather than orientation. This parameter is measured in degrees relative
/// to true north. Must range from 0 to 360.0, or be Double.NaN.
/// </param>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// If latitude, longitude, horizontalAccuracy, verticalAccuracy, course is out of range.
/// </exception>
public GeoCoordinate(double latitude, double longitude, double altitude, double horizontalAccuracy,
double verticalAccuracy, double speed, double course)
{
Latitude = latitude;
Longitude = longitude;
Altitude = altitude;
HorizontalAccuracy = horizontalAccuracy;
VerticalAccuracy = verticalAccuracy;
Speed = speed;
Course = course;
}
/// <summary>
/// Gets or sets the latitude of the GeoCoordinate.
/// </summary>
/// <returns>
/// Latitude of the location.
/// </returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">Latitude is set outside the valid range.</exception>
public double Latitude
{
get { return _latitude; }
set
{
if (value > 90.0 || value < -90.0)
{
throw new ArgumentOutOfRangeException("Latitude", "Argument must be in range of -90 to 90");
}
_latitude = value;
}
}
/// <summary>
/// Gets or sets the longitude of the GeoCoordinate.
/// </summary>
/// <returns>
/// The longitude.
/// </returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">Longitude is set outside the valid range.</exception>
public double Longitude
{
get { return _longitude; }
set
{
if (value > 180.0 || value < -180.0)
{
throw new ArgumentOutOfRangeException("Longitude", "Argument must be in range of -180 to 180");
}
_longitude = value;
}
}
/// <summary>
/// Gets or sets the accuracy of the latitude and longitude that is given by the GeoCoordinate, in meters.
/// </summary>
/// <returns>
/// The accuracy of the latitude and longitude, in meters.
/// </returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">HorizontalAccuracy is set outside the valid range.</exception>
public double HorizontalAccuracy
{
get { return _horizontalAccuracy; }
set
{
if (value < 0.0)
throw new ArgumentOutOfRangeException("HorizontalAccuracy", "Argument must be non negative");
_horizontalAccuracy = value == 0.0 ? double.NaN : value;
}
}
/// <summary>
/// Gets or sets the accuracy of the altitude given by the GeoCoordinate, in meters.
/// </summary>
/// <returns>
/// The accuracy of the altitude, in meters.
/// </returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">VerticalAccuracy is set outside the valid range.</exception>
public double VerticalAccuracy
{
get { return _verticalAccuracy; }
set
{
if (value < 0.0)
throw new ArgumentOutOfRangeException("VerticalAccuracy", "Argument must be non negative");
_verticalAccuracy = value == 0.0 ? double.NaN : value;
}
}
/// <summary>
/// Gets or sets the speed in meters per second.
/// </summary>
/// <returns>
/// The speed in meters per second. The speed must be greater than or equal to zero, or Double.NaN.
/// </returns>
/// <exception cref="System.ArgumentOutOfRangeException">Speed is set outside the valid range.</exception>
public double Speed
{
get { return _speed; }
set
{
if (value < 0.0)
throw new ArgumentOutOfRangeException("speed", "Argument must be non negative");
_speed = value;
}
}
/// <summary>
/// Gets or sets the heading in degrees, relative to true north.
/// </summary>
/// <returns>
/// The heading in degrees, relative to true north.
/// </returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">Course is set outside the valid range.</exception>
public double Course
{
get { return _course; }
set
{
if (value < 0.0 || value > 360.0)
throw new ArgumentOutOfRangeException("course", "Argument must be in range 0 to 360");
_course = value;
}
}
/// <summary>
/// Gets a value that indicates whether the GeoCoordinate does not contain latitude or longitude data.
/// </summary>
/// <returns>
/// true if the GeoCoordinate does not contain latitude or longitude data; otherwise, false.
/// </returns>
public bool IsUnknown => Equals(Unknown);
/// <summary>
/// Gets the altitude of the GeoCoordinate, in meters.
/// </summary>
/// <returns>
/// The altitude, in meters.
/// </returns>
public double Altitude { get; set; }
/// <summary>
/// Determines if the GeoCoordinate object is equivalent to the parameter, based solely on latitude and longitude.
/// </summary>
/// <returns>
/// true if the GeoCoordinate objects are equal; otherwise, false.
/// </returns>
/// <param name="other">The GeoCoordinate object to compare to the calling object.</param>
public bool Equals(GeoCoordinate other)
{
if (ReferenceEquals(other, null))
return false;
var num = Latitude;
if (!num.Equals(other.Latitude))
return false;
num = Longitude;
return num.Equals(other.Longitude);
}
/// <summary>
/// Determines whether two GeoCoordinate objects refer to the same location.
/// </summary>
/// <returns>
/// true, if the GeoCoordinate objects are determined to be equivalent; otherwise, false.
/// </returns>
/// <param name="left">The first GeoCoordinate to compare.</param>
/// <param name="right">The second GeoCoordinate to compare.</param>
public static bool operator ==(GeoCoordinate left, GeoCoordinate right)
{
if (ReferenceEquals(left, null))
return ReferenceEquals(right, null);
return left.Equals(right);
}
/// <summary>
/// Determines whether two GeoCoordinate objects correspond to different locations.
/// </summary>
/// <returns>
/// true, if the GeoCoordinate objects are determined to be different; otherwise, false.
/// </returns>
/// <param name="left">The first GeoCoordinate to compare.</param>
/// <param name="right">The second GeoCoordinate to compare.</param>
public static bool operator !=(GeoCoordinate left, GeoCoordinate right)
{
return !(left == right);
}
/// <summary>
/// Returns the distance between the latitude and longitude coordinates that are specified by this GeoCoordinate and
/// another specified GeoCoordinate.
/// </summary>
/// <returns>
/// The distance between the two coordinates, in meters.
/// </returns>
/// <param name="other">The GeoCoordinate for the location to calculate the distance to.</param>
public double GetDistanceTo(GeoCoordinate other)
{
if (double.IsNaN(Latitude) || double.IsNaN(Longitude) || double.IsNaN(other.Latitude) ||
double.IsNaN(other.Longitude))
{
throw new ArgumentException("Argument latitude or longitude is not a number");
}
var d1 = Latitude * (Math.PI / 180.0);
var num1 = Longitude * (Math.PI / 180.0);
var d2 = other.Latitude * (Math.PI / 180.0);
var num2 = other.Longitude * (Math.PI / 180.0) - num1;
var d3 = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) +
Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0);
return 6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3)));
}
/// <summary>
/// Serves as a hash function for the GeoCoordinate.
/// </summary>
/// <returns>
/// A hash code for the current GeoCoordinate.
/// </returns>
public override int GetHashCode()
{
return Latitude.GetHashCode() ^ Longitude.GetHashCode();
}
/// <summary>
/// Determines if a specified GeoCoordinate is equal to the current GeoCoordinate, based solely on latitude and
/// longitude.
/// </summary>
/// <returns>
/// true, if the GeoCoordinate objects are equal; otherwise, false.
/// </returns>
/// <param name="obj">The object to compare the GeoCoordinate to.</param>
public override bool Equals(object obj)
{
return Equals(obj as GeoCoordinate);
}
/// <summary>
/// Returns a string that contains the latitude and longitude.
/// </summary>
/// <returns>
/// A string that contains the latitude and longitude, separated by a comma.
/// </returns>
public override string ToString()
{
if (this == Unknown)
{
return "Unknown";
}
return
$"{Latitude.ToString("G", CultureInfo.InvariantCulture)}, {Longitude.ToString("G", CultureInfo.InvariantCulture)}";
}
}
}