File: Coordinate.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (478 lines) | stat: -rw-r--r-- 14,146 bytes parent folder | download
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
package tim.prune.data;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * Class to represent a lat/long coordinate
 * and provide conversion functions
 */
public abstract class Coordinate
{
	public static final int NO_CARDINAL = -1;
	public static final int NORTH = 0;
	public static final int EAST = 1;
	public static final int SOUTH = 2;
	public static final int WEST = 3;
	private static final char[] PRINTABLE_CARDINALS = {'N', 'E', 'S', 'W'};
	public static final int FORMAT_DEG_MIN_SEC = 10;
	public static final int FORMAT_DEG_MIN = 11;
	public static final int FORMAT_DEG = 12;
	public static final int FORMAT_DEG_WITHOUT_CARDINAL = 13;
	public static final int FORMAT_DEG_WHOLE_MIN = 14;
	public static final int FORMAT_DEG_MIN_SEC_WITH_SPACES = 15;
	public static final int FORMAT_CARDINAL = 16;
	public static final int FORMAT_DECIMAL_FORCE_POINT = 17;
	public static final int FORMAT_NONE = 19;

	/** Number formatter for fixed decimals with forced decimal point */
	private static final NumberFormat EIGHT_DP = NumberFormat.getNumberInstance(Locale.UK);
	// Select the UK locale for this formatter so that decimal point is always used (not comma)
	static {
		if (EIGHT_DP instanceof DecimalFormat) ((DecimalFormat) EIGHT_DP).applyPattern("0.00000000");
	}
	/** Number formatter for fixed decimals with forced decimal point */
	private static final NumberFormat FIVE_DP = NumberFormat.getNumberInstance(Locale.UK);
	static {
		if (FIVE_DP instanceof DecimalFormat) ((DecimalFormat) FIVE_DP).applyPattern("0.00000");
	}

	// Instance variables
	private boolean _valid = false;
	private boolean _cardinalGuessed = false;
	protected int _cardinal = NORTH;
	private int _degrees = 0;
	private int _minutes = 0;
	private int _seconds = 0;
	private int _fracs = 0;
	private int _fracDenom = 0;
	private String _originalString = null;
	private int _originalFormat = FORMAT_NONE;
	private double _asDouble = 0.0;


