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 /** @return latitude */
142 public Coordinate getLatitude()
146 /** @return longitude */
147 public Coordinate getLongitude()
151 /** @return true if point has altitude */
152 public boolean hasAltitude()
154 return _altitude.isValid();
156 /** @return altitude */
157 public Altitude getAltitude()
161 /** @return true if point has timestamp */
162 public boolean hasTimestamp()
164 return _timestamp.isValid();
166 /** @return timestamp */
167 public Timestamp getTimestamp()
171 /** @return waypoint name, if any */
172 public String getWaypointName()
174 return _waypointName;
178 * @return true if point has a waypoint name
180 public boolean isWaypoint()
182 return (_waypointName != null && !_waypointName.equals(""));
187 * Compare two DataPoint objects to see if they are duplicates
188 * @param inOther other object to compare
189 * @return true if the points are equivalent
191 public boolean isDuplicate(DataPoint inOther)
193 if (inOther == null) return false;
194 if (_longitude == null || _latitude == null
195 || inOther._longitude == null || inOther._latitude == null)
199 // Make sure photo points aren't specified as duplicates
200 if (_photo != null) return false;
201 // Compare latitude and longitude
202 if (!_longitude.equals(inOther._longitude) || !_latitude.equals(inOther._latitude))
206 // Note that conversion from decimal to dms can make non-identical points into duplicates
207 // Compare waypoint name (if any)
210 return !inOther.isWaypoint();
212 return (inOther._waypointName != null && inOther._waypointName.equals(_waypointName));
217 * Set the photo for this data point
218 * @param inPhoto Photo object
220 public void setPhoto(Photo inPhoto)
227 * @return associated Photo object
229 public Photo getPhoto()
236 * @return true if the point is valid
238 public boolean isValid()
240 return _latitude.isValid() && _longitude.isValid();
245 * Interpolate a set of points between this one and the given one
246 * @param inEndPoint end point of interpolation
247 * @param inNumPoints number of points to generate
248 * @return the DataPoint array
250 public DataPoint[] interpolate(DataPoint inEndPoint, int inNumPoints)
252 DataPoint[] range = new DataPoint[inNumPoints];
254 for (int i=0; i<inNumPoints; i++)
256 Coordinate latitude = Coordinate.interpolate(_latitude, inEndPoint.getLatitude(), i, inNumPoints);
257 Coordinate longitude = Coordinate.interpolate(_longitude, inEndPoint.getLongitude(), i, inNumPoints);
258 Altitude altitude = Altitude.interpolate(_altitude, inEndPoint.getAltitude(), i, inNumPoints);
259 range[i] = new DataPoint(latitude, longitude, altitude);
265 * Interpolate between the two given points
266 * @param inStartPoint start point
267 * @param inEndPoint end point
268 * @param inFrac fractional distance from first point (0.0 to 1.0)
269 * @return new DataPoint object between two given ones
271 public static DataPoint interpolate(DataPoint inStartPoint, DataPoint inEndPoint, double inFrac)
273 if (inStartPoint == null || inEndPoint == null) {return null;}
274 return new DataPoint(
275 Coordinate.interpolate(inStartPoint.getLatitude(), inEndPoint.getLatitude(), inFrac),
276 Coordinate.interpolate(inStartPoint.getLongitude(), inEndPoint.getLongitude(), inFrac),
277 Altitude.interpolate(inStartPoint.getAltitude(), inEndPoint.getAltitude(), inFrac)
282 * Calculate the number of radians between two points (for distance calculation)
283 * @param inPoint1 first point
284 * @param inPoint2 second point
285 * @return angular distance between points in radians
287 public static double calculateRadiansBetween(DataPoint inPoint1, DataPoint inPoint2)
289 if (inPoint1 == null || inPoint2 == null)
291 final double TO_RADIANS = Math.PI / 180.0;
292 // Get lat and long from points
293 double lat1 = inPoint1.getLatitude().getDouble() * TO_RADIANS;
294 double lat2 = inPoint2.getLatitude().getDouble() * TO_RADIANS;
295 double lon1 = inPoint1.getLongitude().getDouble() * TO_RADIANS;
296 double lon2 = inPoint2.getLongitude().getDouble() * TO_RADIANS;
297 // Formula given by Wikipedia:Great-circle_distance as follows:
298 // angle = 2 arcsin( sqrt( (sin ((lat2-lat1)/2))^^2 + cos(lat1)cos(lat2)(sin((lon2-lon1)/2))^^2))
299 double firstSine = Math.sin((lat2-lat1) / 2.0);
300 double secondSine = Math.sin((lon2-lon1) / 2.0);
301 double term2 = Math.cos(lat1) * Math.cos(lat2) * secondSine * secondSine;
302 double answer = 2 * Math.asin(Math.sqrt(firstSine*firstSine + term2));
309 * Resize the value array
310 * @param inNewIndex new index to allow
312 private void resizeValueArray(int inNewIndex)
314 int newSize = inNewIndex + 1;
315 if (newSize > _fieldValues.length)
317 String[] newArray = new String[newSize];
318 System.arraycopy(_fieldValues, 0, newArray, 0, _fieldValues.length);
319 _fieldValues = newArray;
325 * @return a clone object with copied data
327 public DataPoint clonePoint()
330 String[] valuesCopy = new String[_fieldValues.length];
331 System.arraycopy(_fieldValues, 0, valuesCopy, 0, _fieldValues.length);
332 // Make new object to hold cloned data
333 DataPoint point = new DataPoint(valuesCopy, _fieldList, _altitude.getFormat());
339 * Restore the contents from another point
340 * @param inOther point containing contents to copy
341 * @return true if successful
343 public boolean restoreContents(DataPoint inOther)
347 // Copy string values across
348 _fieldValues = inOther._fieldValues;
349 if (_altitude != null)
351 parseFields(_altitude.getFormat());
355 // use default altitude format of metres
356 parseFields(Altitude.FORMAT_METRES);
365 * Get string for debug
366 * @see java.lang.Object#toString()
368 public String toString()
370 return "[Lat=" + getLatitude().toString() + ", Lon=" + getLongitude().toString() + "]";