]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Coordinate.java
Version 7, February 2009
[GpsPrune.git] / tim / prune / data / Coordinate.java
1 package tim.prune.data;
2
3 import java.text.DecimalFormat;
4 import java.text.NumberFormat;
5 import java.util.Locale;
6
7 /**
8  * Class to represent a lat/long coordinate
9  * and provide conversion functions
10  */
11 public abstract class Coordinate
12 {
13         public static final int NO_CARDINAL = -1;
14         public static final int NORTH = 0;
15         public static final int EAST = 1;
16         public static final int SOUTH = 2;
17         public static final int WEST = 3;
18         private static final char[] PRINTABLE_CARDINALS = {'N', 'E', 'S', 'W'};
19         public static final int FORMAT_DEG_MIN_SEC = 10;
20         public static final int FORMAT_DEG_MIN = 11;
21         public static final int FORMAT_DEG = 12;
22         public static final int FORMAT_DEG_WITHOUT_CARDINAL = 13;
23         public static final int FORMAT_DEG_WHOLE_MIN = 14;
24         public static final int FORMAT_DEG_MIN_SEC_WITH_SPACES = 15;
25         public static final int FORMAT_CARDINAL = 16;
26         public static final int FORMAT_DECIMAL_FORCE_POINT = 17;
27         public static final int FORMAT_NONE = 19;
28
29         /** Number formatter for fixed decimals with forced decimal point */
30         private static final NumberFormat EIGHT_DP = NumberFormat.getNumberInstance(Locale.UK);
31         // Select the UK locale for this formatter so that decimal point is always used (not comma)
32         static {
33                 if (EIGHT_DP instanceof DecimalFormat) ((DecimalFormat) EIGHT_DP).applyPattern("0.00000000");
34         }
35
36         // Instance variables
37         private boolean _valid = false;
38         protected int _cardinal = NORTH;
39         private int _degrees = 0;
40         private int _minutes = 0;
41         private int _seconds = 0;
42         private int _fracs = 0;
43         private int _fracDenom = 0;
44         private String _originalString = null;
45         private int _originalFormat = FORMAT_NONE;
46         private double _asDouble = 0.0;
47
48
49         /**
50          * Constructor given String
51          * @param inString string to parse
52          */
53         public Coordinate(String inString)
54         {
55                 _originalString = inString;
56                 int strLen = 0;
57                 if (inString != null)
58                 {
59                         inString = inString.trim();
60                         strLen = inString.length();
61                 }
62                 if (strLen > 1)
63                 {
64                         // Check for cardinal character either at beginning or end
65                         boolean hasCardinal = true;
66                         _cardinal = getCardinal(inString.charAt(0), inString.charAt(strLen-1));
67                         if (_cardinal == NO_CARDINAL) {
68                                 hasCardinal = false;
69                                 // use default from concrete subclass
70                                 _cardinal = getDefaultCardinal();
71                         }
72
73                         // count numeric fields - 1=d, 2=dm, 3=dm.m/dms, 4=dms.s
74                         int numFields = 0;
75                         boolean inNumeric = false;
76                         char currChar;
77                         long[] fields = new long[4]; // needs to be long for lengthy decimals
78                         long[] denoms = new long[4];
79                         String secondDelim = "";
80                         try
81                         {
82                                 // Loop over characters in input string, populating fields array
83                                 for (int i=0; i<strLen; i++)
84                                 {
85                                         currChar = inString.charAt(i);
86                                         if (currChar >= '0' && currChar <= '9')
87                                         {
88                                                 if (!inNumeric)
89                                                 {
90                                                         inNumeric = true;
91                                                         numFields++;
92                                                         denoms[numFields-1] = 1;
93                                                 }
94                                                 fields[numFields-1] = fields[numFields-1] * 10 + (currChar - '0');
95                                                 denoms[numFields-1] *= 10;
96                                         }
97                                         else
98                                         {
99                                                 inNumeric = false;
100                                                 // Remember second delimiter
101                                                 if (numFields == 2) {
102                                                         secondDelim += currChar;
103                                                 }
104                                         }
105                                 }
106                                 _valid = (numFields > 0);
107                         }
108                         catch (ArrayIndexOutOfBoundsException obe)
109                         {
110                                 // more than four fields found - unable to parse
111                                 _valid = false;
112                         }
113                         // parse fields according to number found
114                         _degrees = (int) fields[0];
115                         _originalFormat = hasCardinal?FORMAT_DEG:FORMAT_DEG_WITHOUT_CARDINAL;
116                         _fracDenom = 10;
117                         if (numFields == 2)
118                         {
119                                 // String is just decimal degrees
120                                 double numMins = fields[1] * 60.0 / denoms[1];
121                                 _minutes = (int) numMins;
122                                 double numSecs = (numMins - _minutes) * 60.0;
123                                 _seconds = (int) numSecs;
124                                 _fracs = (int) ((numSecs - _seconds) * 10);
125                                 _asDouble = _degrees + 1.0 * fields[1] / denoms[1];
126                         }
127                         // Differentiate between d-m.f and d-m-s using . or ,
128                         else if (numFields == 3 && (secondDelim.equals(".") || secondDelim.equals(",")))
129                         {
130                                 // String is degrees-minutes.fractions
131                                 _originalFormat = FORMAT_DEG_MIN;
132                                 _minutes = (int) fields[1];
133                                 double numSecs = fields[2] * 60.0 / denoms[2];
134                                 _seconds = (int) numSecs;
135                                 _fracs = (int) ((numSecs - _seconds) * 10);
136                                 _asDouble = 1.0 * _degrees + (_minutes / 60.0) + (numSecs / 3600.0);
137                         }
138                         else if (numFields == 4 || numFields == 3)
139                         {
140                                 // String is degrees-minutes-seconds.fractions
141                                 _originalFormat = FORMAT_DEG_MIN_SEC;
142                                 _minutes = (int) fields[1];
143                                 _seconds = (int) fields[2];
144                                 _fracs = (int) fields[3];
145                                 _fracDenom = (int) denoms[3];
146                                 if (_fracDenom < 1) {_fracDenom = 1;}
147                                 _asDouble = 1.0 * _degrees + (_minutes / 60.0) + (_seconds / 3600.0) + (_fracs / 3600.0 / _fracDenom);
148                         }
149                         if (_cardinal == WEST || _cardinal == SOUTH || inString.charAt(0) == '-')
150                                 _asDouble = -_asDouble;
151                         // validate fields
152                         _valid = _valid && (_degrees <= getMaxDegrees() && _minutes < 60 && _seconds < 60 && _fracs < _fracDenom);
153                 }
154                 else _valid = false;
155         }
156
157
158         /**
159          * Get the cardinal from the given character
160          * @param inFirstChar first character from file
161          * @param inLastChar last character from file
162          */
163         protected int getCardinal(char inFirstChar, char inLastChar)
164         {
165                 // Try leading character first
166                 int cardinal = getCardinal(inFirstChar);
167                 // if not there, try trailing character
168                 if (cardinal == NO_CARDINAL) {
169                         cardinal = getCardinal(inLastChar);
170                 }
171                 return cardinal;
172         }
173
174
175         /**
176          * Get the cardinal from the given character
177          * @param inChar character from file
178          */
179         protected abstract int getCardinal(char inChar);
180
181         /**
182          * @return the default cardinal for the subclass
183          */
184         protected abstract int getDefaultCardinal();
185
186         /**
187          * @return the maximum degree range for this coordinate
188          */
189         protected abstract int getMaxDegrees();
190
191
192         /**
193          * Constructor
194          * @param inValue value of coordinate
195          * @param inFormat format to use
196          * @param inCardinal cardinal
197          */
198         protected Coordinate(double inValue, int inFormat, int inCardinal)
199         {
200                 _asDouble = inValue;
201                 // Calculate degrees, minutes, seconds
202                 _degrees = (int) Math.abs(inValue);
203                 double numMins = (Math.abs(_asDouble)-_degrees) * 60.0;
204                 _minutes = (int) numMins;
205                 double numSecs = (numMins - _minutes) * 60.0;
206                 _seconds = (int) numSecs;
207                 _fracs = (int) ((numSecs - _seconds) * 10);
208                 _fracDenom = 10; // fixed for now
209                 // Make a string to display on screen
210                 _cardinal = inCardinal;
211                 _originalFormat = FORMAT_NONE;
212                 if (inFormat == FORMAT_NONE) inFormat = FORMAT_DEG_WITHOUT_CARDINAL;
213                 _originalString = output(inFormat);
214                 _originalFormat = inFormat;
215                 _valid = true;
216         }
217
218
219         /**
220          * @return coordinate as a double
221          */
222         public double getDouble()
223         {
224                 return _asDouble;
225         }
226
227         /**
228          * @return true if Coordinate is valid
229          */
230         public boolean isValid()
231         {
232                 return _valid;
233         }
234
235         /**
236          * Compares two Coordinates for equality
237          * @param inOther other Coordinate object with which to compare
238          * @return true if the two objects are equal
239          */
240         public boolean equals(Coordinate inOther)
241         {
242                 return (inOther != null && _cardinal == inOther._cardinal
243                         && _degrees == inOther._degrees
244                         && _minutes == inOther._minutes
245                         && _seconds == inOther._seconds
246                         && _fracs == inOther._fracs);
247         }
248
249
250         /**
251          * Output the Coordinate in the given format
252          * @param inFormat format to use, eg FORMAT_DEG_MIN_SEC
253          * @return String for output
254          */
255         public String output(int inFormat)
256         {
257                 String answer = _originalString;
258                 if (inFormat != FORMAT_NONE && inFormat != _originalFormat)
259                 {
260                         // TODO: allow specification of precision for output of d-m and d
261                         // format as specified
262                         switch (inFormat)
263                         {
264                                 case FORMAT_DEG_MIN_SEC:
265                                 {
266                                         StringBuffer buffer = new StringBuffer();
267                                         buffer.append(PRINTABLE_CARDINALS[_cardinal])
268                                                 .append(threeDigitString(_degrees)).append('°')
269                                                 .append(twoDigitString(_minutes)).append('\'')
270                                                 .append(twoDigitString(_seconds)).append('.')
271                                                 .append(formatFraction(_fracs, _fracDenom));
272                                         answer = buffer.toString();
273                                         break;
274                                 }
275                                 case FORMAT_DEG_MIN:
276                                 {
277                                         answer = "" + PRINTABLE_CARDINALS[_cardinal] + threeDigitString(_degrees) + "°"
278                                                 + (_minutes + _seconds / 60.0 + _fracs / 60.0 / _fracDenom) + "'";
279                                         break;
280                                 }
281                                 case FORMAT_DEG_WHOLE_MIN:
282                                 {
283                                         answer = "" + PRINTABLE_CARDINALS[_cardinal] + threeDigitString(_degrees) + "°"
284                                                 + (int) Math.floor(_minutes + _seconds / 60.0 + _fracs / 60.0 / _fracDenom + 0.5) + "'";
285                                         break;
286                                 }
287                                 case FORMAT_DEG:
288                                 case FORMAT_DEG_WITHOUT_CARDINAL:
289                                 {
290                                         answer = (_asDouble<0.0?"-":"")
291                                                 + (_degrees + _minutes / 60.0 + _seconds / 3600.0 + _fracs / 3600.0 / _fracDenom);
292                                         break;
293                                 }
294                                 case FORMAT_DECIMAL_FORCE_POINT:
295                                 {
296                                         // Forcing a decimal point instead of system-dependent commas etc
297                                         answer = EIGHT_DP.format(_asDouble);
298                                         break;
299                                 }
300                                 case FORMAT_DEG_MIN_SEC_WITH_SPACES:
301                                 {
302                                         // Note: cardinal not needed as this format is only for exif, which has cardinal separately
303                                         answer = "" + _degrees + " " + _minutes + " " + _seconds + "." + formatFraction(_fracs, _fracDenom);
304                                         break;
305                                 }
306                                 case FORMAT_CARDINAL:
307                                 {
308                                         answer = "" + PRINTABLE_CARDINALS[_cardinal];
309                                         break;
310                                 }
311                         }
312                 }
313                 return answer;
314         }
315
316         /**
317          * Format the fraction part of seconds value
318          * @param inFrac fractional part eg 123
319          * @param inDenom denominator of fraction eg 10000
320          * @return String describing fraction, in this case 0123
321          */
322         private static final String formatFraction(int inFrac, int inDenom)
323         {
324                 if (inDenom <= 1 || inFrac == 0) {return "" + inFrac;}
325                 String denomString = "" + inDenom;
326                 int reqdLen = denomString.length() - 1;
327                 String result = denomString + inFrac;
328                 int resultLen = result.length();
329                 return result.substring(resultLen - reqdLen);
330         }
331
332
333         /**
334          * Format an integer to a two-digit String
335          * @param inNumber number to format
336          * @return two-character String
337          */
338         private static String twoDigitString(int inNumber)
339         {
340                 if (inNumber <= 0) return "00";
341                 if (inNumber < 10) return "0" + inNumber;
342                 if (inNumber < 100) return "" + inNumber;
343                 return "" + (inNumber % 100);
344         }
345
346
347         /**
348          * Format an integer to a three-digit String for degrees
349          * @param inNumber number to format
350          * @return three-character String
351          */
352         private static String threeDigitString(int inNumber)
353         {
354                 if (inNumber <= 0) return "000";
355                 if (inNumber < 10) return "00" + inNumber;
356                 if (inNumber < 100) return "0" + inNumber;
357                 return "" + (inNumber % 1000);
358         }
359
360
361         /**
362          * Create a new Coordinate between two others
363          * @param inStart start coordinate
364          * @param inEnd end coordinate
365          * @param inIndex index of point
366          * @param inNumPoints number of points to interpolate
367          * @return new Coordinate object
368          */
369         public static Coordinate interpolate(Coordinate inStart, Coordinate inEnd,
370                 int inIndex, int inNumPoints)
371         {
372                 return interpolate(inStart, inEnd, 1.0 * (inIndex+1) / (inNumPoints + 1));
373         }
374
375
376         /**
377          * Create a new Coordinate between two others
378          * @param inStart start coordinate
379          * @param inEnd end coordinate
380          * @param inFraction fraction from start to end
381          * @return new Coordinate object
382          */
383         public static Coordinate interpolate(Coordinate inStart, Coordinate inEnd,
384                 double inFraction)
385         {
386                 double startValue = inStart.getDouble();
387                 double endValue = inEnd.getDouble();
388                 double newValue = startValue + (endValue - startValue) * inFraction;
389                 Coordinate answer = inStart.makeNew(newValue, inStart._originalFormat);
390                 return answer;
391         }
392
393
394         /**
395          * Make a new Coordinate according to subclass
396          * @param inValue double value
397          * @param inFormat format to use
398          * @return object of Coordinate subclass
399          */
400         protected abstract Coordinate makeNew(double inValue, int inFormat);
401
402
403         /**
404          * Create a String representation for debug
405          * @return String describing coordinate value
406          */
407         public String toString()
408         {
409                 return "Coord: " + _cardinal + " (" + _degrees + ") (" + _minutes + ") (" + _seconds + "."
410                         + formatFraction(_fracs, _fracDenom) + ") = " + _asDouble;
411         }
412 }