-
Notifications
You must be signed in to change notification settings - Fork 4
/
Longitude.cs
2449 lines (2267 loc) · 106 KB
/
Longitude.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Text;
using System.Globalization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
#if !PocketPC || DesignTime
using System.ComponentModel;
#endif
namespace GeoFramework
{
/// <summary>
/// Represents a line of constant distance east or west from the Prime Meridian.
/// </summary>
/// <remarks>
/// <para>Longitudes measure a distance either East or West from the Prime Meridian, an
/// imaginary line which passes from the North Pole, through the
/// <see href="http://www.nmm.ac.uk/">Royal Observatory in Greenwich, England, and on
/// to the South Pole</see>. Longitudes can range from -180 to 180°, with the Prime
/// Meridian at 0°. Latitudes are commonly paired with Longitudes to mark a specific
/// location on Earth's surface.</para>
/// <para>Latitudes are expressed in either of two major formats. The first format uses
/// only positive numbers and the letter "E" or "W" to indicate the hemisphere (i.e.
/// "94°E" or "32°W"). The second format allows negative numbers an omits the single
/// character (i.e. 94 or -32).</para>
/// <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>
/// <seealso cref="Azimuth">Azimuth Class</seealso>
/// <seealso cref="Elevation">Elevation Class</seealso>
/// <seealso cref="Latitude">Latitude Class</seealso>
/// <seealso cref="Longitude">Longitude Class</seealso>
/// <example>
/// These examples create new instances of Longitude objects.
/// <code lang="VB" description="Create an angle of 90°">
/// Dim MyLongitude As New Longitude(90)
/// </code>
/// <code lang="CS" description="Create an angle of 90°">
/// Longitude MyLongitude = new Longitude(90);
/// </code>
/// <code lang="C++" description="Create an angle of 90°">
/// Longitude MyLongitude = new Longitude(90);
/// </code>
/// <code lang="VB" description="Create an angle of 105°30'21.4">
/// Dim MyLongitude1 As New Longitude(105, 30, 21.4)
/// </code>
/// <code lang="CS" description="Create an angle of 105°30'21.4">
/// Longitude MyLongitude = new Longitude(105, 30, 21.4);
/// </code>
/// <code lang="C++" description="Create an angle of 105°30'21.4">
/// Longitude MyLongitude = new Longitude(105, 30, 21.4);
/// </code>
/// </example>
#if !PocketPC || DesignTime
[TypeConverter("GeoFramework.Design.LongitudeConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=d77afaeb30e3236a")]
#endif
public struct Longitude : IFormattable, IComparable<Longitude>, IEquatable<Longitude>, ICloneable<Longitude>, IXmlSerializable
{
private double _DecimalDegrees;
#region Constants
private const int MaximumPrecisionDigits = 12;
#endregion
#region Fields
/// <summary>Represents a longitude of 0°.</summary>
/// <remarks>
/// <para>The Prime Meridian, located at 0°E (and 0°W), also known as the "Greenwich" Meridian was chosen as the
/// Prime Meridian of the World in 1884. Forty-one delegates from 25 nations met in
/// Washington, D.C. for the International Meridian Conference. By the end of the
/// conference, Greenwich had won the prize of Longitude 0° by a vote of 22 to 1
/// against (San Domingo), with 2 abstentions (France and Brazil).</para>
/// <para>The Prime Meridian is also significant in that it marks the location from
/// which all time zones are measured. Times displayed as "Zulu," "UTC," or "GMT" are
/// all talking about times adjusted to the Greenwich time zone.</para>
/// <para>Before the Prime Meridian, almost every town in the world kept its own local
/// time. There were no national or international conventions which set how time should
/// be measured, or when the day would begin and end, or even what length an hour might
/// be!</para>
/// </remarks>
public static readonly Longitude PrimeMeridian = new Longitude(0.0);
/// <summary>Represents a longitude 180°.</summary>
/// <remarks>
/// This value of 180°W (also 180°E) marks the longitude located on the opposite side of the Earth from the
/// Prime Meridian. It runs approximately through the
/// <a href="http://greenwichmeridian.com/date-line.htm">International Date Line</a>
/// (between Alaska and Russia).
/// </remarks>
public static readonly Longitude InternationalDateline = new Longitude(180.0);
/// <summary>Represents a longitude of 0°.</summary>
public static readonly Longitude Empty = new Longitude(0.0);
/// <summary>Represents the minimum possible longitude of -180°.</summary>
/// <remarks>
/// This member is provided for completeness and is equivalent to the
/// <see cref="PrimeMeridian">PrimeMeridian</see> shared field.
/// </remarks>
public static readonly Longitude Minimum = new Longitude(-180.0);
/// <summary>Represents the maximum possible longitude of 180°.</summary>
/// <remarks>
/// This value of 180°W (also 180°E) marks the longitude located on the opposite side of the Earth from the
/// Prime Meridian. It runs approximately through the
/// <a href="http://greenwichmeridian.com/date-line.htm">International Date Line</a>
/// (between Alaska and Russia).
/// </remarks>
public static readonly Longitude Maximum = new Longitude(180.0);
/// <summary>
/// Represents an invalid or unspecified value.
/// </summary>
public static readonly Longitude Invalid = new Longitude(double.NaN);
#endregion
#region Constructors
/// <summary>Creates a new instance with the specified decimal degrees.</summary>
/// <example>
/// This example demonstrates how to create an angle with a measurement of 90°.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(90)
/// </code>
/// <code lang="CS">
/// Longitude MyLongitude = new Longitude(90);
/// </code>
/// </example>
/// <returns>An <strong>Longitude</strong> containing the specified value.</returns>
public Longitude(double decimalDegrees)
{
// Set the decimal degrees value
_DecimalDegrees = decimalDegrees;
}
/// <summary>
/// Creates a new instance with the specified decimal degrees and hemisphere.
/// </summary>
/// <param name="decimalDegrees">A <strong>Double</strong> specifying the number of hours.</param>
/// <param name="hemisphere">A value from the <strong>LongitudeHemisphere</strong> enumeration.</param>
/// <example>
/// This example creates a new Longitude of 39°30' north.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(39.5, LongitudeHemisphere.North)
/// </code>
/// <code lang="C#">
/// Longitude MyLongitude = new Longitude(39.5, LongitudeHemisphere.North);
/// </code>
/// This example creates a new Longitude of 39°30 south.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(39.5, LongitudeHemisphere.South)
/// </code>
/// <code lang="C#">
/// Longitude MyLongitude = new Longitude(39.5, LongitudeHemisphere.South);
/// </code>
/// </example>
public Longitude(double decimalDegrees, LongitudeHemisphere hemisphere)
{
_DecimalDegrees = ToDecimalDegrees(decimalDegrees, hemisphere);
}
/// <summary>Creates a new instance with the specified degrees.</summary>
/// <returns>An <strong>Longitude</strong> containing the specified value.</returns>
/// <param name="hours">
/// An <strong>Integer</strong> indicating the amount of degrees, typically between 0
/// and 360.
/// </param>
public Longitude(int hours)
{
_DecimalDegrees = ToDecimalDegrees(hours);
}
/// <summary>Creates a new instance with the specified hours, minutes and
/// seconds.</summary>
/// <example>
/// This example demonstrates how to create an angular measurement of 34°12'29.2 in
/// hours, minutes and seconds.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(34, 12, 29.2)
/// </code>
/// <code lang="CS">
/// Longitude MyLongitude = new Longitude(34, 12, 29.2);
/// </code>
/// </example>
/// <returns>An <strong>Longitude</strong> containing the specified value.</returns>
public Longitude(int hours, int minutes, double seconds)
{
_DecimalDegrees = ToDecimalDegrees(hours, minutes, seconds);
}
/// <summary>Creates a new instance using the specified decimal degrees and
/// hemisphere.</summary>
/// <remarks>
/// <para>This constructor is typically used to create a longitude when decimal degrees
/// are always expressed as a positive number. Since the hemisphere property is set
/// <em>after</em> the DecimalDegrees property is set, the DecimalDegrees is adjusted
/// automatically to be positive for the eastern hemisphere and negative for the
/// western hemisphere.</para>
///
/// <para>If the parameters conflict with each other, the <strong>Hemisphere</strong>
/// parameter takes precedence. Therefore, a value of "-19°E" will become "19°E"
/// (without the negative sign) with no exception being thrown.</para>
/// </remarks>
/// <param name="hemisphere">A value from the <strong>LatitudeHemisphere</strong>
/// enumeration.</param>
public Longitude(int hours, int minutes, double seconds, LongitudeHemisphere hemisphere)
{
_DecimalDegrees = ToDecimalDegrees(hours, minutes, seconds, hemisphere);
}
/// <summary>Creates a new instance with the specified hours and decimal minutes.</summary>
/// <example>
/// This example demonstrates how an angle can be created when only the hours and
/// minutes (in decimal form) are known. This creates a value of 12°42.345'.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(12, 42.345)
/// </code>
/// <code lang="VB">
/// Longitude MyLongitude = new Longitude(12, 42.345);
/// </code>
/// </example>
/// <remarks>An <strong>Longitude</strong> containing the specified value.</remarks>
public Longitude(int hours, double decimalMinutes)
{
_DecimalDegrees = ToDecimalDegrees(hours, decimalMinutes);
}
/// <summary>
/// Creates a new instance with the specified hours, decimal minutes, and hemisphere.
/// </summary>
/// <param name="hours">An <strong>Integer</strong> specifying the number of hours.</param>
/// <param name="minutes">An <strong>Integer</strong> specifying the number of minutes.</param>
/// <param name="hemisphere">A value from the <strong>LongitudeHemisphere</strong> enumeration.</param>
/// <example>
/// This example creates a new Longitude of 39°12.34' north.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(39, 12.34, LongitudeHemisphere.North)
/// </code>
/// <code lang="C#">
/// Longitude MyLongitude = new Longitude(39, 12.34, LongitudeHemisphere.North);
/// </code>
/// This example creates a new Longitude of 39°12.34 south.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(39, 12.34, LongitudeHemisphere.South)
/// </code>
/// <code lang="C#">
/// Longitude MyLongitude = new Longitude(39, 12.34, LongitudeHemisphere.South);
/// </code>
/// </example>
public Longitude(int hours, double decimalMinutes, LongitudeHemisphere hemisphere)
{
_DecimalDegrees = ToDecimalDegrees(hours, decimalMinutes, hemisphere);
}
/// <summary>Creates a new instance using the specified string-based measurement.</summary>
/// <remarks>
/// <para>A <strong>String</strong> in any of the following formats (or variation
/// depending on the local culture):</para>
///
/// <para>
/// <table cellspacing="0" cols="4" cellpadding="2" width="100%">
/// <tbody>
/// <tr>
/// <td>hh</td>
/// <td>hh.h</td>
/// <td>hh mm</td>
/// <td>hh mm.mm</td>
/// </tr>
/// <tr>
/// <td>hh mm ss</td>
/// <td>hh mm ss.sss</td>
/// <td>hhi</td>
/// <td>hh.hi</td>
/// </tr>
/// <tr>
/// <td>hh mmi</td>
/// <td>hh mm i</td>
/// <td>hh mm.mi</td>
/// <td>hh mm.m i</td>
/// </tr>
/// <tr>
/// <td>hh mm ssi</td>
/// <td>hh mm ss i</td>
/// <td>hh mm ss.si</td>
/// <td>hh mm ss.s i</td>
/// </tr>
/// <tr>
/// <td>hhhmmssi</td>
/// <td></td>
/// <td></td>
/// <td></td>
/// </tr>
/// </tbody>
/// </table>
/// </para>
///
/// <para>Where <strong>h</strong> represents hours, <strong>m</strong> represents
/// minutes, <strong>s</strong> represents seconds, and <strong>i</strong> represents a
/// one-letter hemisphere indicator of "E" or "W." Any non-numeric character between
/// numbers is considered a delimiter. Thus, a value of <strong>12°34'56.78"</strong>
/// or even <strong>12A34B56.78C</strong> is treated the same as <strong>12 34
/// 56.78</strong>.</para>
/// </remarks>
/// <seealso cref="Longitude.Parse">Parse Method</seealso>
/// <example>
/// This example creates a new instance by parsing a string. (NOTE: The double-quote is
/// doubled up to represent a single double-quote in the string.)
/// <code lang="VB">
/// Dim MyLongitude As New Longitude("123°45'67.8""")
/// </code>
/// <code lang="CS">
/// Longitude MyLongitude = new Longitude("123°45'67.8\"");
/// </code>
/// </example>
/// <returns>An <strong>Longitude</strong> containing the specified value.</returns>
/// <exception cref="ArgumentNullException" caption="ArgumentNullException">The Parse method requires a decimal or sexagesimal measurement.</exception>
/// <exception cref="FormatException" caption="FormatException">Only the right-most portion of a sexagesimal measurement can be a fractional value.</exception>
/// <exception cref="FormatException" caption="FormatException">Extra characters were encountered while parsing an angular measurement. Only hours, minutes, and seconds are allowed.</exception>
/// <exception cref="FormatException" caption="FormatException">The specified text was not fully understood as an angular measurement.</exception>
public Longitude(string value)
: this(value, CultureInfo.CurrentCulture)
{ }
/// <summary>Creates a new instance using the specified string-based measurement.</summary>
/// <remarks>
/// <para>A <strong>String</strong> in any of the following formats (or variation
/// depending on the local culture):</para>
///
/// <para>
/// <table cellspacing="0" cols="4" cellpadding="2" width="100%">
/// <tbody>
/// <tr>
/// <td>hh</td>
/// <td>hh.h</td>
/// <td>hh mm</td>
/// <td>hh mm.mm</td>
/// </tr>
/// <tr>
/// <td>hh mm ss</td>
/// <td>hh mm ss.sss</td>
/// <td>hhi</td>
/// <td>hh.hi</td>
/// </tr>
/// <tr>
/// <td>hh mmi</td>
/// <td>hh mm i</td>
/// <td>hh mm.mi</td>
/// <td>hh mm.m i</td>
/// </tr>
/// <tr>
/// <td>hh mm ssi</td>
/// <td>hh mm ss i</td>
/// <td>hh mm ss.si</td>
/// <td>hh mm ss.s i</td>
/// </tr>
/// <tr>
/// <td>hhhmmssi</td>
/// <td></td>
/// <td></td>
/// <td></td>
/// </tr>
/// </tbody>
/// </table>
/// </para>
///
/// <para>Where <strong>h</strong> represents hours, <strong>m</strong> represents
/// minutes, <strong>s</strong> represents seconds, and <strong>i</strong> represents a
/// one-letter hemisphere indicator of "E" or "W." Any non-numeric character between
/// numbers is considered a delimiter. Thus, a value of <strong>12°34'56.78"</strong>
/// or even <strong>12A34B56.78C</strong> is treated the same as <strong>12 34
/// 56.78</strong>.</para>
/// </remarks>
/// <exception cref="ArgumentNullException" caption="ArgumentNullException">The Parse method requires a decimal or sexagesimal measurement.</exception>
/// <exception cref="FormatException" caption="FormatException">Only the right-most portion of a sexagesimal measurement can be a fractional value.</exception>
/// <exception cref="FormatException" caption="FormatException">Extra characters were encountered while parsing an angular measurement. Only hours, minutes, and seconds are allowed.</exception>
/// <exception cref="FormatException" caption="FormatException">The specified text was not fully understood as an angular measurement.</exception>
/// <summary>
/// Creates a new instance by converting the specified string using the specified
/// culture.
/// </summary>
/// <param name="value">
/// A <strong>String</strong> describing an angle in the form of decimal degrees or a
/// sexagesimal.
/// </param>
/// <param name="culture">
/// A <strong>CultureInfo</strong> object describing the numeric format to use during
/// conversion.
/// </param>
public Longitude(string value, CultureInfo culture)
{
// Is the value null or empty?
if (value == null || value.Length == 0)
{
// Yes. Set to zero
_DecimalDegrees = 0;
return;
}
// Default to the current culture
if (culture == null)
culture = CultureInfo.CurrentCulture;
// We'll be replacing and trimming a lot, so use a Stringbuilder
StringBuilder NewValue = new StringBuilder(value);
// Try to extract the hemisphere
LongitudeHemisphere hemisphere = LongitudeHemisphere.None;
if (value.IndexOf('E') != -1
// Ignore the "E-002" type scientific notation
&& value.IndexOf('E') != value.IndexOf("E-"))
{
hemisphere = LongitudeHemisphere.East;
NewValue.Replace("E", "");
}
else if (value.IndexOf('W') != -1)
{
hemisphere = LongitudeHemisphere.West;
NewValue.Replace("W", "");
}
else if (value.IndexOf('e') != -1
// Ignore the "E-002" type scientific notation
&& value.IndexOf('e') != value.IndexOf("e-"))
{
hemisphere = LongitudeHemisphere.East;
NewValue.Replace("e", "");
}
else if (value.IndexOf('w') != -1)
{
hemisphere = LongitudeHemisphere.West;
NewValue.Replace("w", "");
}
else if (value.StartsWith("-"))
{
hemisphere = LongitudeHemisphere.West;
}
// Yes. First, clean up the strings
try
{
// Clean up the string
NewValue.Replace("°", " ").Replace("'", " ").Replace("\"", " ").Replace(" ", " ");
// Now split the values into an array
string[] Values = NewValue.ToString().Trim().Split(' ');
// How many elements are in the array?
switch (Values.Length)
{
case 0:
// Return a blank Longitude
_DecimalDegrees = 0.0;
return;
case 1: // Decimal degrees
// Is it nothing?
if (Values[0].Length == 0)
{
_DecimalDegrees = 0.0;
return;
}
// Is it infinity?
else if (String.Compare(Values[0], Properties.Resources.Common_Infinity, true, culture) == 0)
{
_DecimalDegrees = double.PositiveInfinity;
return;
}
// Is it empty?
else if (String.Compare(Values[0], Properties.Resources.Common_Empty, true, culture) == 0)
{
_DecimalDegrees = 0.0;
return;
}
// Look at the number of digits, this might be HHHMMSS format.
else if (Values[0].Length == 7 && Values[0].IndexOf(culture.NumberFormat.NumberDecimalSeparator) == -1)
{
_DecimalDegrees = ToDecimalDegrees(
int.Parse(Values[0].Substring(0, 3), culture),
int.Parse(Values[0].Substring(3, 2), culture),
double.Parse(Values[0].Substring(5, 2), culture),
hemisphere);
break;
}
else if (Values[0].Length == 8 && Values[0][0] == '-' && Values[0].IndexOf(culture.NumberFormat.NumberDecimalSeparator) == -1)
{
_DecimalDegrees = ToDecimalDegrees(
int.Parse(Values[0].Substring(0, 4), culture),
int.Parse(Values[0].Substring(4, 2), culture),
double.Parse(Values[0].Substring(6, 2), culture),
hemisphere);
break;
}
else
{
_DecimalDegrees = ToDecimalDegrees(
double.Parse(Values[0], culture),
hemisphere);
break;
}
case 2: // Hours and decimal minutes
// If this is a fractional value, remember that it is
if (Values[0].IndexOf(culture.NumberFormat.NumberDecimalSeparator) != -1)
{
throw new ArgumentException(Properties.Resources.Longitude_OnlyRightmostIsDecimal, "value");
}
// Set decimal degrees
_DecimalDegrees = ToDecimalDegrees(
int.Parse(Values[0], culture),
float.Parse(Values[1], culture),
hemisphere);
break;
default: // Hours, minutes and seconds (most likely)
// If this is a fractional value, remember that it is
if (Values[0].IndexOf(culture.NumberFormat.NumberDecimalSeparator) != -1 || Values[0].IndexOf(culture.NumberFormat.NumberDecimalSeparator) != -1)
{
throw new ArgumentException(Properties.Resources.Longitude_OnlyRightmostIsDecimal, "value");
}
// Set decimal degrees
_DecimalDegrees = ToDecimalDegrees(
int.Parse(Values[0], culture),
int.Parse(Values[1], culture),
double.Parse(Values[2], culture),
hemisphere);
break;
}
}
catch (Exception ex)
{
#if PocketPC
throw new ArgumentException(Properties.Resources.Longitude_InvalidFormat, ex);
#else
throw new ArgumentException(Properties.Resources.Longitude_InvalidFormat, "value", ex);
#endif
}
}
/// <summary>
/// Creates a new instance by deserializing the specified XML.
/// </summary>
/// <param name="reader"></param>
public Longitude(XmlReader reader)
{
// Initialize all fields
_DecimalDegrees = Double.NaN;
// Deserialize the object from XML
ReadXml(reader);
}
#endregion
#region Public Properties
/// <summary>Returns the value of the angle as decimal degrees.</summary>
/// <value>A <strong>Double</strong> value.</value>
/// <remarks>This property returns the value of the angle as a single number.</remarks>
/// <seealso cref="Hours">Hours Property</seealso>
/// <seealso cref="Minutes">Minutes Property</seealso>
/// <seealso cref="Seconds">Seconds Property</seealso>
/// <example>
/// This example demonstrates how the
/// <see cref="DecimalDegrees"><strong>DecimalDegrees</strong></see> property is
/// calculated automatically when creating an angle using hours, minutes and seconds.
/// <code lang="VB">
/// ' Create an angle of 20°30'
/// Dim MyLongitude As New Longitude(20, 30)
/// ' Setting the DecimalMinutes recalculated other properties
/// Debug.WriteLine(MyLongitude.DecimalDegrees)
/// ' Output: "20.5" the same as 20°30'
/// </code>
/// <code lang="CS">
/// // Create an angle of 20°30'
/// Longitude MyLongitude = New Longitude(20, 30);
/// // Setting the DecimalMinutes recalculated other properties
/// Console.WriteLine(MyLongitude.DecimalDegrees)
/// // Output: "20.5" the same as 20°30'
/// </code>
/// </example>
public double DecimalDegrees
{
get
{
return _DecimalDegrees;
}
}
/// <summary>Returns the minutes and seconds as a single numeric value.</summary>
/// <seealso cref="Minutes">Minutes Property</seealso>
/// <seealso cref="DecimalDegrees">DecimalDegrees Property</seealso>
/// <value>A <strong>Double</strong> value.</value>
/// <remarks>
/// This property is used when minutes and seconds are represented as a single
/// decimal value.
/// </remarks>
/// <example>
/// This example demonstrates how the <strong>DecimalMinutes</strong> property is
/// automatically calculated when creating a new angle.
/// <code lang="VB">
/// ' Create an angle of 20°10'30"
/// Dim MyLongitude As New Longitude(20, 10, 30)
/// ' The DecimalMinutes property is automatically calculated
/// Debug.WriteLine(MyLongitude.DecimalMinutes)
/// ' Output: "10.5"
/// </code>
/// <code lang="CS">
/// // Create an angle of 20°10'30"
/// Longitude MyLongitude = new Longitude(20, 10, 30);
/// // The DecimalMinutes property is automatically calculated
/// Console.WriteLine(MyLongitude.DecimalMinutes)
/// // Output: "10.5"
/// </code>
/// </example>
public double DecimalMinutes
{
get
{
#if Framework20 && !PocketPC
return Math.Round(
(Math.Abs(
_DecimalDegrees - Math.Truncate(_DecimalDegrees)) * 60.0),
// Apparently we must round to two less places to preserve accuracy
MaximumPrecisionDigits - 2);
#else
return Math.Round(
(Math.Abs(
_DecimalDegrees - Truncate(_DecimalDegrees)) * 60.0), MaximumPrecisionDigits - 2);
#endif
}
}
/// <summary>Returns the integer hours (degrees) portion of an angular
/// measurement.</summary>
/// <seealso cref="Minutes">Minutes Property</seealso>
/// <seealso cref="Seconds">Seconds Property</seealso>
/// <value>An <strong>Integer</strong> value.</value>
/// <remarks>
/// This property is used in conjunction with the <see cref="Minutes">Minutes</see>
/// and <see cref="Seconds">Seconds</see> properties to create a full angular measurement.
/// This property is the same as <strong>DecimalDegrees</strong> without any fractional
/// value.
/// </remarks>
/// <example>
/// This example creates an angle of 60.5° then outputs the value of the
/// <strong>Hours</strong> property, 60.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(60.5)
/// Debug.WriteLine(MyLongitude.Hours)
/// ' Output: 60
/// </code>
/// <code lang="CS">
/// Longitude MyLongitude = new Longitude(60.5);
/// Console.WriteLine(MyLongitude.Hours);
/// // Output: 60
/// </code>
/// </example>
public int Hours
{
get
{
#if Framework20 && !PocketPC
return (int)Math.Truncate(_DecimalDegrees);
#else
return Truncate(_DecimalDegrees);
#endif
}
}
/// <summary>Returns the integer minutes portion of an angular measurement.</summary>
/// <seealso cref="Hours">Hours Property</seealso>
/// <seealso cref="Seconds">Seconds Property</seealso>
/// <remarks>
/// This property is used in conjunction with the <see cref="Hours">Hours</see> and
/// <see cref="Seconds">Seconds</see> properties to create a sexagesimal
/// measurement.
/// </remarks>
/// <value>An <strong>Integer</strong>.</value>
/// <example>
/// This example creates an angle of 45.5° then outputs the value of the
/// <strong>Minutes</strong> property, 30.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(45.5)
/// Debug.WriteLine(MyLongitude.Minutes)
/// ' Output: 30
/// </code>
/// <code lang="CS">
/// Longitude MyLongitude = new Longitude(45.5);
/// Console.WriteLine(MyLongitude.Minutes);
/// // Output: 30
/// </code>
/// </example>
public int Minutes
{
get
{
return (int)(
Math.Abs(
#if Framework20 && !PocketPC
Math.Truncate(
#else
Truncate(
#endif
Math.Round(
// Calculations appear to support one less digit than the maximum allowed precision
(_DecimalDegrees - Hours) * 60.0, MaximumPrecisionDigits - 1))));
}
}
/// <summary>Returns the seconds minutes portion of an angular measurement.</summary>
/// <remarks>
/// This property is used in conjunction with the <see cref="Hours">Hours</see> and
/// <see cref="Minutes">Minutes</see> properties to create a sexagesimal
/// measurement.
/// </remarks>
/// <seealso cref="Hours">Hours Property</seealso>
/// <seealso cref="Minutes">Minutes Property</seealso>
/// <value>A <strong>Double</strong> value.</value>
/// <example>
/// This example creates an angle of 45°10.5' then outputs the value of the
/// <strong>Seconds</strong> property, 30.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(45, 10.5)
/// Debug.WriteLine(MyLongitude.Seconds)
/// ' Output: 30
/// </code>
/// <code lang="CS">
/// Dim MyLongitude As New Longitude(45, 10.5);
/// Console.WriteLine(MyLongitude.Seconds);
/// // Output: 30
/// </code>
/// </example>
public double Seconds
{
get
{
return Math.Round(
(Math.Abs(_DecimalDegrees - Hours) * 60.0 - Minutes) * 60.0,
// This property appears to support one less digit than the maximum allowed
MaximumPrecisionDigits - 4);
}
}
/// <summary>Returns whether the longitude is east or west of the Prime Meridian.</summary>
/// <remarks>
/// <para>When this property changes, the DecimalDegrees property is adjusted: if the
/// hemisphere is <strong>West</strong>, a negative sign is placed in front of the
/// DecimalDegrees value, and vice versa.</para>
/// </remarks>
public LongitudeHemisphere Hemisphere
{
get
{
// And set the hemisphere
if (_DecimalDegrees < 0)
return LongitudeHemisphere.West;
else
return LongitudeHemisphere.East;
}
}
/// <summary>Returns the Universal Transverse Mercator zone number for this longitude.</summary>
public int UtmZoneNumber
{
get
{
#if Framework20 && !PocketPC
double LongitudeTemp = (DecimalDegrees + 180) - (int)Math.Truncate((DecimalDegrees + 180) / 360) * 360 - 180;
#else
double LongitudeTemp = (DecimalDegrees + 180) - Angle.Truncate((DecimalDegrees + 180) / 360) * 360 - 180;
#endif
//int ZoneNumber = 0;
// Adjust for special zone numbers
if (DecimalDegrees >= 56.0 && DecimalDegrees < 64.0 && LongitudeTemp >= 3.0 && LongitudeTemp < 12.0)
{
return 32;
}
// Special zones for Svalbard
else if (DecimalDegrees >= 72 && DecimalDegrees < 84.0)
{
if (LongitudeTemp >= 0.0 && LongitudeTemp < 9.0)
{
return 31;
}
else if (LongitudeTemp >= 9.0 && LongitudeTemp < 21.0)
{
return 33;
}
else if (LongitudeTemp >= 21.0 && LongitudeTemp < 33.0)
{
return 35;
}
else if (LongitudeTemp >= 33.0 && LongitudeTemp < 42.0)
{
return 37;
}
}
//else
{
#if Framework20 && !PocketPC
return (int)Math.Truncate((LongitudeTemp + 180) / 6) + 1;
#else
return Angle.Truncate((LongitudeTemp + 180) / 6) + 1;
#endif
}
//return ZoneNumber;
// int Zone;
// if (ToRadians() < Math.PI)
//#if Framework20 && !PocketPC
// Zone = (int)Math.Truncate(31 + (DecimalDegrees / 6.0));
//#else
// Zone = Truncate(31 + (DecimalDegrees / 6.0));
//#endif
// else
//#if Framework20 && !PocketPC
// Zone = (int)Math.Truncate((DecimalDegrees / 6.0) - 29);
//#else
// Zone = Truncate((DecimalDegrees / 6.0) - 29);
//#endif
// if (Zone > 60)
// Zone = 1;
// return Zone;
}
}
/// <summary>Indicates if the current instance has a non-zero value.</summary>
/// <value>
/// A <strong>Boolean</strong>, <strong>True</strong> if the
/// <strong>DecimalDegrees</strong> property is zero.
/// </value>
/// <seealso cref="Empty">Empty Field</seealso>
public bool IsEmpty
{
get
{
return (_DecimalDegrees == 0);
}
}
/// <summary>Indicates if the current instance represents an infinite value.</summary>
public bool IsInfinity
{
get
{
return double.IsInfinity(_DecimalDegrees);
}
}
/// <summary>
/// Indicates whether the value is invalid or unspecified.
/// </summary>
public bool IsInvalid
{
get { return double.IsNaN(_DecimalDegrees); }
}
/// <summary>Indicates whether the value has been normalized and is within the
/// allowed bounds of -180° and 180°.</summary>
public bool IsNormalized
{
get { return _DecimalDegrees >= -180 && _DecimalDegrees <= 180; }
}
#endregion
#region Public Methods
/// <summary>Returns the object with the smallest value.</summary>
/// <returns>The <strong>Longitude</strong> containing the smallest value.</returns>
/// <param name="value">An <strong>Longitude</strong> object to compare to the current instance.</param>
public Longitude LesserOf(Longitude value)
{
if (_DecimalDegrees < value.DecimalDegrees)
return this;
else
return value;
}
/// <returns>An <strong>Longitude</strong> containing the largest value.</returns>
/// <summary>Returns the object with the largest value.</summary>
/// <param name="value">An <strong>Longitude</strong> object to compare to the current instance.</param>
public Longitude GreaterOf(Longitude value)
{
if (_DecimalDegrees > value.DecimalDegrees)
return this;
else
return value;
}
/// <summary>Returns a value indicating the relative order of two objects.</summary>
/// <returns>A value of -1, 0, or 1 as documented by the IComparable interface.</returns>
/// <remarks>
/// This method allows collections of <strong>Longitude</strong> objects to be sorted.
/// The <see cref="DecimalDegrees">DecimalDegrees</see> property of each instance is compared.
/// </remarks>
/// <param name="value">An <strong>Longitude</strong> object to compare with.</param>
public int Compare(double value)
{
return _DecimalDegrees.CompareTo(value);
}
/// <summary>Returns an angle opposite of the current instance.</summary>
/// <returns>An <strong>Longitude</strong> representing the mirrored value.</returns>
/// <remarks>
/// This method returns the "opposite" of the current instance. The opposite is
/// defined as the point on the other side of an imaginary circle. For example, if an angle
/// is 0°, at the top of a circle, this method returns 180°, at the bottom of the
/// circle.
/// </remarks>
/// <example>
/// This example creates a new <strong>Longitude</strong> of 45° then calculates its mirror
/// of 225°. (45 + 180)
/// <code lang="VB" title="[New Example]">
/// Dim Longitude1 As New Longitude(45)
/// Dim Longitude2 As Longitude = Longitude1.Mirror()
/// Debug.WriteLine(Longitude2.ToString())
/// ' Output: 225
/// </code>
/// <code lang="CS" title="[New Example]">
/// Longitude Longitude1 = new Longitude(45);
/// Longitude Longitude2 = Longitude1.Mirror();
/// Console.WriteLine(Longitude2.ToString());
/// // Output: 225
/// </code>
/// </example>
public Longitude Mirror()
{
return Normalize().Multiply(-1.0);
}
/// <summary>Converts the current instance into radians.</summary>
/// <returns>A <see cref="Radian">Radian</see> object.</returns>
/// <remarks>
/// <para>This function is typically used to convert an angular measurement into
/// radians before performing a trigonometric function.
/// </para>
/// </remarks>
/// <seealso cref="Radian">Radian Class</seealso>
/// <overloads>Converts an angular measurement into radians before further processing.</overloads>
/// <example>
/// This example converts a measurement of 90° into radians.
/// <code lang="VB">
/// Dim MyLongitude As New Longitude(90)
/// Dim MyRadians As Radian = MyLongitude.ToRadians()
/// </code>
/// <code lang="CS">
/// Longitude MyLongitude = new Longitude(90);
/// Radian MyRadians = MyLongitude.ToRadians();
/// </code>
/// </example>
public Radian ToRadians()
{
return new Radian(_DecimalDegrees * Radian.RadiansPerDegree);
}
/// <summary>
/// Indicates if the current instance is East of the specified longitude.
/// </summary>
/// <param name="longitude">A <strong>Longitude</strong> object to examine.</param>
/// <returns>A <strong>Boolean</strong>, <strong>True</strong> if the current instance is more East than the specified instance.</returns>
public bool IsEastOf(Longitude longitude)
{
return IsGreaterThan(longitude);
}
/// <summary>
/// Indicates if the current instance is West of the specified longitude.
/// </summary>
/// <param name="longitude">A <strong>Longitude</strong> object to examine.</param>
/// <returns>A <strong>Boolean</strong>, <strong>True</strong> if the current instance is more West than the specified instance.</returns>
public bool IsWestOf(Longitude longitude)
{
return IsLessThan(longitude);
}
/// <remarks>
/// <para>This powerful method returns the current angular measurement in a specific
/// format. If no value for the format is specified, a format of
/// <strong>hhh°mm'SS.SS"I</strong> (adjusted to the current culture) will be used. The
/// resulting <strong>String</strong> can be converted back into an
/// <strong>Longitude</strong> via the
/// <see href="Angle.Parse">Parse</see> method so long as a delimiter separates each individual
/// value.</para>
/// </remarks>
/// <param name="format">
/// <para>A combination of symbols, spaces, and any of the following case-insensitive
/// letters: <strong>D</strong> or <strong>H</strong> for hours, <strong>M</strong> for
/// minutes, <strong>S</strong> for seconds, and <strong>I</strong> to indicate the
/// hemisphere. Here are some examples:</para>
///
/// <para>
/// <table cellspacing="0" cols="3" cellpadding="2" width="100%">
/// <tbody>
/// <tr>