-
Notifications
You must be signed in to change notification settings - Fork 4
/
Latitude.cs
2192 lines (2013 loc) · 93.2 KB
/
Latitude.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 north or south of the equator.</summary>
/// <remarks>
/// <para>Latitudes measure a distance North or South away from the equator. Latitudes
/// can range from -90° (at the South pole) to 90° (the North pole), with 0°
/// representing the equator. 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 "N" or "S" to indicate the hemisphere (i.e.
/// "45°N" or "60°S"). The second format allows negative numbers an omits the single
/// character (i.e. 45 or -60).</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="Longitude">Longitude Class</seealso>
/// <seealso cref="Position">Position Class</seealso>
/// <seealso cref="Azimuth">Azimuth Class</seealso>
/// <seealso cref="Elevation">Elevation Class</seealso>
/// <seealso cref="Angle">Angle Class</seealso>
/// <example>
/// These examples create new instances of Latitude objects.
/// <code lang="VB" description="Create an angle of 90°">
/// Dim MyLatitude As New Latitude(90)
/// </code>
/// <code lang="CS" description="Create an angle of 90°">
/// Latitude MyLatitude = new Latitude(90);
/// </code>
/// <code lang="C++" description="Create an angle of 90°">
/// Latitude MyLatitude = new Latitude(90);
/// </code>
/// <code lang="VB" description="Create an angle of 105°30'21.4">
/// Dim MyLatitude1 As New Latitude(105, 30, 21.4)
/// </code>
/// <code lang="CS" description="Create an angle of 105°30'21.4">
/// Latitude MyLatitude = new Latitude(105, 30, 21.4);
/// </code>
/// <code lang="C++" description="Create an angle of 105°30'21.4">
/// Latitude MyLatitude = new Latitude(105, 30, 21.4);
/// </code>
/// </example>
#if !PocketPC || DesignTime
[TypeConverter("GeoFramework.Design.LatitudeConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=d77afaeb30e3236a")]
#endif
public struct Latitude : IFormattable, IComparable<Latitude>, IEquatable<Latitude>, ICloneable<Latitude>, IXmlSerializable
{
private double _DecimalDegrees;
#region Constants
private const int MaximumPrecisionDigits = 13;
#endregion
#region Fields
/// <summary>Represents a latitude of 0°.</summary>
public static readonly Latitude Equator = new Latitude(0.0);
/// <summary>Represents a latitude of 0°.</summary>
public static readonly Latitude Empty = new Latitude(0.0);
/// <summary>Represents a latitude of 23.5°S.</summary>
public static readonly Latitude TropicOfCapricorn = new Latitude(-23.5);
/// <summary>Represents a latitude of 23.5°N.</summary>
public static readonly Latitude TropicOfCancer = new Latitude(23.5);
/// <summary>Represents a latitude of 90°N.</summary>
public static readonly Latitude NorthPole = new Latitude(90.0);
/// <summary>Represents a latitude of 90°S.</summary>
public static readonly Latitude SouthPole = new Latitude(-90.0);
/// <summary>Represents the minimum possible latitude -90°.</summary>
public static readonly Latitude Minimum = new Latitude(-90.0);
/// <summary>Represents the maximum possible latitude of 90°.</summary>
public static readonly Latitude Maximum = new Latitude(90.0);
/// <summary>
/// Represents an invalid or unspecified value.
/// </summary>
public static readonly Latitude Invalid = new Latitude(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 MyLatitude As New Latitude(90)
/// </code>
/// <code lang="CS">
/// Latitude MyLatitude = new Latitude(90);
/// </code>
/// </example>
/// <returns>An <strong>Latitude</strong> containing the specified value.</returns>
public Latitude(double decimalDegrees)
{
_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>LatitudeHemisphere</strong> enumeration.</param>
/// <example>
/// This example creates a new latitude of 39°30' north.
/// <code lang="VB">
/// Dim MyLatitude As New Latitude(39.5, LatitudeHemisphere.North)
/// </code>
/// <code lang="C#">
/// Latitude MyLatitude = new Latitude(39.5, LatitudeHemisphere.North);
/// </code>
/// This example creates a new latitude of 39°30 south.
/// <code lang="VB">
/// Dim MyLatitude As New Latitude(39.5, LatitudeHemisphere.South)
/// </code>
/// <code lang="C#">
/// Latitude MyLatitude = new Latitude(39.5, LatitudeHemisphere.South);
/// </code>
/// </example>
public Latitude(double decimalDegrees, LatitudeHemisphere hemisphere)
{
_DecimalDegrees = ToDecimalDegrees(decimalDegrees, hemisphere);
}
/// <summary>Creates a new instance with the specified degrees.</summary>
/// <returns>An <strong>Latitude</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 Latitude(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 MyLatitude As New Latitude(34, 12, 29.2)
/// </code>
/// <code lang="CS">
/// Latitude MyLatitude = new Latitude(34, 12, 29.2);
/// </code>
/// </example>
/// <returns>An <strong>Latitude</strong> containing the specified value.</returns>
public Latitude(int hours, int minutes, double seconds)
{
_DecimalDegrees = ToDecimalDegrees(hours, minutes, seconds);
}
/// <summary>
/// Creates a new longitude with the specified hours, minutes, seconds, 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="seconds">An <strong>Double</strong> specifying the number of seconds.</param>
/// <param name="hemisphere">A value from the <strong>LatitudeHemisphere</strong> enumeration.</param>
/// <example>
/// This example creates a new latitude of 39°12'10" north.
/// <code lang="VB">
/// Dim MyLatitude As New Latitude(39, 12, 10, LatitudeHemisphere.North)
/// </code>
/// <code lang="C#">
/// Latitude MyLatitude = new Latitude(39, 12, 10, LatitudeHemisphere.North);
/// </code>
/// This example creates a new latitude of 39°12'10" south.
/// <code lang="VB">
/// Dim MyLatitude As New Latitude(39, 12, 10, LatitudeHemisphere.South)
/// </code>
/// <code lang="C#">
/// Latitude MyLatitude = new Latitude(39, 12, 10, LatitudeHemisphere.South);
/// </code>
/// </example>
public Latitude(int hours, int minutes, double seconds, LatitudeHemisphere 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 MyLatitude As New Latitude(12, 42.345)
/// </code>
/// <code lang="VB">
/// Latitude MyLatitude = new Latitude(12, 42.345);
/// </code>
/// </example>
/// <remarks>An <strong>Latitude</strong> containing the specified value.</remarks>
public Latitude(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="decimalMinutes">An <strong>Integer</strong> specifying the number of minutes.</param>
/// <param name="hemisphere">A value from the <strong>LatitudeHemisphere</strong> enumeration.</param>
/// <example>
/// This example creates a new latitude of 39°12.34' north.
/// <code lang="VB">
/// Dim MyLatitude As New Latitude(39, 12.34, LatitudeHemisphere.North)
/// </code>
/// <code lang="C#">
/// Latitude MyLatitude = new Latitude(39, 12.34, LatitudeHemisphere.North);
/// </code>
/// This example creates a new latitude of 39°12.34 south.
/// <code lang="VB">
/// Dim MyLatitude As New Latitude(39, 12.34, LatitudeHemisphere.South)
/// </code>
/// <code lang="C#">
/// Latitude MyLatitude = new Latitude(39, 12.34, LatitudeHemisphere.South);
/// </code>
/// </example>
public Latitude(int hours, double decimalMinutes, LatitudeHemisphere hemisphere)
{
_DecimalDegrees = ToDecimalDegrees(hours, decimalMinutes, hemisphere);
}
/// <summary>
/// Creates a new instance by parsing the specified string value.
/// </summary>
/// <param name="value">
/// <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></td>
/// <td></td>
/// </tr>
/// <tr>
/// <td>hh i</td>
/// <td>hh.h i</td>
/// <td>hh mm i</td>
/// <td>hh mm.mm i</td>
/// </tr>
/// <tr>
/// <td>hh mm ss i</td>
/// <td>hh mm ss.sss i</td>
/// <td></td>
/// <td></td>
/// </tr>
/// </tbody>
/// </table>
///
/// Where <strong>I</strong> represents a case-insensitive hemisphere indicator "N" or "S".
/// </para>
/// <para>Any non-numeric character between numbers is considered a delimiter. Thus, a
/// value of <strong>12°34'56.78"N</strong> or even <strong>12A34B56.78CN</strong> is
/// treated the same as <strong>12 34 56.78 N</strong>.</para>
/// </param>
/// <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 MyLatitude As New Latitude("23°45'67.8""N")
/// </code>
/// <code lang="CS">
/// Latitude MyLatitude = new Latitude("23°45'67.8\"N");
/// </code>
/// </example>
/// <returns>An <strong>Latitude</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 Latitude(string value)
: this(value, CultureInfo.CurrentCulture)
{ }
/// <summary>
/// Creates a new instance by parsing the specified string value.
/// </summary>
/// <param name="value">
/// <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></td>
/// <td></td>
/// </tr>
/// <tr>
/// <td>hh i</td>
/// <td>hh.h i</td>
/// <td>hh mm i</td>
/// <td>hh mm.mm i</td>
/// </tr>
/// <tr>
/// <td>hh mm ss i</td>
/// <td>hh mm ss.sss i</td>
/// <td></td>
/// <td></td>
/// </tr>
/// </tbody>
/// </table>
///
/// Where <strong>I</strong> represents a case-insensitive hemisphere indicator "N" or "S".
/// </para>
/// <para>Any non-numeric character between numbers is considered a delimiter. Thus, a
/// value of <strong>12°34'56.78"N</strong> or even <strong>12A34B56.78CN</strong> is
/// treated the same as <strong>12 34 56.78 N</strong>.</para>
/// </param>
/// <seealso cref="Parse">Parse</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 MyLatitude As New Latitude("23°45'67.8""N")
/// </code>
/// <code lang="CS">
/// Latitude MyLatitude = new Latitude("23°45'67.8\"N");
/// </code>
/// </example>
/// <returns>An <strong>Latitude</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 Latitude(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;
// Try to extract the hemisphere
LatitudeHemisphere hemisphere = LatitudeHemisphere.None;
if (value.IndexOf("N") != -1)
{
hemisphere = LatitudeHemisphere.North;
value = value.Replace("N", "");
}
else if (value.IndexOf("S") != -1)
{
hemisphere = LatitudeHemisphere.South;
value = value.Replace("S", "");
}
else if (value.IndexOf("n") != -1)
{
hemisphere = LatitudeHemisphere.North;
value = value.Replace("n", "");
}
else if (value.IndexOf("s") != -1)
{
hemisphere = LatitudeHemisphere.South;
value = value.Replace("s", "");
}
try
{
// Clean up the string
StringBuilder NewValue = new StringBuilder(value);
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 Latitude
_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.Latitude_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.Latitude_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.Latitude_InvalidFormat, ex);
#else
throw new ArgumentException(Properties.Resources.Latitude_InvalidFormat, "value", ex);
#endif
}
}
/// <summary>
/// Creates a new instance by deserializing the specified XML.
/// </summary>
/// <param name="reader"></param>
public Latitude(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 MyLatitude As New Latitude(20, 30)
/// ' Setting the DecimalMinutes recalculated other properties
/// Debug.WriteLine(MyLatitude.DecimalDegrees)
/// ' Output: "20.5" the same as 20°30'
/// </code>
/// <code lang="CS">
/// // Create an angle of 20°30'
/// Latitude MyLatitude = New Latitude(20, 30);
/// // Setting the DecimalMinutes recalculated other properties
/// Console.WriteLine(MyLatitude.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 MyLatitude As New Latitude(20, 10, 30)
/// ' The DecimalMinutes property is automatically calculated
/// Debug.WriteLine(MyLatitude.DecimalMinutes)
/// ' Output: "10.5"
/// </code>
/// <code lang="CS">
/// // Create an angle of 20°10'30"
/// Latitude MyLatitude = new Latitude(20, 10, 30);
/// // The DecimalMinutes property is automatically calculated
/// Console.WriteLine(MyLatitude.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 MyLatitude As New Latitude(60.5)
/// Debug.WriteLine(MyLatitude.Hours)
/// ' Output: 60
/// </code>
/// <code lang="CS">
/// Latitude MyLatitude = new Latitude(60.5);
/// Console.WriteLine(MyLatitude.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 MyLatitude As New Latitude(45.5)
/// Debug.WriteLine(MyLatitude.Minutes)
/// ' Output: 30
/// </code>
/// <code lang="CS">
/// Latitude MyLatitude = new Latitude(45.5);
/// Console.WriteLine(MyLatitude.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 MyLatitude As New Latitude(45, 10.5)
/// Debug.WriteLine(MyLatitude.Seconds)
/// ' Output: 30
/// </code>
/// <code lang="CS">
/// Dim MyLatitude As New Latitude(45, 10.5);
/// Console.WriteLine(MyLatitude.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>Indicates if the latitude is north or south of the equator.</summary>
public LatitudeHemisphere Hemisphere
{
get
{
// And set the hemisphere
return DecimalDegrees < 0 ? LatitudeHemisphere.South : LatitudeHemisphere.North;
}
}
/// <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 -90° and 90°.</summary>
public bool IsNormalized
{
get { return _DecimalDegrees >= -90 && _DecimalDegrees <= 90; }
}
#endregion
#region Public Methods
/// <returns>The <strong>Latitude</strong> containing the smallest value.</returns>
/// <summary>Returns the object with the smallest value.</summary>
/// <param name="value">A <strong>Latitude</strong> object to compare to the current instance.</param>
public Latitude LesserOf(Latitude value)
{
if (_DecimalDegrees < value.DecimalDegrees)
return this;
else
return value;
}
/// <summary>Returns the object with the largest value.</summary>
/// <returns>A <strong>Latitude</strong> containing the largest value.</returns>
/// <param name="value">A <strong>Latitude</strong> object to compare to the current instance.</param>
public Latitude GreaterOf(Latitude value)
{
if (_DecimalDegrees > value.DecimalDegrees)
return this;
else
return value;
}
/// <summary>
/// Returns a new instance whose value is rounded the specified number of decimals.
/// </summary>
/// <param name="decimals">An <strong>Integer</strong> specifying the number of decimals to round off to.</param>
/// <returns></returns>
public Latitude Round(int decimals)
{
return new Latitude(Math.Round(_DecimalDegrees, decimals));
}
/// <summary>Returns an angle opposite of the current instance.</summary>
/// <returns>An <strong>Latitude</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>Latitude</strong> of 45° then calculates its mirror
/// of 225°. (45 + 180)
/// <code lang="VB" title="[New Example]">
/// Dim Latitude1 As New Latitude(45)
/// Dim Latitude2 As Latitude = Latitude1.Mirror()
/// Debug.WriteLine(Latitude2.ToString())
/// ' Output: 225
/// </code>
/// <code lang="CS" title="[New Example]">
/// Latitude Latitude1 = new Latitude(45);
/// Latitude Latitude2 = Latitude1.Mirror();
/// Console.WriteLine(Latitude2.ToString());
/// // Output: 225
/// </code>
/// </example>
public Latitude 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 MyLatitude As New Latitude(90)
/// Dim MyRadians As Radian = MyLatitude.ToRadians()
/// </code>
/// <code lang="CS">
/// Latitude MyLatitude = new Latitude(90);
/// Radian MyRadians = MyLatitude.ToRadians();
/// </code>
/// </example>
public Radian ToRadians()
{
return new Radian(_DecimalDegrees * Radian.RadiansPerDegree);
}
/// <summary>
/// Causes the value to be adjusted to between -90 and +90.
/// </summary>
/// <returns></returns>
public Latitude Normalize()
{
// Is the value not a number, infinity, or already normalized?
if (double.IsInfinity(_DecimalDegrees)
|| double.IsNaN(_DecimalDegrees)
|| IsNormalized)
return this;
// Calculate the number of times the degree value winds completely
// through a hemisphere
int HemisphereFlips = Convert.ToInt32(Math.Floor(_DecimalDegrees / 180.0));
// If the value is in the southern hemisphere, apply another flip
if (_DecimalDegrees < 0)
HemisphereFlips++;
// Calculate the new value
double NewValue = _DecimalDegrees % 180;
// if the value is > 90, return 180 - X
if (NewValue > 90)
NewValue = 180 - NewValue;
// If the value id < -180, return -180 - X
else if (NewValue < -90.0)
NewValue = -180.0 - NewValue;
// Account for flips around hemispheres by flipping the sign
if (HemisphereFlips % 2 != 0)
return new Latitude(-NewValue);
else
return new Latitude(NewValue);
}
/// <summary>
/// Indicates if the current instance is North of the specified latitude.
/// </summary>
/// <param name="latitude">A <strong>Latitude</strong> object to examine.</param>
/// <returns>A <strong>Boolean</strong>, <strong>True</strong> if the current instance is more North than the specified instance.</returns>
public bool IsNorthOf(Latitude latitude)
{
return IsGreaterThan(latitude);
}
/// <summary>
/// Indicates if the current instance is South of the specified latitude.
/// </summary>
/// <param name="latitude">A <strong>Latitude</strong> object to examine.</param>
/// <returns>A <strong>Boolean</strong>, <strong>True</strong> if the current instance is more South than the specified instance.</returns>
public bool IsSouthOf(Latitude latitude)
{
return IsLessThan(latitude);
}
/// <summary>Converts the current instance to the northern or southern hemisphere.</summary>
public Latitude ToHemisphere(LatitudeHemisphere hemisphere)
{
if (hemisphere == LatitudeHemisphere.None)
throw new ArgumentException(Properties.Resources.Latitude_InvalidHemisphere);
// IF the degrees is already in the right hemishpere, do nothing
if (hemisphere == LatitudeHemisphere.North && _DecimalDegrees >= 0
|| hemisphere == LatitudeHemisphere.South && _DecimalDegrees < 0)
return this;
// Switch the hemisphere
return Mirror();
}
/// <summary>Outputs the angle as a string using the specified format.</summary>
/// <returns>A <strong>String</strong> in the specified format.</returns>
/// <remarks>
/// <para>This method returns the current instance output in a specific format. If no
/// value for the format is specified, a default format of "d.dddd°" is used. Any
/// string output by this method can be converted back into an Latitude object using the
/// <strong>Parse</strong> method or <strong>Latitude(string)</strong> constructor.</para>
/// </remarks>
/// <example>
/// This example uses the <strong>ToString</strong> method to output an angle in a
/// custom format. The " <strong>h°</strong> " code represents hours along with a
/// degree symbol (Alt+0176 on the keypad), and " <strong>m.mm</strong> " represents
/// the minutes out to two decimals. Mmm.
/// <code lang="VB">
/// Dim MyLatitude As New Latitude(45, 16.772)
/// Debug.WriteLine(MyLatitude.ToString("h°m.mm"))
/// ' Output: 45°16.78
/// </code>
/// <code lang="CS">
/// Dim MyLatitude As New Latitude(45, 16.772);
/// Debug.WriteLine(MyLatitude.ToString("h°m.mm"));
/// // Output: 45°16.78
/// </code>
/// </example>
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
/// <summary>Returns the smallest integer greater than the specified value.</summary>
public Latitude Ceiling()
{
return new Latitude(Math.Ceiling(_DecimalDegrees));
}
/// <summary>Returns the largest integer which is smaller than the specified value.</summary>
public Latitude Floor()
{
return new Latitude(Math.Floor(_DecimalDegrees));
}
#if !Framework20 || PocketPC
internal static int Truncate(double value)
{
return value > 0
? (int)(value - (value - Math.Floor(value)))
: (int)(value - (value - Math.Ceiling(value)));
}
#endif
/// <summary>Returns a new instance whose Seconds property is evenly divisible by 15.</summary>
/// <returns>An <strong>Latitude</strong> containing the rounded value.</returns>
/// <remarks>
/// This method is used to align or "snap" an angle to a regular interval. For
/// example, a grid might be easier to read if it were drawn at 30-second intervals instead
/// of 24.198-second intervals.
/// </remarks>
public Latitude RoundSeconds()
{
return RoundSeconds(15.0);
}
/// <summary>
/// Returns a new angle whose Seconds property is evenly divisible by the specified amount.
/// </summary>
/// <returns>An <strong>Latitude</strong> containing the rounded value.</returns>
/// <remarks>
/// This method is used to align or "snap" an angle to a regular interval. For
/// example, a grid might be easier to read if it were drawn at 30-second intervals instead
/// of 24.198-second intervals.
/// </remarks>
/// <param name="interval">
/// A <strong>Double</strong> between 0 and 60 indicating the interval to round
/// to.
/// </param>
public Latitude RoundSeconds(double interval)
{
// Interval must be > 0
if (interval == 0)
#if PocketPC
throw new ArgumentOutOfRangeException(Properties.Resources.Angle_InvalidInterval);
#else
throw new ArgumentOutOfRangeException("interval", interval, Properties.Resources.Angle_InvalidInterval);
#endif
// Get the amount in seconds
double NewSeconds = Seconds;
//double HalfInterval = interval * 0.5;
// Loop through all intervals to find the right rounding
for (double value = 0; value < 60; value += interval)
{
// Calculate the value of the next interval
double NextInterval = value + interval;