View Javadoc

1   /*
2    * Trip Tracker, a real-time position tracking system for the Internet.
3    * Copyright (C) 2006  Team Trip Tracker
4    *
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or (at your
8    * option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful, but
11   * WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License along
16   * with this program; if not, write to the Free Software Foundation, Inc.,
17   * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
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 }