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;
20 private Photo _photo = null;
21 private String _waypointName = null;
22 private boolean _startOfSegment = false;
23 private boolean _markedForDeletion = false;
24 private int _modifyCount = 0;
28 * @param inValueArray array of String values
29 * @param inFieldList list of fields
30 * @param inAltFormat altitude format
32 public DataPoint(String[] inValueArray, FieldList inFieldList, Altitude.Format inAltFormat)
35 _fieldValues = inValueArray;
36 // save list of fields
37 _fieldList = inFieldList;
38 // parse fields into objects
39 parseFields(null, inAltFormat);
44 * Parse the string values into objects eg Coordinates
45 * @param inField field which has changed, or null for all
46 * @param inAltFormat altitude format
48 private void parseFields(Field inField, Altitude.Format inAltFormat)
50 if (inField == null || inField == Field.LATITUDE) {
51 _latitude = new Latitude(getFieldValue(Field.LATITUDE));
53 if (inField == null || inField == Field.LONGITUDE) {
54 _longitude = new Longitude(getFieldValue(Field.LONGITUDE));
56 if (inField == null || inField == Field.ALTITUDE) {
57 _altitude = new Altitude(getFieldValue(Field.ALTITUDE), inAltFormat);
59 if (inField == null || inField == Field.TIMESTAMP) {
60 _timestamp = new Timestamp(getFieldValue(Field.TIMESTAMP));
62 if (inField == null || inField == Field.WAYPT_NAME) {
63 _waypointName = getFieldValue(Field.WAYPT_NAME);
65 if (inField == null || inField == Field.NEW_SEGMENT)
67 String segmentStr = getFieldValue(Field.NEW_SEGMENT);
68 if (segmentStr != null) {segmentStr = segmentStr.trim();}
69 _startOfSegment = (segmentStr != null && (segmentStr.equals("1") || segmentStr.toUpperCase().equals("Y")));
75 * Constructor for additional points (eg interpolated, photos)
76 * @param inLatitude latitude
77 * @param inLongitude longitude
78 * @param inAltitude altitude
80 public DataPoint(Coordinate inLatitude, Coordinate inLongitude, Altitude inAltitude)
82 // Only these three fields are available
83 _fieldValues = new String[3];
84 Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE};
85 _fieldList = new FieldList(fields);
86 _latitude = inLatitude;
87 _fieldValues[0] = inLatitude.output(Coordinate.FORMAT_NONE);
88 _longitude = inLongitude;
89 _fieldValues[1] = inLongitude.output(Coordinate.FORMAT_NONE);
90 if (inAltitude == null) {
91 _altitude = Altitude.NONE;
94 _altitude = inAltitude;
95 _fieldValues[2] = "" + inAltitude.getValue();
97 _timestamp = new Timestamp(null);
102 * Get the value for the given field
103 * @param inField field to interrogate
104 * @return value of field
106 public String getFieldValue(Field inField)
108 return getFieldValue(_fieldList.getFieldIndex(inField));
113 * Get the value at the given index
114 * @param inIndex index number starting at zero
115 * @return field value, or null if not found
117 public String getFieldValue(int inIndex)
119 if (_fieldValues == null || inIndex < 0 || inIndex >= _fieldValues.length)
121 return _fieldValues[inIndex];
126 * Set (or edit) the specified field value
127 * @param inField Field to set
128 * @param inValue value to set
129 * @param inUndo true if undo operation, false otherwise
131 public void setFieldValue(Field inField, String inValue, boolean inUndo)
133 // See if this data point already has this field
134 int fieldIndex = _fieldList.getFieldIndex(inField);
135 // Add to field list if necessary
138 // If value is empty & field doesn't exist then do nothing
139 if (inValue == null || inValue.equals(""))
143 // value isn't empty so extend field list
144 fieldIndex = _fieldList.extendList(inField);
146 // Extend array of field values if necessary
147 if (fieldIndex >= _fieldValues.length)
149 resizeValueArray(fieldIndex);
151 // Set field value in array
152 _fieldValues[fieldIndex] = inValue;
153 // Increment edit count on all field edits except segment
154 if (inField != Field.NEW_SEGMENT) {
162 // Change Coordinate, Altitude, Name or Timestamp fields after edit
163 if (_altitude != null && _altitude.getFormat() != Altitude.Format.NO_FORMAT) {
164 // Altitude already present so reuse format
165 parseFields(inField, _altitude.getFormat());
168 // use default altitude format from config
169 parseFields(inField, Config.getConfigBoolean(Config.KEY_METRIC_UNITS)?Altitude.Format.METRES:Altitude.Format.FEET);
174 * @return field list for this point
176 public FieldList getFieldList()
181 /** @param inFlag true for start of track segment */
182 public void setSegmentStart(boolean inFlag)
184 setFieldValue(Field.NEW_SEGMENT, inFlag?"1":null, false);
188 * Mark the point for deletion
189 * @param inFlag true to delete, false to keep
191 public void setMarkedForDeletion(boolean inFlag) {
192 _markedForDeletion = inFlag;
195 /** @return latitude */
196 public Coordinate getLatitude()
200 /** @return longitude */
201 public Coordinate getLongitude()
205 /** @return true if point has altitude */
206 public boolean hasAltitude()
208 return _altitude.isValid();
210 /** @return altitude */
211 public Altitude getAltitude()
215 /** @return true if point has timestamp */
216 public boolean hasTimestamp()
218 return _timestamp.isValid();
220 /** @return timestamp */
221 public Timestamp getTimestamp()
225 /** @return waypoint name, if any */
226 public String getWaypointName()
228 return _waypointName;
231 /** @return true if start of new track segment */
232 public boolean getSegmentStart()
234 return _startOfSegment;
237 /** @return true if point marked for deletion */
238 public boolean getDeleteFlag()
240 return _markedForDeletion;
244 * @return true if point has a waypoint name
246 public boolean isWaypoint()
248 return (_waypointName != null && !_waypointName.equals(""));
252 * @return true if point has been modified since loading
254 public boolean isModified()
256 return _modifyCount > 0;
260 * Compare two DataPoint objects to see if they are duplicates
261 * @param inOther other object to compare
262 * @return true if the points are equivalent
264 public boolean isDuplicate(DataPoint inOther)
266 if (inOther == null) return false;
267 if (_longitude == null || _latitude == null
268 || inOther._longitude == null || inOther._latitude == null)
272 // Make sure photo points aren't specified as duplicates
273 if (_photo != null) return false;
274 // Compare latitude and longitude
275 if (!_longitude.equals(inOther._longitude) || !_latitude.equals(inOther._latitude))
279 // Note that conversion from decimal to dms can make non-identical points into duplicates
280 // Compare waypoint name (if any)
283 return !inOther.isWaypoint();
285 return (inOther._waypointName != null && inOther._waypointName.equals(_waypointName));
290 * Set the photo for this data point
291 * @param inPhoto Photo object
293 public void setPhoto(Photo inPhoto)
300 * @return associated Photo object
302 public Photo getPhoto()
309 * @return true if the point is valid
311 public boolean isValid()
313 return _latitude.isValid() && _longitude.isValid();
317 * Interpolate a set of points between this one and the given one
318 * @param inEndPoint end point of interpolation
319 * @param inNumPoints number of points to generate
320 * @return the DataPoint array
322 public DataPoint[] interpolate(DataPoint inEndPoint, int inNumPoints)
324 DataPoint[] range = new DataPoint[inNumPoints];
326 for (int i=0; i<inNumPoints; i++)
328 Coordinate latitude = Coordinate.interpolate(_latitude, inEndPoint.getLatitude(), i, inNumPoints);
329 Coordinate longitude = Coordinate.interpolate(_longitude, inEndPoint.getLongitude(), i, inNumPoints);
330 Altitude altitude = Altitude.interpolate(_altitude, inEndPoint.getAltitude(), i, inNumPoints);
331 range[i] = new DataPoint(latitude, longitude, altitude);
337 * Interpolate between the two given points
338 * @param inStartPoint start point
339 * @param inEndPoint end point
340 * @param inFrac fractional distance from first point (0.0 to 1.0)
341 * @return new DataPoint object between two given ones
343 public static DataPoint interpolate(DataPoint inStartPoint, DataPoint inEndPoint, double inFrac)
345 if (inStartPoint == null || inEndPoint == null) {return null;}
346 return new DataPoint(
347 Coordinate.interpolate(inStartPoint.getLatitude(), inEndPoint.getLatitude(), inFrac),
348 Coordinate.interpolate(inStartPoint.getLongitude(), inEndPoint.getLongitude(), inFrac),
349 Altitude.interpolate(inStartPoint.getAltitude(), inEndPoint.getAltitude(), inFrac)
354 * Calculate the number of radians between two points (for distance calculation)
355 * @param inPoint1 first point
356 * @param inPoint2 second point
357 * @return angular distance between points in radians
359 public static double calculateRadiansBetween(DataPoint inPoint1, DataPoint inPoint2)
361 if (inPoint1 == null || inPoint2 == null)
363 final double TO_RADIANS = Math.PI / 180.0;
364 // Get lat and long from points
365 double lat1 = inPoint1.getLatitude().getDouble() * TO_RADIANS;
366 double lat2 = inPoint2.getLatitude().getDouble() * TO_RADIANS;
367 double lon1 = inPoint1.getLongitude().getDouble() * TO_RADIANS;
368 double lon2 = inPoint2.getLongitude().getDouble() * TO_RADIANS;
369 // Formula given by Wikipedia:Great-circle_distance as follows:
370 // angle = 2 arcsin( sqrt( (sin ((lat2-lat1)/2))^^2 + cos(lat1)cos(lat2)(sin((lon2-lon1)/2))^^2))
371 double firstSine = Math.sin((lat2-lat1) / 2.0);
372 double secondSine = Math.sin((lon2-lon1) / 2.0);
373 double term2 = Math.cos(lat1) * Math.cos(lat2) * secondSine * secondSine;
374 double answer = 2 * Math.asin(Math.sqrt(firstSine*firstSine + term2));
381 * Resize the value array
382 * @param inNewIndex new index to allow
384 private void resizeValueArray(int inNewIndex)
386 int newSize = inNewIndex + 1;
387 if (newSize > _fieldValues.length)
389 String[] newArray = new String[newSize];
390 System.arraycopy(_fieldValues, 0, newArray, 0, _fieldValues.length);
391 _fieldValues = newArray;
397 * @return a clone object with copied data
399 public DataPoint clonePoint()
401 // Copy all values (note that photo not copied)
402 String[] valuesCopy = new String[_fieldValues.length];
403 System.arraycopy(_fieldValues, 0, valuesCopy, 0, _fieldValues.length);
404 // Make new object to hold cloned data
405 DataPoint point = new DataPoint(valuesCopy, _fieldList, _altitude.getFormat());
411 * Get string for debug
412 * @see java.lang.Object#toString()
414 public String toString()
416 return "[Lat=" + getLatitude().toString() + ", Lon=" + getLongitude().toString() + "]";