	/**
	 * Constructor given String
	 * @param inString string to parse
	 */
	public Coordinate(String inString)
	{
		_originalString = inString;
		int strLen = 0;
		if (inString != null)
		{
			inString = inString.trim();
			strLen = inString.length();
		}
		if (strLen > 0)
		{
			// Check for cardinal character either at beginning or end
			boolean hasCardinal = true;
			_cardinal = getCardinal(inString.charAt(0), inString.charAt(strLen-1));
			if (_cardinal == NO_CARDINAL) {
				hasCardinal = false;
				// use default from concrete subclass
				_cardinal = getDefaultCardinal();
				_cardinalGuessed = true;
			}
			else if (isJustNumber(inString)) {
				// it's just a number
				hasCardinal = false;
				_cardinalGuessed = true;
			}

			// count numeric fields - 1=d, 2=dm, 3=dm.m/dms, 4=dms.s
			int numFields = 0;
			boolean isNumeric = false;
			char currChar;
			long[] fields = new long[4]; // needs to be long for lengthy decimals
			long[] denoms = new long[4];
			boolean[] otherDelims = new boolean[5]; // remember whether delimiters have non-decimal chars
			try
			{
				// Loop over characters in input string, populating fields array
				for (int i=0; i<strLen; i++)
				{
					currChar = inString.charAt(i);
					if (currChar >= '0' && currChar <= '9')
					{
						if (!isNumeric)
						{
							isNumeric = true;
							numFields++;
							denoms[numFields-1] = 1;
						}
						if (denoms[numFields-1] < 1E18) // ignore trailing characters if too big for long
						{
							fields[numFields-1] = fields[numFields-1] * 10 + (currChar - '0');
							denoms[numFields-1] *= 10;
						}
					}
					else
					{
						isNumeric = false;
						// Remember delimiters
						if (currChar != ',' && currChar != '.') {otherDelims[numFields] = true;}
					}
				}
				_valid = (numFields > 0);
			}
			catch (ArrayIndexOutOfBoundsException obe)
			{
				// more than four fields found - unable to parse
				_valid = false;
			}
			// parse fields according to number found
			_degrees = (int) fields[0];
			_asDouble = _degrees;
			_originalFormat = hasCardinal ? FORMAT_DEG : FORMAT_DEG_WITHOUT_CARDINAL;
			_fracDenom = 10;
			if (numFields == 2)
			{
				if (!otherDelims[1])
				{
					// String is just decimal degrees
					double numMins = fields[1] * 60.0 / denoms[1];
					_minutes = (int) numMins;
					double numSecs = (numMins - _minutes) * 60.0;
					_seconds = (int) numSecs;
					_fracs = (int) ((numSecs - _seconds) * 10);
					_asDouble = _degrees + 1.0 * fields[1] / denoms[1];
				}
				else
				{
					// String is degrees and minutes (due to non-decimal separator)
					_originalFormat = FORMAT_DEG_MIN;
					_minutes = (int) fields[1];
					_seconds = 0;
					_fracs = 0;
					_asDouble = 1.0 * _degrees + (_minutes / 60.0);
				}
			}
			// Check for exponential degrees like 1.3E-6
			else if (numFields == 3 && !otherDelims[1] && otherDelims[2] && isJustNumber(inString))
			{
				_originalFormat = FORMAT_DEG;
				_asDouble = Math.abs(Double.parseDouble(inString)); // must succeed if isJustNumber has given true
				// now we can ignore the fields and just use this double
				_degrees = (int) _asDouble;
				double numMins = (_asDouble - _degrees) * 60.0;
				_minutes = (int) numMins;
				double numSecs = (numMins - _minutes) * 60.0;
				_seconds = (int) numSecs;
				_fracs = (int) ((numSecs - _seconds) * 10);
			}
			// Differentiate between d-m.f and d-m-s using . or ,
			else if (numFields == 3 && !otherDelims[2])
			{
				// String is degrees-minutes.fractions
				_originalFormat = FORMAT_DEG_MIN;
				_minutes = (int) fields[1];
				double numSecs = fields[2] * 60.0 / denoms[2];
				_seconds = (int) numSecs;
				_fracs = (int) ((numSecs - _seconds) * 10);
				_asDouble = 1.0 * _degrees + (_minutes / 60.0) + (numSecs / 3600.0);
			}
			else if (numFields == 4 || numFields == 3)
			{
				// String is degrees-minutes-seconds.fractions
				_originalFormat = FORMAT_DEG_MIN_SEC;
				_minutes = (int) fields[1];
				_seconds = (int) fields[2];
				_fracs = (int) fields[3];
				_fracDenom = (int) denoms[3];
				if (_fracDenom < 1) {_fracDenom = 1;}
				_asDouble = 1.0 * _degrees + (_minutes / 60.0) + (_seconds / 3600.0) + (_fracs / 3600.0 / _fracDenom);
			}
			if (_cardinal == WEST || _cardinal == SOUTH || inString.charAt(0) == '-')
				_asDouble = -_asDouble;
			// validate fields
			_valid = _valid && (_degrees <= getMaxDegrees() && _minutes < 60 && _seconds < 60 && _fracs < _fracDenom)
				&& Math.abs(_asDouble) <= getMaxDegrees();
		}
		else _valid = false;
	}


	/**
	 * Get the cardinal from the given character
	 * @param inFirstChar first character from string
	 * @param inLastChar last character from string
	 */
	private int getCardinal(char inFirstChar, char inLastChar)
	{
		// Try leading character first
		int cardinal = getCardinal(inFirstChar);
		// if not there, try trailing character
		if (cardinal == NO_CARDINAL) {
			cardinal = getCardinal(inLastChar);
		}
		return cardinal;
	}

	/**
	 * @return true if cardinal was guessed, false if parsed
	 */
	public boolean getCardinalGuessed() {
		return _cardinalGuessed;
	}

