1 package tim.prune.data;
3 import tim.prune.config.Config;
6 * Class to represent a single data point in the series
7 * including all its fields
8 * Can be either a track point or a waypoint
10 public class DataPoint
12 /** Array of Strings holding raw values */
13 private String[] _fieldValues = null;
14 /** list of field definitions */
15 private FieldList _fieldList = null;
16 /** Special fields for coordinates */
17 private Coordinate _latitude = null, _longitude = null;
18 private Altitude _altitude;
19 private Timestamp _timestamp = null;
21 private Photo _photo = null;
22 /** Attached audio file */
23 private AudioFile _audio = null;
24 private String _waypointName = null;
25 private boolean _startOfSegment = false;
26 private boolean _markedForDeletion = false;
27 private int _modifyCount = 0;
31 * @param inValueArray array of String values
32 * @param inFieldList list of fields
33 * @param inAltFormat altitude format
35 public DataPoint(String[] inValueArray, FieldList inFieldList, Altitude.Format inAltFormat)
38 _fieldValues = inValueArray;
39 // save list of fields
40 _fieldList = inFieldList;
41 // parse fields into objects
42 parseFields(null, inAltFormat);
47 * Parse the string values into objects eg Coordinates
48 * @param inField field which has changed, or null for all
49 * @param inAltFormat altitude format
51 private void parseFields(Field inField, Altitude.Format inAltFormat)
53 if (inField == null || inField == Field.LATITUDE) {
54 _latitude = new Latitude(getFieldValue(Field.LATITUDE));
56 if (inField == null || inField == Field.LONGITUDE) {
57 _longitude = new Longitude(getFieldValue(Field.LONGITUDE));
59 if (inField == null || inField == Field.ALTITUDE) {
60 _altitude = new Altitude(getFieldValue(Field.ALTITUDE), inAltFormat);
62 if (inField == null || inField == Field.TIMESTAMP) {
63 _timestamp = new Timestamp(getFieldValue(Field.TIMESTAMP));
65 if (inField == null || inField == Field.WAYPT_NAME) {
66 _waypointName = getFieldValue(Field.WAYPT_NAME);
68 if (inField == null || inField == Field.NEW_SEGMENT)
70 String segmentStr = getFieldValue(Field.NEW_SEGMENT);
71 if (segmentStr != null) {segmentStr = segmentStr.trim();}
72 _startOfSegment = (segmentStr != null && (segmentStr.equals("1") || segmentStr.toUpperCase().equals("Y")));
78 * Constructor for additional points (eg interpolated, photos)
79 * @param inLatitude latitude
80 * @param inLongitude longitude
81 * @param inAltitude altitude
83 public DataPoint(Coordinate inLatitude, Coordinate inLongitude, Altitude inAltitude)
85 // Only these three fields are available
86 _fieldValues = new String[3];
87 Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE};
88 _fieldList = new FieldList(fields);
89 _latitude = inLatitude;
90 _fieldValues[0] = inLatitude.output(Coordinate.FORMAT_NONE);
91 _longitude = inLongitude;
92 _fieldValues[1] = inLongitude.output(Coordinate.FORMAT_NONE);
93 if (inAltitude == null) {
94 _altitude = Altitude.NONE;
97 _altitude = inAltitude;
98 _fieldValues[2] = "" + inAltitude.getValue();
100 _timestamp = new Timestamp(null);
105 * Get the value for the given field
106 * @param inField field to interrogate
107 * @return value of field
109 public String getFieldValue(Field inField)
111 return getFieldValue(_fieldList.getFieldIndex(inField));
116 * Get the value at the given index
117 * @param inIndex index number starting at zero
118 * @return field value, or null if not found
120 public String getFieldValue(int inIndex)
122 if (_fieldValues == null || inIndex < 0 || inIndex >= _fieldValues.length)
124 return _fieldValues[inIndex];
129 * Set (or edit) the specified field value
130 * @param inField Field to set
131 * @param inValue value to set
132 * @param inUndo true if undo operation, false otherwise
134 public void setFieldValue(Field inField, String inValue, boolean inUndo)
136 // See if this data point already has this field
137 int fieldIndex = _fieldList.getFieldIndex(inField);
138 // Add to field list if necessary
141 // If value is empty & field doesn't exist then do nothing
142 if (inValue == null || inValue.equals(""))
146 // value isn't empty so extend field list
147 fieldIndex = _fieldList.extendList(inField);
149 // Extend array of field values if necessary
150 if (fieldIndex >= _fieldValues.length)
152 resizeValueArray(fieldIndex);
154 // Set field value in array
155 _fieldValues[fieldIndex] = inValue;
156 // Increment edit count on all field edits except segment
157 if (inField != Field.NEW_SEGMENT) {
160 // Change Coordinate, Altitude, Name or Timestamp fields after edit
161 if (_altitude != null && _altitude.getFormat() != Altitude.Format.NO_FORMAT) {
162 // Altitude already present so reuse format
163 parseFields(inField, _altitude.getFormat());
166 // use default altitude format from config
167 parseFields(inField, Config.getConfigBoolean(Config.KEY_METRIC_UNITS)?Altitude.Format.METRES:Altitude.Format.FEET);
172 * Either increment or decrement the modify count, depending on whether it's an undo or not
173 * @param inUndo true for undo, false otherwise
175 public void setModified(boolean inUndo)
186 * @return field list for this point
188 public FieldList getFieldList()
193 /** @param inFlag true for start of track segment */
194 public void setSegmentStart(boolean inFlag)
196 setFieldValue(Field.NEW_SEGMENT, inFlag?"1":null, false);
200 * Mark the point for deletion
201 * @param inFlag true to delete, false to keep
203 public void setMarkedForDeletion(boolean inFlag) {
204 _markedForDeletion = inFlag;
207 /** @return latitude */
208 public Coordinate getLatitude()
212 /** @return longitude */
213 public Coordinate getLongitude()
217 /** @return true if point has altitude */
218 public boolean hasAltitude()
220 return _altitude.isValid();
222 /** @return altitude */
223 public Altitude getAltitude()
227 /** @return true if point has timestamp */
228 public boolean hasTimestamp()
230 return _timestamp.isValid();
232 /** @return timestamp */
233 public Timestamp getTimestamp()
237 /** @return waypoint name, if any */
238 public String getWaypointName()
240 return _waypointName;
243 /** @return true if start of new track segment */
244 public boolean getSegmentStart()
246 return _startOfSegment;
249 /** @return true if point marked for deletion */
250 public boolean getDeleteFlag()
252 return _markedForDeletion;
256 * @return true if point has a waypoint name
258 public boolean isWaypoint()
260 return (_waypointName != null && !_waypointName.equals(""));
264 * @return true if point has been modified since loading
266 public boolean isModified()
268 return _modifyCount > 0;
272 * Compare two DataPoint objects to see if they are duplicates
273 * @param inOther other object to compare
274 * @return true if the points are equivalent
276 public boolean isDuplicate(DataPoint inOther)
278 if (inOther == null) return false;
279 if (_longitude == null || _latitude == null
280 || inOther._longitude == null || inOther._latitude == null)
284 // Make sure photo points aren't specified as duplicates
285 if (_photo != null) return false;
286 // Compare latitude and longitude
287 if (!_longitude.equals(inOther._longitude) || !_latitude.equals(inOther._latitude))
291 // Note that conversion from decimal to dms can make non-identical points into duplicates
292 // Compare waypoint name (if any)
295 return !inOther.isWaypoint();
297 return (inOther._waypointName != null && inOther._waypointName.equals(_waypointName));
302 * Set the photo for this data point
303 * @param inPhoto Photo object
305 public void setPhoto(Photo inPhoto) {
311 * @return associated Photo object
313 public Photo getPhoto() {
318 * Set the audio file for this point
319 * @param inAudio audio object
321 public void setAudio(AudioFile inAudio) {
327 * @return associated audio object
329 public AudioFile getAudio() {
334 * Attach the given media object according to type
335 * @param inMedia either a photo or an audio file
337 public void attachMedia(MediaFile inMedia)
339 if (inMedia != null) {
340 if (inMedia instanceof Photo) {
341 setPhoto((Photo) inMedia);
342 inMedia.setDataPoint(this);
344 else if (inMedia instanceof AudioFile) {
345 setAudio((AudioFile) inMedia);
346 inMedia.setDataPoint(this);
352 * @return true if the point is valid
354 public boolean isValid()
356 return _latitude.isValid() && _longitude.isValid();
360 * @return true if the point has either a photo or audio attached
362 public boolean hasMedia() {
363 return _photo != null || _audio != null;
367 * Interpolate a set of points between this one and the given one
368 * @param inEndPoint end point of interpolation
369 * @param inNumPoints number of points to generate
370 * @return the DataPoint array
372 public DataPoint[] interpolate(DataPoint inEndPoint, int inNumPoints)
374 DataPoint[] range = new DataPoint[inNumPoints];
376 for (int i=0; i<inNumPoints; i++)
378 Coordinate latitude = Coordinate.interpolate(_latitude, inEndPoint.getLatitude(), i, inNumPoints);
379 Coordinate longitude = Coordinate.interpolate(_longitude, inEndPoint.getLongitude(), i, inNumPoints);
380 Altitude altitude = Altitude.interpolate(_altitude, inEndPoint.getAltitude(), i, inNumPoints);
381 range[i] = new DataPoint(latitude, longitude, altitude);
387 * Interpolate between the two given points
388 * @param inStartPoint start point
389 * @param inEndPoint end point
390 * @param inFrac fractional distance from first point (0.0 to 1.0)
391 * @return new DataPoint object between two given ones
393 public static DataPoint interpolate(DataPoint inStartPoint, DataPoint inEndPoint, double inFrac)
395 if (inStartPoint == null || inEndPoint == null) {return null;}
396 return new DataPoint(
397 Coordinate.interpolate(inStartPoint.getLatitude(), inEndPoint.getLatitude(), inFrac),
398 Coordinate.interpolate(inStartPoint.getLongitude(), inEndPoint.getLongitude(), inFrac),
399 Altitude.interpolate(inStartPoint.getAltitude(), inEndPoint.getAltitude(), inFrac)
404 * Calculate the number of radians between two points (for distance calculation)
405 * @param inPoint1 first point
406 * @param inPoint2 second point
407 * @return angular distance between points in radians
409 public static double calculateRadiansBetween(DataPoint inPoint1, DataPoint inPoint2)
411 if (inPoint1 == null || inPoint2 == null)
413 final double TO_RADIANS = Math.PI / 180.0;
414 // Get lat and long from points
415 double lat1 = inPoint1.getLatitude().getDouble() * TO_RADIANS;
416 double lat2 = inPoint2.getLatitude().getDouble() * TO_RADIANS;
417 double lon1 = inPoint1.getLongitude().getDouble() * TO_RADIANS;
418 double lon2 = inPoint2.getLongitude().getDouble() * TO_RADIANS;
419 // Formula given by Wikipedia:Great-circle_distance as follows:
420 // angle = 2 arcsin( sqrt( (sin ((lat2-lat1)/2))^^2 + cos(lat1)cos(lat2)(sin((lon2-lon1)/2))^^2))
421 double firstSine = Math.sin((lat2-lat1) / 2.0);
422 double secondSine = Math.sin((lon2-lon1) / 2.0);
423 double term2 = Math.cos(lat1) * Math.cos(lat2) * secondSine * secondSine;
424 double answer = 2 * Math.asin(Math.sqrt(firstSine*firstSine + term2));
431 * Resize the value array
432 * @param inNewIndex new index to allow
434 private void resizeValueArray(int inNewIndex)
436 int newSize = inNewIndex + 1;
437 if (newSize > _fieldValues.length)
439 String[] newArray = new String[newSize];
440 System.arraycopy(_fieldValues, 0, newArray, 0, _fieldValues.length);
441 _fieldValues = newArray;
447 * @return a clone object with copied data
449 public DataPoint clonePoint()
451 // Copy all values (note that photo not copied)
452 String[] valuesCopy = new String[_fieldValues.length];
453 System.arraycopy(_fieldValues, 0, valuesCopy, 0, _fieldValues.length);
454 // Make new object to hold cloned data
455 DataPoint point = new DataPoint(valuesCopy, _fieldList, _altitude.getFormat());
461 * Get string for debug
462 * @see java.lang.Object#toString()
464 public String toString()
466 return "[Lat=" + getLatitude().toString() + ", Lon=" + getLongitude().toString() + "]";