]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Coordinate.java
Version 1, September 2006
[GpsPrune.git] / tim / prune / data / Coordinate.java
1 package tim.prune.data;
2
3 /**
4  * Class to represent a lat/long coordinate
5  * and provide conversion functions
6  */
7 public abstract class Coordinate
8 {
9         public static final int NORTH = 0;
10         public static final int EAST = 1;
11         public static final int SOUTH = 2;
12         public static final int WEST = 3;
13         public static final char[] PRINTABLE_CARDINALS = {'N', 'E', 'S', 'W'};
14         public static final int FORMAT_DEG_MIN_SEC = 10;
15         public static final int FORMAT_DEG_MIN = 11;
16         public static final int FORMAT_DEG = 12;
17         public static final int FORMAT_DEG_WITHOUT_CARDINAL = 13;
18         public static final int FORMAT_NONE = 19;
19
20         // Instance variables
21         private boolean _valid = false;
22         protected int _cardinal = NORTH;
23         private int _degrees = 0;
24         private int _minutes = 0;
25         private int _seconds = 0;
26         private int _fracs = 0;
27         private String _originalString = null;
28         private int _originalFormat = FORMAT_NONE;
29         private double _asDouble = 0.0;
30
31
32         /**
33          * Constructor given String
34          * @param inString string to parse
35          */
36         public Coordinate(String inString)
37         {
38                 _originalString = inString;
39                 int strLen = 0;
40                 if (inString != null)
41                 {
42                         inString = inString.trim();
43                         strLen = inString.length();
44                 }
45                 if (strLen > 1)
46                 {
47                         // Check for leading character NSEW
48                         _cardinal = getCardinal(inString.charAt(0));
49                         // count numeric fields - 1=d, 2=dm, 3=dm.m/dms, 4=dms.s
50                         int numFields = 0;
51                         boolean inNumeric = false;
52                         char currChar;
53                         long[] fields = new long[4];
54                         long[] denoms = new long[4];
55                         try
56                         {
57                                 for (int i=0; i<strLen; i++)
58                                 {
59                                         currChar = inString.charAt(i);
60                                         if (currChar >= '0' && currChar <= '9')
61                                         {
62                                                 if (!inNumeric)
63                                                 {
64                                                         inNumeric = true;
65                                                         numFields++;
66                                                         denoms[numFields-1] = 1;
67                                                 }
68                                                 fields[numFields-1] = fields[numFields-1] * 10 + (currChar - '0');
69                                                 denoms[numFields-1] *= 10;
70                                         }
71                                         else
72                                         {
73                                                 inNumeric = false;
74                                         }
75                                 }
76                                 _valid = (numFields > 0);
77                         }
78                         catch (ArrayIndexOutOfBoundsException obe)
79                         {
80                                 // more than four fields found - unable to parse
81                                 _valid = false;
82                         }
83                         // parse fields according to number found
84                         _degrees = (int) fields[0];
85                         _originalFormat = FORMAT_DEG;
86                         if (numFields == 2)
87                         {
88                                 // String is just decimal degrees
89                                 double numMins = fields[1] * 60.0 / denoms[1];
90                                 _minutes = (int) numMins;
91                                 double numSecs = (numMins - _minutes) * 60.0;
92                                 _seconds = (int) numSecs;
93                                 _fracs = (int) ((numSecs - _seconds) * 10);
94                         }
95                         else if (numFields == 3)
96                         {
97                                 // String is degrees-minutes.fractions
98                                 _originalFormat = FORMAT_DEG_MIN;
99                                 _minutes = (int) fields[1];
100                                 double numSecs = fields[2] * 60.0 / denoms[2];
101                                 _seconds = (int) numSecs;
102                                 _fracs = (int) ((numSecs - _seconds) * 10);
103                         }
104                         else if (numFields == 4)
105                         {
106                                 _originalFormat = FORMAT_DEG_MIN_SEC;
107                                 // String is degrees-minutes-seconds.fractions
108                                 _minutes = (int) fields[1];
109                                 _seconds = (int) fields[2];
110                                 _fracs = (int) fields[3];
111                         }
112                         _asDouble = 1.0 * _degrees + (_minutes / 60.0) + (_seconds / 3600.0) + (_fracs / 36000.0);
113                         if (_cardinal == WEST || _cardinal == SOUTH || inString.charAt(0) == '-')
114                                 _asDouble = -_asDouble;
115                 }
116                 else _valid = false;
117         }
118
119
120         /**
121          * Get the cardinal from the given character
122          * @param inChar character from file
123          */
124         protected abstract int getCardinal(char inChar);
125
126
127         /**
128          * Constructor
129          * @param inValue value of coordinate
130          * @param inFormat format to use
131          */
132         protected Coordinate(double inValue, int inFormat)
133         {
134                 _asDouble = inValue;
135                 // Calculate degrees, minutes, seconds
136                 _degrees = (int) inValue;
137                 double numMins = (Math.abs(_asDouble)-Math.abs(_degrees)) * 60.0;
138                 _minutes = (int) numMins;
139                 double numSecs = (numMins - _minutes) * 60.0;
140                 _seconds = (int) numSecs;
141                 _fracs = (int) ((numSecs - _seconds) * 10);
142                 // Make a string to display on screen
143                 _originalFormat = FORMAT_NONE;
144                 if (inFormat == FORMAT_NONE) inFormat = FORMAT_DEG_WITHOUT_CARDINAL;
145                 _originalString = output(inFormat);
146                 _originalFormat = inFormat;
147         }
148
149
150         /**
151          * @return coordinate as a double
152          */
153         public double getDouble()
154         {
155                 return _asDouble;
156         }
157
158         /**
159          * @return true if Coordinate is valid
160          */
161         public boolean isValid()
162         {
163                 return _valid;
164         }
165
166         /**
167          * Compares two Coordinates for equality
168          * @param inOther other Coordinate object with which to compare
169          * @return true if the two objects are equal
170          */
171         public boolean equals(Coordinate inOther)
172         {
173                 return (inOther != null && _cardinal == inOther._cardinal
174                         && _degrees == inOther._degrees
175                         && _minutes == inOther._minutes
176                         && _seconds == inOther._seconds
177                         && _fracs == inOther._fracs);
178         }
179
180
181         /**
182          * Output the Coordinate in the given format
183          * @param inOriginalString the original String to use as default
184          * @param inFormat format to use, eg FORMAT_DEG_MIN_SEC
185          * @return String for output
186          */
187         public String output(int inFormat)
188         {
189                 String answer = _originalString;
190                 if (inFormat != FORMAT_NONE && inFormat != _originalFormat)
191                 {
192                         // TODO: allow specification of precision for output of d-m and d
193                         // format as specified
194                         switch (inFormat)
195                         {
196                                 case FORMAT_DEG_MIN_SEC: {
197                                         StringBuffer buffer = new StringBuffer();
198                                         buffer.append(PRINTABLE_CARDINALS[_cardinal])
199                                                 .append(threeDigitString(_degrees)).append('°')
200                                                 .append(twoDigitString(_minutes)).append('\'')
201                                                 .append(twoDigitString(_seconds)).append('.')
202                                                 .append(_fracs);
203                                         answer = buffer.toString(); break;
204                                 }
205                                 case FORMAT_DEG_MIN: answer = "" + PRINTABLE_CARDINALS[_cardinal] + threeDigitString(_degrees) + "°"
206                                         + (_minutes + _seconds / 60.0 + _fracs / 600.0); break;
207                                 case FORMAT_DEG:
208                                 case FORMAT_DEG_WITHOUT_CARDINAL: answer = (_asDouble<0.0?"-":"")
209                                 + (_degrees + _minutes / 60.0 + _seconds / 3600.0 + _fracs / 36000.0); break;
210                         }
211                 }
212                 return answer;
213         }
214
215
216         /**
217          * Format an integer to a two-digit String
218          * @param inNumber number to format
219          * @return two-character String
220          */
221         private static String twoDigitString(int inNumber)
222         {
223                 if (inNumber <= 0) return "00";
224                 if (inNumber < 10) return "0" + inNumber;
225                 if (inNumber < 100) return "" + inNumber;
226                 return "" + (inNumber % 100);
227         }
228
229
230         /**
231          * Format an integer to a three-digit String for degrees
232          * @param inNumber number to format
233          * @return three-character String
234          */
235         private static String threeDigitString(int inNumber)
236         {
237                 if (inNumber <= 0) return "000";
238                 if (inNumber < 10) return "00" + inNumber;
239                 if (inNumber < 100) return "0" + inNumber;
240                 return "" + (inNumber % 1000);
241         }
242
243
244         /**
245          * Create a new Coordinate between two others
246          * @param inStart start coordinate
247          * @param inEnd end coordinate
248          * @param inIndex index of point
249          * @param inNumPoints number of points to interpolate
250          * @return new Coordinate object
251          */
252         public static Coordinate interpolate(Coordinate inStart, Coordinate inEnd,
253                 int inIndex, int inNumPoints)
254         {
255                 double startValue = inStart.getDouble();
256                 double endValue = inEnd.getDouble();
257                 double newValue = startValue + (endValue - startValue) * (inIndex+1) / (inNumPoints + 1);
258                 Coordinate answer = inStart.makeNew(newValue, inStart._originalFormat);
259                 return answer;
260         }
261
262
263         /**
264          * Make a new Coordinate according to subclass
265          * @param inValue double value
266          * @param inFormat format to use
267          * @return object of Coordinate subclass
268          */
269         protected abstract Coordinate makeNew(double inValue, int inFormat);
270
271
272         /**
273          * Create a String representation for debug
274          */
275         public String toString()
276         {
277                 return "Coord: " + _cardinal + " (" + _degrees + ") (" + _minutes + ") (" + _seconds + "." + _fracs + ") = " + _asDouble;
278         }
279 }