	/**
	 * Get the cardinal from the given character
	 * @param inChar character from file
	 */
	protected abstract int getCardinal(char inChar);

	/**
	 * @return the default cardinal for the subclass
	 */
	protected abstract int getDefaultCardinal();

	/**
	 * @return the maximum degree range for this coordinate
	 */
	protected abstract int getMaxDegrees();


	/**
	 * Constructor
	 * @param inValue value of coordinate
	 * @param inFormat format to use
	 * @param inCardinal cardinal
	 */
	protected Coordinate(double inValue, int inFormat, int inCardinal)
	{
		_asDouble = inValue;
		// Calculate degrees, minutes, seconds
		_degrees = (int) Math.abs(inValue);
		double numMins = (Math.abs(_asDouble)-_degrees) * 60.0;
		_minutes = (int) numMins;
		double numSecs = (numMins - _minutes) * 60.0;
		_seconds = (int) numSecs;
		_fracs = (int) ((numSecs - _seconds) * 10);
		_fracDenom = 10; // fixed for now
		// Make a string to display on screen
		_cardinal = inCardinal;
		_originalFormat = FORMAT_NONE;
		if (inFormat == FORMAT_NONE) inFormat = FORMAT_DEG_WITHOUT_CARDINAL;
		_originalString = output(inFormat);
		_originalFormat = inFormat;
		_valid = true;
	}


	/**
	 * @return coordinate as a double
	 */
	public double getDouble()
	{
		return _asDouble;
	}

	/**
	 * @return true if Coordinate is valid
	 */
	public boolean isValid()
	{
		return _valid;
	}

	/**
	 * Compares two Coordinates for equality
	 * @param inOther other Coordinate object with which to compare
	 * @return true if the two objects are equal
	 */
	public boolean equals(Coordinate inOther)
	{
		return (_asDouble == inOther._asDouble);
	}


	/**
	 * Output the Coordinate in the given format
	 * @param inFormat format to use, eg FORMAT_DEG_MIN_SEC
	 * @return String for output
	 */
	public String output(int inFormat)
	{
		String answer = _originalString;
		if (inFormat != FORMAT_NONE && inFormat != _originalFormat)
		{
			// TODO: allow specification of precision for output of d-m and d
			// format as specified
			switch (inFormat)
			{
				case FORMAT_DEG_MIN_SEC:
				{
					StringBuffer buffer = new StringBuffer();
					buffer.append(PRINTABLE_CARDINALS[_cardinal])
						.append(threeDigitString(_degrees)).append('\u00B0')
						.append(twoDigitString(_minutes)).append('\'')
						.append(twoDigitString(_seconds)).append('.')
						.append(formatFraction(_fracs, _fracDenom));
					answer = buffer.toString();
					break;
				}
				case FORMAT_DEG_MIN:
				{
					answer = "" + PRINTABLE_CARDINALS[_cardinal] + threeDigitString(_degrees) + "\u00B0"
						+ FIVE_DP.format((Math.abs(_asDouble) - _degrees) * 60.0) + "'";
					break;
				}
				case FORMAT_DEG_WHOLE_MIN:
				{
					int deg = _degrees;
					int min = (int) Math.floor(_minutes + _seconds / 60.0 + _fracs / 60.0 / _fracDenom + 0.5);
					if (min == 60) {
						min = 0; deg++;
					}
					answer = "" + PRINTABLE_CARDINALS[_cardinal] + threeDigitString(deg) + "\u00B0" + min + "'";
					break;
				}
				case FORMAT_DEG:
				case FORMAT_DEG_WITHOUT_CARDINAL:
				{
					if (_originalFormat != FORMAT_DEG_WITHOUT_CARDINAL)
					{
						answer = (_asDouble<0.0?"-":"")
							+ (_degrees + _minutes / 60.0 + _seconds / 3600.0 + _fracs / 3600.0 / _fracDenom);
					}
					break;
				}
				case FORMAT_DECIMAL_FORCE_POINT:
				{
					// Forcing a decimal point instead of system-dependent commas etc
					if (_originalFormat != FORMAT_DEG_WITHOUT_CARDINAL || answer.indexOf('.') < 0) {
						answer = EIGHT_DP.format(_asDouble);
					}
					break;
				}
				case FORMAT_DEG_MIN_SEC_WITH_SPACES:
				{
					// Note: cardinal not needed as this format is only for exif, which has cardinal separately
					answer = "" + _degrees + " " + _minutes + " " + _seconds + "." + formatFraction(_fracs, _fracDenom);
					break;
				}
				case FORMAT_CARDINAL:
				{
					answer = "" + PRINTABLE_CARDINALS[_cardinal];
					break;
				}
			}
		}
		return answer;
	}

