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 String segmentStr = getFieldValue(Field.NEW_SEGMENT);
52 if (segmentStr != null) {segmentStr = segmentStr.trim();}
53 _startOfSegment = (segmentStr != null && (segmentStr.equals("1") || segmentStr.toUpperCase().equals("Y")));
58 * Constructor for additional points (eg interpolated, photos)
59 * @param inLatitude latitude
60 * @param inLongitude longitude
61 * @param inAltitude altitude
63 public DataPoint(Coordinate inLatitude, Coordinate inLongitude, Altitude inAltitude)
65 // Only these three fields are available
66 _fieldValues = new String[3];
67 Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE};
68 _fieldList = new FieldList(fields);
69 _latitude = inLatitude;
70 _fieldValues[0] = inLatitude.output(Coordinate.FORMAT_DEG_MIN_SEC);
71 _longitude = inLongitude;
72 _fieldValues[1] = inLongitude.output(Coordinate.FORMAT_DEG_MIN_SEC);
73 _altitude = inAltitude;
74 if (inAltitude != null) {_fieldValues[2] = "" + inAltitude.getValue(Altitude.FORMAT_METRES);}
75 _timestamp = new Timestamp(null);
80 * Get the value for the given field
81 * @param inField field to interrogate
82 * @return value of field
84 public String getFieldValue(Field inField)
86 return getFieldValue(_fieldList.getFieldIndex(inField));
91 * Get the value at the given index
92 * @param inIndex index number starting at zero
93 * @return field value, or null if not found
95 public String getFieldValue(int inIndex)
97 if (_fieldValues == null || inIndex < 0 || inIndex >= _fieldValues.length)
99 return _fieldValues[inIndex];
104 * Set (or edit) the specified field value
105 * @param inField Field to set
106 * @param inValue value to set
108 public void setFieldValue(Field inField, String inValue)
110 // See if this data point already has this field
111 int fieldIndex = _fieldList.getFieldIndex(inField);
112 // Add to field list if necessary
115 // If value is empty & field doesn't exist then do nothing
116 if (inValue == null || inValue.equals(""))
120 // value isn't empty so extend field list
121 fieldIndex = _fieldList.extendList(inField);
123 // Extend array of field values if necessary
124 if (fieldIndex >= _fieldValues.length)
126 resizeValueArray(fieldIndex);
128 // Set field value in array
129 _fieldValues[fieldIndex] = inValue;
130 // Change Coordinate, Altitude, Name or Timestamp fields after edit
131 if (_altitude != null)
133 parseFields(_altitude.getFormat());
137 // use default altitude format of metres
138 parseFields(Altitude.FORMAT_METRES);
142 /** @param inFlag true for start of track segment */
143 public void setSegmentStart(boolean inFlag)
145 setFieldValue(Field.NEW_SEGMENT, inFlag?"1":null);
148 /** @return latitude */
149 public Coordinate getLatitude()
153 /** @return longitude */
154 public Coordinate getLongitude()
158 /** @return true if point has altitude */
159 public boolean hasAltitude()
161 return _altitude.isValid();
163 /** @return altitude */
164 public Altitude getAltitude()
168 /** @return true if point has timestamp */
169 public boolean hasTimestamp()
171 return _timestamp.isValid();
173 /** @return timestamp */
174 public Timestamp getTimestamp()
178 /** @return waypoint name, if any */
179 public String getWaypointName()
181 return _waypointName;
184 /** @return true if start of new track segment */
185 public boolean getSegmentStart()
187 return _startOfSegment;
191 * @return true if point has a waypoint name
193 public boolean isWaypoint()
195 return (_waypointName != null && !_waypointName.equals(""));
200 * Compare two DataPoint objects to see if they are duplicates
201 * @param inOther other object to compare
202 * @return true if the points are equivalent
204 public boolean isDuplicate(DataPoint inOther)
206 if (inOther == null) return false;
207 if (_longitude == null || _latitude == null
208 || inOther._longitude == null || inOther._latitude == null)
212 // Make sure photo points aren't specified as duplicates
213 if (_photo != null) return false;
214 // Compare latitude and longitude
215 if (!_longitude.equals(inOther._longitude) || !_latitude.equals(inOther._latitude))
219 // Note that conversion from decimal to dms can make non-identical points into duplicates
220 // Compare waypoint name (if any)
223 return !inOther.isWaypoint();
225 return (inOther._waypointName != null && inOther._waypointName.equals(_waypointName));
230 * Set the photo for this data point
231 * @param inPhoto Photo object
233 public void setPhoto(Photo inPhoto)
240 * @return associated Photo object
242 public Photo getPhoto()
249 * @return true if the point is valid
251 public boolean isValid()
253 return _latitude.isValid() && _longitude.isValid();
258 * Interpolate a set of points between this one and the given one
259 * @param inEndPoint end point of interpolation
260 * @param inNumPoints number of points to generate
261 * @return the DataPoint array
263 public DataPoint[] interpolate(DataPoint inEndPoint, int inNumPoints)
265 DataPoint[] range = new DataPoint[inNumPoints];
267 for (int i=0; i<inNumPoints; i++)
269 Coordinate latitude = Coordinate.interpolate(_latitude, inEndPoint.getLatitude(), i, inNumPoints);
270 Coordinate longitude = Coordinate.interpolate(_longitude, inEndPoint.getLongitude(), i, inNumPoints);
271 Altitude altitude = Altitude.interpolate(_altitude, inEndPoint.getAltitude(), i, inNumPoints);
272 range[i] = new DataPoint(latitude, longitude, altitude);
278 * Interpolate between the two given points
279 * @param inStartPoint start point
280 * @param inEndPoint end point
281 * @param inFrac fractional distance from first point (0.0 to 1.0)
282 * @return new DataPoint object between two given ones
284 public static DataPoint interpolate(DataPoint inStartPoint, DataPoint inEndPoint, double inFrac)
286 if (inStartPoint == null || inEndPoint == null) {return null;}
287 return new DataPoint(
288 Coordinate.interpolate(inStartPoint.getLatitude(), inEndPoint.getLatitude(), inFrac),
289 Coordinate.interpolate(inStartPoint.getLongitude(), inEndPoint.getLongitude(), inFrac),
290 Altitude.interpolate(inStartPoint.getAltitude(), inEndPoint.getAltitude(), inFrac)
295 * Calculate the number of radians between two points (for distance calculation)
296 * @param inPoint1 first point
297 * @param inPoint2 second point
298 * @return angular distance between points in radians
300 public static double calculateRadiansBetween(DataPoint inPoint1, DataPoint inPoint2)
302 if (inPoint1 == null || inPoint2 == null)
304 final double TO_RADIANS = Math.PI / 180.0;
305 // Get lat and long from points
306 double lat1 = inPoint1.getLatitude().getDouble() * TO_RADIANS;
307 double lat2 = inPoint2.getLatitude().getDouble() * TO_RADIANS;
308 double lon1 = inPoint1.getLongitude().getDouble() * TO_RADIANS;
309 double lon2 = inPoint2.getLongitude().getDouble() * TO_RADIANS;
310 // Formula given by Wikipedia:Great-circle_distance as follows:
311 // angle = 2 arcsin( sqrt( (sin ((lat2-lat1)/2))^^2 + cos(lat1)cos(lat2)(sin((lon2-lon1)/2))^^2))
312 double firstSine = Math.sin((lat2-lat1) / 2.0);
313 double secondSine = Math.sin((lon2-lon1) / 2.0);
314 double term2 = Math.cos(lat1) * Math.cos(lat2) * secondSine * secondSine;
315 double answer = 2 * Math.asin(Math.sqrt(firstSine*firstSine + term2));
322 * Resize the value array
323 * @param inNewIndex new index to allow
325 private void resizeValueArray(int inNewIndex)
327 int newSize = inNewIndex + 1;
328 if (newSize > _fieldValues.length)
330 String[] newArray = new String[newSize];
331 System.arraycopy(_fieldValues, 0, newArray, 0, _fieldValues.length);
332 _fieldValues = newArray;
338 * @return a clone object with copied data
340 public DataPoint clonePoint()
343 String[] valuesCopy = new String[_fieldValues.length];
344 System.arraycopy(_fieldValues, 0, valuesCopy, 0, _fieldValues.length);
345 // Make new object to hold cloned data
346 DataPoint point = new DataPoint(valuesCopy, _fieldList, _altitude.getFormat());
352 * Restore the contents from another point
353 * @param inOther point containing contents to copy
354 * @return true if successful
356 public boolean restoreContents(DataPoint inOther)
360 // Copy string values across
361 _fieldValues = inOther._fieldValues;
362 if (_altitude != null)
364 parseFields(_altitude.getFormat());
368 // use default altitude format of metres
369 parseFields(Altitude.FORMAT_METRES);
378 * Get string for debug
379 * @see java.lang.Object#toString()
381 public String toString()
383 return "[Lat=" + getLatitude().toString() + ", Lon=" + getLongitude().toString() + "]";