1 package tim.prune.data;
4 * Class to represent a single data point in the series
5 * including all its fields
6 * Can be either a track point or a waypoint
10 /** Array of Strings holding raw values */
11 private String[] _fieldValues = null;
12 /** list of field definitions */
13 private FieldList _fieldList = null;
14 /** Special fields for coordinates */
15 private Coordinate _latitude = null, _longitude = null;
16 private Altitude _altitude;
17 private Timestamp _timestamp = null;
18 private Photo _photo = null;
19 private String _waypointName = null;
20 // private boolean _startOfSegment = false;
25 * @param inValueArray array of String values
26 * @param inFieldList list of fields
27 * @param inAltFormat altitude format
29 public DataPoint(String[] inValueArray, FieldList inFieldList, int inAltFormat)
32 _fieldValues = inValueArray;
33 // save list of fields
34 _fieldList = inFieldList;
35 // parse fields into objects
36 parseFields(inAltFormat);
41 * Parse the string values into objects eg Coordinates
42 * @param inAltFormat altitude format
44 private void parseFields(int inAltFormat)
46 _latitude = new Latitude(getFieldValue(Field.LATITUDE));
47 _longitude = new Longitude(getFieldValue(Field.LONGITUDE));
48 _altitude = new Altitude(getFieldValue(Field.ALTITUDE), inAltFormat);
49 _timestamp = new Timestamp(getFieldValue(Field.TIMESTAMP));
50 _waypointName = getFieldValue(Field.WAYPT_NAME);
51 // TODO: Parse segment start field (format?)
56 * Constructor for additional points (eg interpolated, photos)
57 * @param inLatitude latitude
58 * @param inLongitude longitude
59 * @param inAltitude altitude
61 public DataPoint(Coordinate inLatitude, Coordinate inLongitude, Altitude inAltitude)
63 // Only these three fields are available
64 _fieldValues = new String[3];
65 Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE};
66 _fieldList = new FieldList(fields);
67 _latitude = inLatitude;
68 _fieldValues[0] = inLatitude.output(Coordinate.FORMAT_DEG_MIN_SEC);
69 _longitude = inLongitude;
70 _fieldValues[1] = inLongitude.output(Coordinate.FORMAT_DEG_MIN_SEC);
71 _altitude = inAltitude;
72 if (inAltitude != null) {_fieldValues[2] = "" + inAltitude.getValue(Altitude.FORMAT_METRES);}
73 _timestamp = new Timestamp(null);
78 * Get the value for the given field
79 * @param inField field to interrogate
80 * @return value of field
82 public String getFieldValue(Field inField)
84 return getFieldValue(_fieldList.getFieldIndex(inField));
89 * Get the value at the given index
90 * @param inIndex index number starting at zero
91 * @return field value, or null if not found
93 public String getFieldValue(int inIndex)
95 if (_fieldValues == null || inIndex < 0 || inIndex >= _fieldValues.length)
97 return _fieldValues[inIndex];
102 * Set (or edit) the specified field value
103 * @param inField Field to set
104 * @param inValue value to set
106 public void setFieldValue(Field inField, String inValue)
108 // See if this data point already has this field
109 int fieldIndex = _fieldList.getFieldIndex(inField);
110 // Add to field list if necessary
113 // If value is empty & field doesn't exist then do nothing
114 if (inValue == null || inValue.equals(""))
118 // value isn't empty so extend field list
119 fieldIndex = _fieldList.extendList(inField);
121 // Extend array of field values if necessary
122 if (fieldIndex >= _fieldValues.length)
124 resizeValueArray(fieldIndex);
126 // Set field value in array
127 _fieldValues[fieldIndex] = inValue;
128 // Change Coordinate, Altitude, Name or Timestamp fields after edit
129 if (_altitude != null)
131 parseFields(_altitude.getFormat());
135 // use default altitude format of metres
136 parseFields(Altitude.FORMAT_METRES);
141 public Coordinate getLatitude()
145 public Coordinate getLongitude()
149 public boolean hasAltitude()
151 return _altitude.isValid();
153 public Altitude getAltitude()
157 public boolean hasTimestamp()
159 return _timestamp.isValid();
161 public Timestamp getTimestamp()
165 public String getWaypointName()
167 return _waypointName;
171 * @return true if point has a waypoint name
173 public boolean isWaypoint()
175 return (_waypointName != null && !_waypointName.equals(""));
180 * Compare two DataPoint objects to see if they are duplicates
181 * @param inOther other object to compare
182 * @return true if the points are equivalent
184 public boolean isDuplicate(DataPoint inOther)
186 if (inOther == null) return false;
187 if (_longitude == null || _latitude == null
188 || inOther._longitude == null || inOther._latitude == null)
192 // Make sure photo points aren't specified as duplicates
193 if (_photo != null) return false;
194 // Compare latitude and longitude
195 if (!_longitude.equals(inOther._longitude) || !_latitude.equals(inOther._latitude))
199 // Note that conversion from decimal to dms can make non-identical points into duplicates
200 // Compare waypoint name (if any)
203 return !inOther.isWaypoint();
207 return (inOther._waypointName != null && inOther._waypointName.equals(_waypointName));
213 * Set the photo for this data point
214 * @param inPhoto Photo object
216 public void setPhoto(Photo inPhoto)
223 * @return associated Photo object
225 public Photo getPhoto()
232 * @return true if the point is valid
234 public boolean isValid()
236 return _latitude.isValid() && _longitude.isValid();
241 * Interpolate a set of points between this one and the given one
242 * @param inEndPoint end point of interpolation
243 * @param inNumPoints number of points to generate
244 * @return the DataPoint array
246 public DataPoint[] interpolate(DataPoint inEndPoint, int inNumPoints)
248 DataPoint[] range = new DataPoint[inNumPoints];
249 Coordinate endLatitude = inEndPoint.getLatitude();
250 Coordinate endLongitude = inEndPoint.getLongitude();
251 Altitude endAltitude = inEndPoint.getAltitude();
254 for (int i=0; i<inNumPoints; i++)
256 Coordinate latitude = Coordinate.interpolate(_latitude, endLatitude, i, inNumPoints);
257 Coordinate longitude = Coordinate.interpolate(_longitude, endLongitude, i, inNumPoints);
258 Altitude altitude = Altitude.interpolate(_altitude, endAltitude, i, inNumPoints);
259 range[i] = new DataPoint(latitude, longitude, altitude);
266 * Calculate the number of radians between two points (for distance calculation)
267 * @param inPoint1 first point
268 * @param inPoint2 second point
269 * @return angular distance between points in radians
271 public static double calculateRadiansBetween(DataPoint inPoint1, DataPoint inPoint2)
273 if (inPoint1 == null || inPoint2 == null)
275 final double TO_RADIANS = Math.PI / 180.0;
276 // Get lat and long from points
277 double lat1 = inPoint1.getLatitude().getDouble() * TO_RADIANS;
278 double lat2 = inPoint2.getLatitude().getDouble() * TO_RADIANS;
279 double lon1 = inPoint1.getLongitude().getDouble() * TO_RADIANS;
280 double lon2 = inPoint2.getLongitude().getDouble() * TO_RADIANS;
281 // Formula given by Wikipedia:Great-circle_distance as follows:
282 // angle = 2 arcsin( sqrt( (sin ((lat2-lat1)/2))^^2 + cos(lat1)cos(lat2)(sin((lon2-lon1)/2))^^2))
283 double firstSine = Math.sin((lat2-lat1) / 2.0);
284 double secondSine = Math.sin((lon2-lon1) / 2.0);
285 double term2 = Math.cos(lat1) * Math.cos(lat2) * secondSine * secondSine;
286 double answer = 2 * Math.asin(Math.sqrt(firstSine*firstSine + term2));
293 * Resize the value array
294 * @param inNewIndex new index to allow
296 private void resizeValueArray(int inNewIndex)
298 int newSize = inNewIndex + 1;
299 if (newSize > _fieldValues.length)
301 String[] newArray = new String[newSize];
302 System.arraycopy(_fieldValues, 0, newArray, 0, _fieldValues.length);
303 _fieldValues = newArray;
309 * @return a clone object with copied data
311 public DataPoint clonePoint()
314 String[] valuesCopy = new String[_fieldValues.length];
315 System.arraycopy(_fieldValues, 0, valuesCopy, 0, _fieldValues.length);
316 // Make new object to hold cloned data
317 DataPoint point = new DataPoint(valuesCopy, _fieldList, _altitude.getFormat());
323 * Restore the contents from another point
324 * @param inOther point containing contents to copy
325 * @return true if successful
327 public boolean restoreContents(DataPoint inOther)
331 // Copy string values across
332 _fieldValues = inOther._fieldValues;
333 if (_altitude != null)
335 parseFields(_altitude.getFormat());
339 // use default altitude format of metres
340 parseFields(Altitude.FORMAT_METRES);