	/**
	 * Format the fraction part of seconds value
	 * @param inFrac fractional part eg 123
	 * @param inDenom denominator of fraction eg 10000
	 * @return String describing fraction, in this case 0123
	 */
	private static final String formatFraction(int inFrac, int inDenom)
	{
		if (inDenom <= 1 || inFrac == 0) {return "" + inFrac;}
		String denomString = "" + inDenom;
		int reqdLen = denomString.length() - 1;
		String result = denomString + inFrac;
		int resultLen = result.length();
		return result.substring(resultLen - reqdLen);
	}


	/**
	 * Format an integer to a two-digit String
	 * @param inNumber number to format
	 * @return two-character String
	 */
	private static String twoDigitString(int inNumber)
	{
		if (inNumber <= 0) return "00";
		if (inNumber < 10) return "0" + inNumber;
		if (inNumber < 100) return "" + inNumber;
		return "" + (inNumber % 100);
	}


	/**
	 * Format an integer to a three-digit String for degrees
	 * @param inNumber number to format
	 * @return three-character String
	 */
	private static String threeDigitString(int inNumber)
	{
		if (inNumber <= 0) return "000";
		if (inNumber < 10) return "00" + inNumber;
		if (inNumber < 100) return "0" + inNumber;
		return "" + (inNumber % 1000);
	}


	/**
	 * Create a new Coordinate between two others
	 * @param inStart start coordinate
	 * @param inEnd end coordinate
	 * @param inIndex index of point
	 * @param inNumPoints number of points to interpolate
	 * @return new Coordinate object
	 */
	public static Coordinate interpolate(Coordinate inStart, Coordinate inEnd,
		int inIndex, int inNumPoints)
	{
		return interpolate(inStart, inEnd, 1.0 * (inIndex+1) / (inNumPoints + 1));
	}


	/**
	 * Create a new Coordinate between two others
	 * @param inStart start coordinate
	 * @param inEnd end coordinate
	 * @param inFraction fraction from start to end
	 * @return new Coordinate object
	 */
	public static Coordinate interpolate(Coordinate inStart, Coordinate inEnd,
		double inFraction)
	{
		double startValue = inStart.getDouble();
		double endValue = inEnd.getDouble();
		double newValue = startValue + (endValue - startValue) * inFraction;
		Coordinate answer = inStart.makeNew(newValue, inStart._originalFormat);
		return answer;
	}


	/**
	 * Make a new Coordinate according to subclass
	 * @param inValue double value
	 * @param inFormat format to use
	 * @return object of Coordinate subclass
	 */
	protected abstract Coordinate makeNew(double inValue, int inFormat);

	/**
	 * Try to parse the given string
	 * @param inString string to check
	 * @return true if it can be parsed as a number
	 */
	private static boolean isJustNumber(String inString)
	{
		boolean justNum = false;
		try {
			double x = Double.parseDouble(inString);
			justNum = (x >= -180.0 && x <= 360.0);
		}
		catch (NumberFormatException nfe) {} // flag remains false
		return justNum;
	}

	/**
	 * Create a String representation for debug
	 * @return String describing coordinate value
	 */
	public String toString()
	{
		return "Coord: " + _cardinal + " (" + _degrees + ") (" + _minutes + ") (" + _seconds + "."
			+ formatFraction(_fracs, _fracDenom) + ") = " + _asDouble;
	}
}