1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package triptracker.core;
21
22 import java.text.DecimalFormat;
23
24 /***
25 * Util class to represent a coordinate in sexagesimal format.
26 */
27 public class Sexagesimal {
28 private final int deg;
29 private final int min;
30 private final double sec;
31
32 public Sexagesimal(int degrees, int minutes, double seconds) {
33 deg = degrees;
34 min = minutes;
35 sec = seconds;
36 }
37
38 public Sexagesimal(double degrees) {
39 deg = (int)degrees;
40 degrees = (degrees - deg) * 60;
41
42 min = (int)degrees;
43 degrees = degrees - min;
44
45 sec = degrees * 60;
46 }
47
48 public static Sexagesimal decToSex(double degrees) {
49 int deg = (int)degrees;
50 degrees = (degrees - deg) * 60;
51
52 int min = (int)degrees;
53 degrees = degrees - min;
54
55 return new Sexagesimal(deg, min, degrees * 60);
56 }
57
58 public int getDeg() {
59 return deg;
60 }
61
62 public int getMin() {
63 return min;
64 }
65
66 public double getSec() {
67 return sec;
68 }
69
70 /***
71 * Returns a sexagesimal formatted string representation of a coordinate.
72 *
73 * @see java.lang.Object#toString()
74 */
75 @Override
76 public String toString() {
77 return toString(2);
78 }
79
80 /***
81 * Returns a sexagesimal formatted string representation of a coordinate
82 * with the given number of decimals for seconds.
83 * @param decimals
84 * @return sexagesimal representation of a coordinate as String
85 */
86 public String toString(int decimals) {
87 return (deg + "?" + min + "'" + Utils.round(sec, decimals) + "\"");
88 }
89
90 /***
91 * Returns a sexagesimal formatted string representation of a coordinate.
92 */
93 public static String toString(double degrees) {
94 int deg = (int)degrees;
95 degrees = (degrees - deg) * 60;
96
97 int min = (int)degrees;
98 degrees = degrees - min;
99
100 double sec = Utils.round(degrees * 60, 2);
101
102 String fDeg = new DecimalFormat("000").format(deg);
103 String fSec = new DecimalFormat("00").format(sec);
104
105 return (fDeg + "?" + min + "'" + fSec + "\"");
106 }
107 }