-
Notifications
You must be signed in to change notification settings - Fork 4
/
Position3D.cs
445 lines (372 loc) · 16.2 KB
/
Position3D.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using System;
using System.Globalization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace GeoFramework
{
/// <summary>
/// Represents a position on Earth marked by latitude, longitude, and altitude.
/// </summary>
/// <remarks>
/// <para>Instances of this class are guaranteed to be thread-safe because the class is
/// immutable (its properties can only be changed via constructors).</para>
/// </remarks>
public struct Position3D : IFormattable, IEquatable<Position3D>, ICloneable<Position3D>, IXmlSerializable
{
private Position _Position;
private Distance _Altitude;
#region Constructors
public Position3D(Distance altitude, Position location)
{
_Position = location;
_Altitude = altitude;
}
public Position3D(Distance altitude, Longitude longitude, Latitude latitude)
{
_Position = new Position(longitude, latitude);
_Altitude = altitude;
}
/// <overloads>Creates a new instance.</overloads>
public Position3D(Distance altitude, Latitude latitude, Longitude longitude)
{
_Position = new Position(latitude, longitude);
_Altitude = altitude;
}
public Position3D(Longitude longitude, Latitude latitude, Distance altitude)
{
_Position = new Position(latitude, longitude);
_Altitude = altitude;
}
public Position3D(Latitude latitude, Longitude longitude, Distance altitude)
{
_Position = new Position(latitude, longitude);
_Altitude = altitude;
}
/// <summary>
/// Creates a new instance by parsing latitude and longitude from a single string.
/// </summary>
/// <param name="value">A <strong>String</strong> containing both a latitude and longitude to parse.</param>
public Position3D(string altitude, string location)
: this(altitude, location, CultureInfo.CurrentCulture)
{ }
/// <summary>
/// Creates a new instance by interpreting the specified latitude and longitude.
/// </summary>
/// <param name="latitude">A <strong>String</strong> describing a latitude in the current culture.</param>
/// <param name="longitude">A <strong>String</strong> describing a longitude in the current culture.</param>
/// <remarks>Latitude and longitude values are parsed using the current local culture. For better support
/// of international cultures, add a CultureInfo parameter.</remarks>
public Position3D(string altitude, string latitude, string longitude)
: this(altitude, latitude, longitude, CultureInfo.CurrentCulture)
{ }
/// <summary>
/// Creates a new instance by interpreting the specified latitude and longitude.
/// </summary>
/// <param name="latitude">A <strong>String</strong> describing a latitude in the current culture.</param>
/// <param name="longitude">A <strong>String</strong> describing a longitude in the current culture.</param>
/// <remarks>Latitude and longitude values are parsed using the current local culture. For better support
/// of international cultures, a CultureInfo parameter should be specified to indicate how numbers should
/// be parsed.</remarks>
public Position3D(string altitude, string latitude, string longitude, CultureInfo culture)
{
_Position = new Position(latitude, longitude, culture);
_Altitude = new Distance(altitude, culture);
}
/// <summary>
/// Creates a new instance by converting the specified string using the specific culture.
/// </summary>
/// <param name="value"></param>
/// <param name="culture"></param>
public Position3D(string altitude, string location, CultureInfo culture)
{
_Position = new Position(location, culture);
_Altitude = new Distance(altitude, culture);
}
/// <summary>
/// Upgrades a Position object to a Position3D object.
/// </summary>
/// <param name="position"></param>
public Position3D(Position position)
{
_Position = position;
_Altitude = Distance.Empty;
}
public Position3D(XmlReader reader)
{
// Initialize all fields
_Position = Position.Invalid;
_Altitude = Distance.Invalid;
// Deserialize the object from XML
ReadXml(reader);
}
#endregion
#region Public Properties
/// <summary>Returns the location's distance above sea level.</summary>
public Distance Altitude
{
get { return _Altitude; }
}
public Latitude Latitude
{
get { return _Position.Latitude; }
}
public Longitude Longitude
{
get { return _Position.Longitude; }
}
#endregion
#region Public Methods
public CartesianPoint ToCartesianPoint()
{
return ToCartesianPoint(Ellipsoid.Default);
}
public CartesianPoint ToCartesianPoint(Ellipsoid ellipsoid)
{
return _Position.ToCartesianPoint(ellipsoid, _Altitude);
}
#endregion
#region Operators
public static bool operator ==(Position3D left, Position3D right)
{
return left.Equals(right);
}
public static bool operator !=(Position3D left, Position3D right)
{
return !left.Equals(right);
}
public static Position3D operator +(Position3D left, Position3D right)
{
return left.Add(right);
}
public static Position3D operator -(Position3D left, Position3D right)
{
return left.Subtract(right);
}
public static Position3D operator *(Position3D left, Position3D right)
{
return left.Multiply(right);
}
public static Position3D operator /(Position3D left, Position3D right)
{
return left.Divide(right);
}
public Position3D Add(Position3D position)
{
return
new Position3D(this.Latitude.Add(position.Latitude.DecimalDegrees),
this.Longitude.Add(position.Longitude.DecimalDegrees),
this._Altitude.Add(position.Altitude));
}
public Position3D Subtract(Position3D position)
{
return
new Position3D(this.Latitude.Subtract(position.Latitude.DecimalDegrees),
this.Longitude.Subtract(position.Longitude.DecimalDegrees),
this._Altitude.Subtract(position.Altitude));
}
public Position3D Multiply(Position3D position)
{
return
new Position3D(this.Latitude.Multiply(position.Latitude.DecimalDegrees),
this.Longitude.Multiply(position.Longitude.DecimalDegrees),
this._Altitude.Multiply(position.Altitude));
}
public Position3D Divide(Position3D position)
{
return
new Position3D(this.Latitude.Divide(position.Latitude.DecimalDegrees),
this.Longitude.Divide(position.Longitude.DecimalDegrees),
this._Altitude.Divide(position.Altitude));
}
#endregion
/// <summary>
/// Returns whether the latitude, longitude and altitude are zero.
/// </summary>
public bool IsEmpty
{
get
{
return _Altitude.IsEmpty && _Position.IsEmpty;
}
}
#region Overrides
public override int GetHashCode()
{
return _Position.GetHashCode() ^ _Altitude.GetHashCode();
}
public override bool Equals(object obj)
{
if(obj is Position3D)
return Equals((Position3D)obj);
return false;
}
#endregion
#region Conversions
public static explicit operator Position3D(CartesianPoint value)
{
return value.ToPosition3D();
}
public static explicit operator string(Position3D value)
{
return value.ToString();
}
#endregion
#region IXmlSerializable
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
public void WriteXml(XmlWriter writer)
{
/* The position class uses the GML 3.0 specification for XML.
*
* <gml:pos>X Y</gml:pos>
*
*/
writer.WriteStartElement(Xml.GmlXmlPrefix, "pos", Xml.GmlXmlNamespace);
writer.WriteString(Longitude.DecimalDegrees.ToString("G17", CultureInfo.InvariantCulture));
writer.WriteString(" ");
writer.WriteString(Latitude.DecimalDegrees.ToString("G17", CultureInfo.InvariantCulture));
writer.WriteString(" ");
writer.WriteString(Altitude.ToMeters().Value.ToString("G17", CultureInfo.InvariantCulture));
writer.WriteEndElement();
}
public void ReadXml(XmlReader reader)
{
/* The position class uses the GML 3.0 specification for XML.
*
* <gml:pos>X Y</gml:pos>
*
* ... but it is also helpful to be able to READ older versions
* of GML, such as this one for GML 2.0:
*
* <gml:coord>
* <gml:X>double</gml:X>
* <gml:Y>double</gml:Y> // optional
* <gml:Z>double</gml:Z> // optional
* </gml:coord>
*
*/
// .NET Complains if we don't assign values
_Position = Position.Empty;
_Altitude = Distance.Empty;
Longitude longitude = Longitude.Empty;
Latitude latitude = Latitude.Empty;
// Move to the <gml:pos> or <gml:coord> element
if (!reader.IsStartElement("pos", Xml.GmlXmlNamespace)
&& !reader.IsStartElement("coord", Xml.GmlXmlNamespace))
reader.ReadStartElement();
switch (reader.LocalName.ToLower(CultureInfo.InvariantCulture))
{
case "pos":
// Read the "X Y" string, then split by the space between them
string[] Values = reader.ReadElementContentAsString().Split(' ');
// Deserialize the longitude
longitude = new Longitude(Values[0], CultureInfo.InvariantCulture);
// Deserialize the latitude
if (Values.Length >= 2)
latitude = new Latitude(Values[1], CultureInfo.InvariantCulture);
// Deserialize the altitude
if (Values.Length == 3)
_Altitude = Distance.FromMeters(double.Parse(Values[2], CultureInfo.InvariantCulture));
// Make the position
_Position = new Position(latitude, longitude);
break;
case "coord":
// Read the <gml:coord> start tag
reader.ReadStartElement();
// Now read up to 3 elements: X, and optionally Y or Z
for (int index = 0; index < 3; index++)
{
switch (reader.LocalName.ToLower(CultureInfo.InvariantCulture))
{
case "x":
longitude = new Longitude(reader.ReadElementContentAsDouble());
break;
case "y":
latitude = new Latitude(reader.ReadElementContentAsDouble());
break;
case "z":
// Read Z as meters (there's no unit type in the spec :P morons)
_Altitude = Distance.FromMeters(reader.ReadElementContentAsDouble());
break;
}
// If we're at an end element, stop
if (reader.NodeType == XmlNodeType.EndElement)
break;
}
// Make the position
_Position = new Position(latitude, longitude);
// Read the </gml:coord> end tag
reader.ReadEndElement();
break;
}
}
#endregion
#region IEquatable<Position3D>
/// <summary>
/// Compares the current instance to the specified position.
/// </summary>
/// <param name="value">A <strong>Position</strong> object to compare with.</param>
/// <returns>A <strong>Boolean</strong>, <strong>True</strong> if the values are identical.</returns>
/// <remarks>The two objects are compared at up to four digits of precision.</remarks>
public bool Equals(Position3D other)
{
return Latitude.Equals(other.Latitude)
&& Longitude.Equals(other.Longitude)
&& _Altitude.Equals(other.Altitude);
}
/// <summary>
/// Compares the current instance to the specified position using the specified numeric precision.
/// </summary>
/// <param name="value">A <strong>Position</strong> object to compare with.</param>
/// <param name="decimals">An <strong>Integer</strong> specifying the number of fractional digits to compare.</param>
/// <returns>A <strong>Boolean</strong>, <strong>True</strong> if the values are identical.</returns>
/// <remarks>This method is typically used when positions do not mark the same location unless they are
/// extremely close to one another. Conversely, a low or even negative value for <strong>Precision</strong>
/// allows positions to be considered equal even when they do not precisely match.</remarks>
public bool Equals(Position3D other, int decimals)
{
return Latitude.Equals(other.Latitude, decimals)
&& Longitude.Equals(other.Longitude, decimals)
&& _Altitude.Equals(other.Altitude, decimals);
}
#endregion
#region IFormattable Members
/// <summary>
/// Outputs the current instance as a string using the specified format and culture information.
/// </summary>
/// <returns></returns>
public string ToString(string format, IFormatProvider formatProvider)
{
CultureInfo culture = (CultureInfo)formatProvider;
if (culture == null)
culture = CultureInfo.CurrentCulture;
if (format == null || format.Length == 0)
format = "G";
// Output as latitude and longitude
return _Position.ToString(format, culture)
+ culture.TextInfo.ListSeparator
+ _Altitude.ToString(format, culture);
}
/// <summary>
/// Returns a coordinate which has been shifted the specified bearing and distance.
/// </summary>
/// <param name="bearing"></param>
/// <param name="distance"></param>
/// <param name="ellipsoid"></param>
/// <returns></returns>
public Position3D TranslateTo(Azimuth bearing, Distance distance, Ellipsoid ellipsoid)
{
return new Position3D(_Altitude, _Position.TranslateTo(bearing, distance, ellipsoid));
}
#endregion
#region ICloneable<Position3D> Members
public Position3D Clone()
{
return new Position3D(_Position);
}
#endregion
}
}